This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am currently working on a relatively simple Java application that uses Swing for its GUI. As such, there are several image files that it needs to load for icons etc. There are also a few text files that it might need to read for help files.
Ideally, the entire application, including all of the picture/text files, will be packaged into a single JAR file. At runtime, the application would be able to load the required files out of the JAR file.
Now, I'm not sure if there is a simple solution to this. If there is, please share because my googling hasn't given me much success.
Alternatively, I suppose what the program needs to do is find the JAR file that is being loaded from - this in itself is not an easy task because the working directory may not always be the same as the directory with the JAR in it. After finding the JAR file, I figure I can use the JAR API included with Java to pull the required files out and load them up.
They will at least need a JRE to run your Java program, right? And, all modern JREs come with Java WebStart right? And, you will probably be packaging your application as an executable jar, right? Then, why not use WebStart if you're going to be serving the application from a webserver anyway?
James Carman, President<br />Carman Consulting, Inc.
Joined: Dec 03, 2000
Thanks for your reply James. It seems that I have found a solution to the problem myself.
Basically the solution I have found is to use the getResource() method in the Class class to retrieve a URL to the file I need. This works whether the image or other required external file is inside the JAR file or not. This is all on
Yes, that's true. You can use the classloader to load resources from the classpath. And, you don't need to know where your jar file is located for that. Sorry, I think I concentrated too much on the "I don't want them to have to install anything" part. It sounds like you're on the right track. Good luck!