-1

I feel like I've tried everything. These are some of the examples of the code I've tried which runs perfectly fine inside the IDE, but as soon as it's packaged in a jar file, it falls apart. Every file is on the same level and packaged inside the jar file. I open it with WinRar and see that the image "c3.png" is absolutely inside, right next to my Main-Class, in the root of the Jar.

C3 = new ImageIcon( getClass().getResource("c3.png") );

.

C3 = new ImageIcon( this.getClass().getResource("c3.png") );

.

C3 = new ImageIcon( ImageIO.read( getClass().getResource("c3.png") );

.

C3 = new ImageIcon( ImageIO.read( ClassLoader.getSystemClassLoader().getResourceAsStream( "c3.png" ) ) );

.

C3 = new ImageIcon( ImageIO.read( getClass().getClassLoader().getResource( "C3.png" ) ) );

edit: I've tried some more that have... of course... failed.

C3 = new ImageIcon(VP2CPConverter.class.getResource("/C3.png"));

.

C3 = new ImageIcon( VP2CPConverter.class.getClassLoader().getResource( "C3.png" ) );

In a brief troubleshooting attempt, I even tried getResourceAsStream() to find out every resource available when the Jar executes. Inside the IDE, all of them. In the Jar... NONE of them.

Somebody shoot me now.

mfgravesjr
  • 37
  • 5
  • Which tool are you using to generate the jar file? – Sikandar Sahab Apr 30 '18 at 10:19
  • Question edited and cruft removed. Better to show more pertinent information, including if needed the visualized structure of your class, the stacktrace, the line at fault, an [mcve]. – Hovercraft Full Of Eels Apr 30 '18 at 10:20
  • Can you please share the stack trace or the error, that might really help to solve your problem. – Ran Apr 30 '18 at 10:20
  • @HovercraftFullOfEels Just so you know, the solution listed in that existing question is exactly the same attempt I listed above. – mfgravesjr Apr 30 '18 at 10:38
  • @BadshahTracker I'm using the command line: jar cvfe VP2CPConverter.jar VP2CPConverter *.class *.png – mfgravesjr Apr 30 '18 at 10:38
  • @Ran The stack trace reads as: Exception in thread "main" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) at VP2CPConverter.(VP2CPConverter.java:110) at VP2CPConverter.main(VP2CPConverter.java:283) Sorry for the misnomer of "NullPointerException", but still: input == null – mfgravesjr Apr 30 '18 at 10:38
  • @mfgravesjr where's your manifest.txt file? – Sikandar Sahab Apr 30 '18 at 10:51
  • @BadshahTracker I tried creating my own which may have been in the root or a meta-inf folder, and that didn't work; then I had the jar compiler create one using a set entry-point (that's the 'e' command in "cvfe") and it's in a meta-inf folder in the root of the jar. Comparing the two manifests, there is nothing different between the two files. – mfgravesjr Apr 30 '18 at 20:57
  • @BadshahTracker Btw, it's not a manifest.txt it's manifest.mf. – mfgravesjr Apr 30 '18 at 21:03
  • Open your jar file with your favorite un-zipping utility and take a picture of the file structure and post it here. A picture is worth a thousand words. – Hovercraft Full Of Eels Apr 30 '18 at 22:05
  • I can't take a picture of all of the contents because there are about 60 png images that won't fit on the screen. There's a META-INF folder with a MANIFEST.MF file. WindowPositioner.class, VP2CPConverter.class, VP2CPConverter$1.class, and VP2CPConverter$2.class. What do you need from that information? – mfgravesjr May 01 '18 at 00:23
  • @HovercraftFullOfEels I also updated the question to include some further troubleshooting steps. Nothing's working. – mfgravesjr May 01 '18 at 01:10
  • 1
    @BadshahTracker The manifest file has nothing to do with it. – user207421 May 01 '18 at 02:09
  • @mfgravesjr are you typing the all image names including the extensions, while you run cmd using your command: jar cf MyJar *.java image1.png image2.jpg image3.jpg ????? – Sikandar Sahab May 02 '18 at 04:35
  • @BadshahTracker I am just using the wildcard. (*.png) And I've verified that every png file I need is inside that jar file. – mfgravesjr May 02 '18 at 14:48
  • @HovercraftFullOfEels http://i1065.photobucket.com/albums/u400/mfgravesjr/Untitled_zpsincrlbf5.png... I moved the images to a different directory like you suggested, and I changed the path in the code. Still nothing. It works when I run it in IDE but it doesn't work when running the jar file – mfgravesjr May 04 '18 at 00:18

1 Answers1

1
C3 = new ImageIcon( getClass().getResource("c3.png") );
C3 = new ImageIcon( this.getClass().getResource("c3.png") );

These two are identical, and assume that the resource is present in the folder represented by the package name of the current class. Clearly it isn't.

