Reading resources from JAR file

M

MikL

The following code works fine when the resource is a free-standing file, but
when it's packaged up in a JAR it can't find it. What methods should I use
to read it from a JAR?

BufferedReader r
= new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("clientSchema.sql")));
 
A

Andrew Thompson

The following code works fine when the resource is a free-standing file, but
when it's packaged up in a JAR it can't find it.

Where in the jar do you put it? How do you reference it?
..What methods should I use
to read it from a JAR?

BufferedReader r
= new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("clientSchema.sql")));

What do you do with the exceptions you are getting?

In any case, break this down and find the resource first.

// ensure the capitals are exactly the same
URL resource = getClass()("clientSchema.sql");
System.out.println("URL to resource: " + resource );

BufferedReader r ..
 
A

Alex Kizub

Two points for attention.
1) you use realtive path.
clientSchema.sql
so this particular file should be in the same directory where is class located.
So, if you have this method in class abc.xyz.Test then clientSchema.sql should
be in directory
abc/xyz starting somewhere in your classpath.
It's better idea to use absolute path.
For example /config/clientSchema.sql
It will work if you have directory config in the roor of your jar file.

2) Always use package or at least one step down in your directory structure.
For some reason /clientSchema.sql doesn't work when /config/clientSchema.sql
works fine.

Alex Kizub.
 
M

MikL

Andrew Thompson said:
Where in the jar do you put it? How do you reference it?
getClass().getResourceAsStream("clientSchema.sql")));

What do you do with the exceptions you are getting?

In any case, break this down and find the resource first.

// ensure the capitals are exactly the same
URL resource = getClass()("clientSchema.sql");
System.out.println("URL to resource: " + resource );

BufferedReader r ..

Found it. My (three) errors, of course.
1) It was a different piece of code running - hard to tell when the
"uncaught error" stack trace only lists 5 com.sun.imageio.* method calls.
2) The code was using getSystemResource() which doesn't find things that
getResource() does.
3) The code didn't validate the URL before handing it to
Toolkit.getDefaultToolkit().getImage(vURL);

Thanks for your help. It was of course while producing an executable
example that I found error #1 above, which of course led to finding #2 & #3.
(smites forehead, mutters "always create an executable example FIRST".. :)
 
M

MikL

Alex Kizub said:
Two points for attention.
1) you use realtive path.
clientSchema.sql
so this particular file should be in the same directory where is class located.
So, if you have this method in class abc.xyz.Test then clientSchema.sql should
be in directory
abc/xyz starting somewhere in your classpath.
It's better idea to use absolute path.
For example /config/clientSchema.sql
It will work if you have directory config in the roor of your jar file.

2) Always use package or at least one step down in your directory structure.
For some reason /clientSchema.sql doesn't work when /config/clientSchema.sql
works fine.

Alex Kizub.

Alex, I totally agree. It turns out my errors were elsewhere. Thanks for
your input.
 
H

hiwa

MikL said:
The following code works fine when the resource is a free-standing file, but
when it's packaged up in a JAR it can't find it. What methods should I use
to read it from a JAR?

BufferedReader r
= new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("clientSchema.sql")));

If your clientSchema.sql is in your JAR file's top-level, i.e. root,
then you have to write "/clientSchema.sql" not "clientSchema.sql".
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top