how to get application path? Is that my method in getting path wrong?

J

James

eg.
c:\myapp\test.class
c:\myapp\icon\icon1.gif
c:\myapp\setting.cfg

when I compile and run test.class
in coding, I write
something.setImageIcon(getClass().getResource("/icon1.gif"));
after process something, the program will save the file setting in
setting.cfg
something.savefile(new
File(getClass().getResource("/setting.cfg").toString()));

all of this run ok, the image got load up, then when save setting, it got
write back into setting.cfg file.

Later I group all of this in 1 jar file.
then when I try the program, it cannot run anymore. something related to
nullpointerexception.

I try to get out the path and I found out that when running in normal
class(without jar), that code can get correct path and the file.
but when run in jar(with main class) that code can't get correct path.
It got something at between the parent path and class path.
at center it come out something like jar file(it act like 1 level path)

so I want to ask you all, normally how do you all get the running path
correctly so that can load the image and save file back to running class's
path
 
J

joeking

James said:
eg.
c:\myapp\test.class
c:\myapp\icon\icon1.gif
c:\myapp\setting.cfg

when I compile and run test.class
in coding, I write
something.setImageIcon(getClass().getResource("/icon1.gif"));
after process something, the program will save the file setting in
setting.cfg
something.savefile(new
File(getClass().getResource("/setting.cfg").toString()));

all of this run ok, the image got load up, then when save setting, it got
write back into setting.cfg file.

Later I group all of this in 1 jar file.
then when I try the program, it cannot run anymore. something related to
nullpointerexception.

Any specific 'something', or are you inviting us to guess what your
actual error was?

I try to get out the path and I found out that when running in normal
class(without jar), that code can get correct path and the file.
but when run in jar(with main class) that code can't get correct path.
It got something at between the parent path and class path.
at center it come out something like jar file(it act like 1 level path)

Examples?


so I want to ask you all, normally how do you all get the running path
correctly so that can load the image and save file back to running class's
path

When working with getResource() you aren't really working with regular
files on a disk, like you would with a java.io.File object, but data
which acts as resources to classes. Physically these can be stored in
many ways - just as class files themselves can be stored in many ways:
as files on the file system, as entries in a Jar, even as records in
a database - so long as there is a classloader which knowns how to
fetch them from a given identifier String.

If you want to read and write to a local disk, use java.io.File
and an appropriate chain of Input/Output and Reader/Writer streams.
Don't use getResource() , as when your representation changes (like
you go from packages mapped out on the local disk, to packed in
a Jar file) then things are likely to break.


-FISH- ><>
 
A

Andrew Thompson

eg.
c:\myapp\test.class

is 'test.class' in 'myapp' package, or is it in the default package?
c:\myapp\icon\icon1.gif
c:\myapp\setting.cfg

when I compile and run test.class
in coding, I write
something.setImageIcon(getClass().getResource("/icon1.gif"));

If 'default'. The path to the icon is..

URL url = getClass().getResource("/icon/icon1.gif");
// now TEST the URL!
System.out.println( "icon1 URL: " + url );
something.setImageIcon( url );
after process something, the program will save the file setting in
setting.cfg
something.savefile(new
File(getClass().getResource("/setting.cfg").toString()));

You cannot save the data back into the class jar, and it it
best *not* to write it in the progtam path but the user's
'user.home' directory..
all of this run ok, the image got load up, then when save setting, it got
write back into setting.cfg file.

Later I group all of this in 1 jar file.
then when I try the program, it cannot run anymore. something related to
nullpointerexception.

To expand on what FISH wrote..
<http://www.physci.org/codes/javafaq.jsp#exact>
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top