Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creating a JAR file: File finding problem

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NOTE: I hesitated whether to put this topic in the SWING Forum or in this Forum since I'm using the ImageIcon class, put this consists a problem with a JAR file.

Hello everyone.

I'm working on a project that has several CLASS files and PNG files (for icons). I want to be able to send the BETA version of this project to others efficiently. Obviously this means I need to put everything into a JAR file. I'm using BlueJ (an IDE) to create a JAR file in which I add the CLASS and PNG files (because without the icons the program wouldn't work properly).

When I launch the JAR file the project launches fine but the it doesn't recognize the Icon files in the JAR file, so it doesn't load them (which basically makes the project totally useless! )

I know I have to change something in the code, but I don't know what to put exactly. This is the code of the class containing the ImageIcon objects (which most of the other classes in the project use). I've tried adding ".\NameOfJarFile.jar\" before the filename. But that didn't work.



Thank you.
 
Marshal
Posts: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, but have you got folders inside the .jar? In which case you might need to precede the name of the .png with the folder name.

And try / rather than \ in the folder names, so as to maintain platform independence.
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You used a constructor whose description says "Creates an ImageIcon from the specified file". But your images aren't in files, they are resources in a jar. Assuming your images are in the root of the jar's directory structure, I would try this:
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I realized I made I slight mistake in my post. By ".\NameOfJarFile.jar\" I actually meant that I put ".\\NameOfJarFile.jar\\". There are two things that I don't fully understand.

Firstly, Campbell mentioned I should put / instead of \ to maintain platform independence. But how could it maintain platform independence when I Windows the backslash \ is used :?: (I know Linux and MacOS use / but Windows is different, would a / still work in Windows?)

Secondly I have no idea what the method Icons.class.getResource(String) does or where it comes from. Could you please briefly explain it to me or give me a link to it's API.

Regardless to that I still tried what Paul suggested and it worked fine when I ran it directly from the IDE. But after compressing the classes and image files into a JAR file the compiler gave me this error:

Exception in thread "main" java.lang.ExceptionInInitializerError
at Cell.<init>(Cell.java:24)
at Board.<init>(Board.java:54)
at Application.<init>(Application.java:14)
at MainClass.main(MainClass.java:17)
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at Icons.clinit>(Icons.java:11)
... 4 more



I did recompile the classes and I checked that the PNG files are in the JAR file, so that's not the issue.
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wait, I just got it working. I didn't realize that I had type Icons.class.getResource("\\0.png") instead of Icons.class.getResource("/0.png") (Which is what Paul suggested). It works when I put a / instead of \\.

Still, I don't understand what the method Icons.class.getResource(String) does or where it comes from and I don't see why a / works whilst a \\ doesn't (even though I'm in Windows). I would appreciate any clarifications. Thank you
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check Class.getResource, Class.getResourceAsStream and equally named methods in the ClassLoader class.
 
Campbell Ritchie
Marshal
Posts: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, but I am not certain, that the Windows version of the JVM can translate / to \ in file paths. I don't know, but I don't think it works the other way round. You could easily find out by passing file names like C:/DocumentsAndSettings/Olivier/MyDocuments/myFile.txt and \home\olivier\Documents\myfile.txt on different operating systems and seeing which one works and which produces FileNotFoundExceptions.
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not the JVM itself, it's java.io.File that does the translation.
 
Campbell Ritchie
Marshal
Posts: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Rob
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I understand the / and \ thing now.

But when I check the Java API for the method class.getResource(String name) it says "Finds a resource with a given name" and returns a URL. So basically if I'm getting this right the method gets a resource of the class and appends the String name. I don't quite understand what they mean by "resource" though. It is something like File Address / Location? :?
 
I will suppress my every urge. But not this shameless plug:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic