Java prob
Hello
Im having some new Java problems, while trying to export my project to a JAR file in Eclipse. When I run it from the IDE its OK, all roses and bourbon, but I get hit over the head this when I run it from cmd :
C:\Users\Joey\Desktop>java -jar FLIPMEMATEY.jar
Exception in thread "main" java.lang.NullPointerException
at henry.eats.cake(Hello.java:84)
..print stack
In line 84, I need to read some pics in a List that are located in "src\\PICS". By carefully examining the newly created JAR file Ive noticed that the folder PICS is now in root of the JAR.
Could that be the problem :?
Some directions please?
Thank you very much
Joey
Joey_Brown
Junior Poster in Training
73 posts since May 2010
Reputation Points: 10
Solved Threads: 1
What code causes the NullPointerException at line 84?
You should include all you image files in the jar file so you don't need to worry about reading them from the disk.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
What code causes the NullPointerException at line 84?
You should include all you image files in the jar file so you don't need to worry about reading them from the disk.
Yes, the all the files are included in the JAR. Now when I create a new folder called src and place the PICS folder inside and then copy it to the JAR, it works!
Im using a absolute path "src\\PICS" in my project.
Joey_Brown
Junior Poster in Training
73 posts since May 2010
Reputation Points: 10
Solved Threads: 1
Have you fixed the problem now?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Have you fixed the problem now?
No, because the files need to be in the same folder as my JAR.. Only in that case it works. Ive tried to use this
URL url = this.getClass().getClassLoader().getResource(path);
ImageIcon icon = new ImageIcon(url);
But to no avail..
It just cant read my picture files inside the JAR.
Joey_Brown
Junior Poster in Training
73 posts since May 2010
Reputation Points: 10
Solved Threads: 1
because the files need to be in the same folder as my JAR
No, the files need to be IN THE JAR.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
No, the files need to be IN THE JAR.
Yes, the files are in the JAR all along.
FLIPMEMATEY.jar ROOT:
prog
PICS
META-INF
.classpath
.project
manifest.MF
Doesnt work, nullpointer ex [works fine inside the IDE]
But when I do this:
Create new folder, place the jar inside along with the PICS folder
SOMENEWFOLDER root:
FLIPMEMATEY.jar
PICS
It works. Now why is that?
Does it have something to do with the way Ive declared the FILE path inside the project:
FILE read = new FILE("src\\PICS\\")
The code in line 84 that makes the exception is a call to a function that returns the List object with the pictures inside the PICS folder.
Joey_Brown
Junior Poster in Training
73 posts since May 2010
Reputation Points: 10
Solved Threads: 1
If you're using Windows then it's possibe to get this problem because Windows file names are not case-sensitive, but files within a jar are case-sensitive. So if the file name in you code differs in case from the real file name it will work in Windows, but not in a jar.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
If you're using Windows then it's possibe to get this problem because Windows file names are not case-sensitive, but files within a jar are case-sensitive. So if the file name in you code differs in case from the real file name it will work in Windows, but not in a jar.
Sir James man, you are the boss.:)
Great hint, works like a charm now. ::respect ::
Joey_Brown
Junior Poster in Training
73 posts since May 2010
Reputation Points: 10
Solved Threads: 1
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
FILE read = new FILE("src\\PICS\\")
This usage requires that the files be on the disk.
You should change how you access the image files to treat them as resources which will allow you to put them into the jar file and read them from there.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656