Load images and text file from jar file


F

Fuma

Hello,
I know this question has been asked before, but I cannot seem to
get the solutions posted to work. I am trying load to three images and
a text file from a jar file (this jar file is not in the classpath).
This jar file is the main jar file though. I intend for users to be
able to download this file to their desktop, double click, and run.
Unfortunately, I cannot figure out how to load the three images and
textfile from the jar file.

I have tried this solution but it doesnt want to work:

URL url = this.getClass().getResource("Teacher_online.jpg");
image = new ImageIcon( url );

and it says it can not file the file. And for the text file i have

URL url = this.getClass().getResource("config_student.ini");
ins = new InputStreamReader(url.openStream());
bufferedReader = new BufferedReader( ins );

If somone could shed some light on this problem i am having, i would
really appreciate it!

Thanks,
Chris
 
Ad

Advertisements

F

Filip Larsen

Fuma wrote
I have tried this solution but it doesnt want to work:

URL url = this.getClass().getResource("Teacher_online.jpg");
image = new ImageIcon( url );

and it says it can not file the file.

As the documentation says, the Class.getResource method will prepend the
"package path" to resource names that do not begin with a slash. In your
case, if file is placed as images/myfile.jpg in your jar-file you
probably want to call it with .getResource("/images/myfile.jpg").


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top