0

This question is edited to include everything asked. New picture links will be added below.

My program runs fine in NetBeans, and cannot load the program when running from its jar file, compiled by NetBeans. I'm compiling on Java 1.6_45, which is simply for compatibility, since this a school-oriented program, for older machines that hardly ever receive any upgrades or updates.

Anyhow, because the program never loaded when I tried it as a .jar file, I was concerned and looked up manual ways to make the jar run. I tried the command prompt command java -jar PhysicsEquations.jar , which I copied straight into my Users folder so I wouldn't have to worry about classpaths.

This is the error message I get (below, first link), an IllegalArgumentException. I'm not sure what to do, and I've researched for a couple of days now trying different things. I'm up for retrying anything though if it could be better explained.

Here is the entire EquationImage class (just a couple lines, bear with me, please). In other code, all I do with it is call the constructor, such as EquationImage fma = new EquationImage("fma.jpg"); and then use setBounds(x,y,width,height); to add it to a panel that's added to my CardLayout with all of the equations.

package physicsequations;
...
public class EquationImage extends JPanel {
    public EquationImage() {
    }

    public EquationImage(String filename) {
        try {
            System.out.println(filename);
            imageBuffer = ImageIO.read(getClass().getResourceAsStream("images/" + filename));
        }
        catch(IOException e) {
            JOptionPane.showMessageDialog(null, "EquationImage could not find/load the file \n" + filename, "Error Message", JOptionPane.PLAIN_MESSAGE);
        }
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.drawImage(imageBuffer, 0, 0, null);
    }

    private BufferedImage imageBuffer = null;
}

As notable in the code, and the error message, the constructor does receive a filename. In NetBeans, the compiler is able to find all of the images correctly. However, in the jar that I get using 'Clean and Build', it can vary whether or not I actually see a JOptionPane with the error.

I was asked to provide filepaths that I have for my code.

In the project, I have the folder PhysicsEquations. PhysicsEquations has folders dist, build, nbproject, and src. PhysicsEquations also has files build, and manifest.mf.

src has the folder physicsequations, the package that EquationImage is in.

physicsequations then has five files and a folder 'images'. The files are Main, EquationPanel, EquationImage, BadInputException, and Variable.

images has all of my pictures, including Physics.jpg, which is the icon I use in Main:

String imagePath = "physics.jpg";
InputStream imgStream = Main.class.getResourceAsStream("images/" + imagePath);
BufferedImage myImg = null;
try {
    myImg = ImageIO.read(imgStream);
}
catch(IOException e) {
    JOptionPane.showMessageDialog(null, "The program has failed to load file physics.jpg from the source package.", "Error Message", JOptionPane.ERROR_MESSAGE);
}
setIconImage(myImg);

In the jar itself, I open up the jar and have 2 folders: META-INF and physicsequations. In the folder physicsequations, there are 101 class files, and a folder called images. That folder has all 17 of my pictures used in the project. Below is the text of the manifest.mf document in META-INF from the jar. (Can someone explain to me why it says 1.8? I have 1.6 in my project properties...)

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_31-b13 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: physicsequations.Main

https://i.stack.imgur.com/IqgzE.jpg
https://i.stack.imgur.com/rHaDa.jpg
https://i.stack.imgur.com/j5Npw.jpg
https://i.stack.imgur.com/Yt2O3.jpg

I'm hoping all of the links will submit since it says I have limit with 10 rep.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • have you checked, if the image is even included in the jar? –  May 07 '15 at 04:03
  • 1
    What path are you using to the images? When using `getClass().getResource(...)` if you use a relative path, class's package name will be prefixed to the path. You can use `getClass().getResource("/...")` for absolute paths, which appends the path to the `classpath` search path – MadProgrammer May 07 '15 at 04:03
  • A jar file is just a zip file. Open the jar file (rename the extension as zip). Are your images in the jar file? – Brett Walker May 07 '15 at 04:05
  • I opened the jar file. I have two folders, one of which is physicsequations, and I see all of my images right there in the folder. –  May 07 '15 at 04:16
  • I don't quite understand you MadProgrammer, sorry. Classpath stuff confuses me quite a bit. –  May 07 '15 at 04:17
  • Outside of the jar, I have Physics Equations/src/physicsequations/... which has all of my java and jpg files (which was an attempt at trying to get everything in the same folder to make it run...) –  May 07 '15 at 04:20
  • Please edit your question to include the block of code which is trying to load these images, the exception that you are getting, and the pathnames of these image files within the jar. – Kenster May 07 '15 at 10:23
  • I followed your advice haraldK, and this was not my solution. It, in fact, corrupted the project by moving around files and making new stray folders outside of the project. I will be starting a new project to fix this. I will also try to update this question afterwards... –  May 08 '15 at 01:41
  • I have edited to include all of the information requested, and I have screenshots of my project properties in key places of the classpaths to help with the problem. –  May 08 '15 at 03:15
  • Quote: "...including Physics.jpg, which is the icon I use in Main: `String imagePath = "physics.jpg"`" (actually, it seems neither spellings are correct, as the last image shows the extensions are upper-case ".JPG"). You are aware the names are *case sensitive*, right? How about posting the output of `jar tf yourjar.jar`? – Harald K May 08 '15 at 08:31

1 Answers1

0

From looking at your first linked image which states you are trying to load the resource "fma.jpg" and IllegalArgumentException: input == null and your last image which clearly shows that the file included in your project is named fma.JPG I think it's pretty clear what is going on (PS: None of your images, as far as I can tell, shows a file named "physics.jpg", "Physics.jpg" or "physics.JPG", but I assume the problem is the same here):

Any resource lookups using Class.getResource(...) or Class.getResourceAsStream(...) are case sensitive. It probably worked when you were running from the file system, as the normal Windows file system is not case sensitive, and will resolve the file anyway. However, when inside the JAR, the matching is case sensitive and the lookup won't work.

You have two choices:

  • Either rename your files to the more common standard of lower case extensions (ie. rename "fma.JPG" to "fma.jpg").
  • Or change your code to actually look for the resources you have in your JAR (ie. change the code from "fma.jpg" to "fma.JPG").

To verify that everything is correct, you can run the command:

jar tf yourjar.jar 

(the t option list the table of contents of the file). It will produce output similar to the following example:

META-INF/
META-INF/MANIFEST.MF
TicTacToe.class
TicTacToe.class
TicTacToe.java
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
example1.html
images/
images/cross.gif
images/not.gif 

Verify that the names in the list and the code are exactly the same, and that the spelling matches.


PPS: About the 1.8 reference:

Created-By: 1.8.0_31-b13 (Oracle Corporation)

This is simply the version of the jar command (typically the same version as the JDK that contains it) that created the JAR. This could be different than the (javac) version used to build the project, or the JDK library version you require for building.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Thanks, I will try this and get back to you on it. This would make sense, but I didn't know my file extensions were uppercase...I simply saved them as JPEG's when I was using the snipping tool. I will absolutely try this. I had a project that I had similar to this, but never encountered the problem...I used png, not JPG. Thank you. –  May 08 '15 at 11:56
  • Thank you. This was an issue with the case sensitivity. I didn't know there were different case sensitivities for JPEGs and I thought they were all the same .jpg. I have fixed this now, thank you so much. –  May 08 '15 at 15:34