C3 = new ImageIcon( ImageIO.read( getClass().getResource("c3.png") );

This makes the same assumption.

C3 = new ImageIcon( ImageIO.read( ClassLoader.getSystemClassLoader().getResourceAsStream( "c3.png" ) ) );

This makes the assumption that the resource is present in the root folder of the JAR file. Clearly it isn't.

C3 = new ImageIcon( ImageIO.read( getClass().getClassLoader().getResource( "C3.png" ) ) );

This makes the same assumption.

C3 = new ImageIcon(VP2CPConverter.class.getResource("/C3.png"));

This makes the same assumption.

C3 = new ImageIcon( VP2CPConverter.class.getClassLoader().getResource( "C3.png" ) );

This makes the same assumption.

Clearly all the above assumptions are false. So make one of them true, and use the corresponding code that assumes it. If you want to get the resource via the current class, put it into the current class's package. If you want to get it via the class loader or a path starting in /, put it into the root directory of the JAR file.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • You missed the conversation. Every file IS in the root of the Jar file (opened with WinRar). The C3.png is in the same location as VP2CPConverter.class which calls getResource(). I don't have to assume, as I can see the image there. But for some reason, the class cannot see any of the resources that are available to it. – mfgravesjr May 01 '18 at 11:04
  • @mfgravesjr: and you've not yet posted the *image* of your jar's file structure yet as has been requested of you. If the images are in the *root* of the jar, then that's your problem and you must move them, as the duplicates will tell you. – Hovercraft Full Of Eels May 01 '18 at 11:19
  • 1
    I missed nothing. The behaviour of your code proves that what you claim isn't true. – user207421 May 01 '18 at 22:31
  • @EJP If you don't believe me about my own problem, then that's your problem and you're going to be of absolutely no help. The list of directories and files inside the root of the jar file is: – mfgravesjr May 02 '18 at 15:24
  • META-INF, C3.png, D3.png, E3.png, F3.png, G3.png, A3.png, B.png, C4.png, D4.png, E4.png, F4.png, G4.png, A4.png, B4.png, C5.png, D5.png, E5.png, F5.png, G5.png, A5.png, B5.png, C6.png, D6.png, E6.png, F6.png, G6.png, A6.png, B6.png, C7.png, D7.png, E7.png, F7.png, G7.png, A7.png, B7.png, C8.png, C3#.png, D3#.png, F3#.png, G3#.png, A3#.png, C4#.png, D4#.png, F4#.png, G4#.png, A4#.png, C5#.png, D5#.png, F5#.png, G5#.png, A5#.png, C6#.png, D6#.png, F6#.png, G6#.png, A6#.png, B6#.png, C7#.png, D7#.png, E7#.png, F7#.png... – mfgravesjr May 02 '18 at 15:24
  • G7#.png, A7#.png, B7#.png, VP2CPConverter.class, VP2CPConverter$1.class, VP2CPConverter$2.class, and WindowPositioner.class. – mfgravesjr May 02 '18 at 15:24
  • @HovercraftFullOfEels I'll try moving all the images to a new folder and changing the path, but having the resources in the root has worked before for me... I'm pretty sure, anyways. I just won't get a chance to try it out until tonight. – mfgravesjr May 02 '18 at 15:30
  • @mfgravesjr: don't misunderstand EJP or bite the hand that feeds you. He understands your problem better than you or I, and believes that you have a problem, but my feeling is that it **is** a duplicate and that it likely stems from your misunderstanding of where resources should be or how to use them. Don't post meaningless file names which don't help us one bit but again post an image of the file structure as has been asked (repeatedly). – Hovercraft Full Of Eels May 02 '18 at 16:23
  • @EJP http://i1065.photobucket.com/albums/u400/mfgravesjr/Untitled_zpsincrlbf5.png.html I moved the images to a different directory like HovercraftFullOfEels suggested, and I changed the path in the code. Still nothing. It works when I run it in IDE but it doesn't work when running the jar file. – mfgravesjr May 05 '18 at 13:49
  • @HovercraftFullOfEels I have used executable Jar files several times in the past. getClass().getResource(...) was all I needed before. There's something probably wrong with my system somewhere, and I'd like to get some leads on what it is before uninstalling and reinstalling java. – mfgravesjr May 05 '18 at 23:09
  • @mfgravesjr: again it could be the path. Please post a link to a decent **image** not a web page that clearly shows your jar file structure. Your images should be in a directory that branches from the class files, not before the class files. – Hovercraft Full Of Eels May 06 '18 at 19:02
  • @HovercraftFullOfEels That link IS an image. When I click on it, it takes me to photobucket which shows a decent image that clearly shows my jar file structure. Are you having problems with your browser? I'll try a different link. Try [this][http://s1065.photobucket.com/user/mfgravesjr/media/Untitled_zpsincrlbf5.png.html] and [this][http://s1065.photobucket.com/user/mfgravesjr/media/Untitled_zpsicfzwfw5.png.html]. – mfgravesjr May 08 '18 at 10:59