Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get application path? Is that my method in getting path wrong?

Reply
Thread Tools

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

 
 
James
Guest
Posts: n/a
 
      12-07-2004
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


 
Reply With Quote
 
 
 
 
joeking@merseymail.com
Guest
Posts: n/a
 
      12-07-2004
James wrote:
> 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- ><>

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-07-2004
On Tue, 7 Dec 2004 17:39:26 +0800, James wrote:

> 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..
<http://www.physci.org/codes/javafaq.jsp#path>

> 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>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
How get the virtual path based on the physical path? marss ASP .Net 5 09-13-2007 04:20 PM
Getting the application path Water Cooler v2 ASP .Net 6 09-24-2005 09:28 PM
Hi I have one web application and i want to get the number of users who are currently accessing the application. Also I want to get the user details of each user, which is stored in a database. How can I do this? Pls help. Getting No: and anu Java 11 05-12-2005 03:25 PM
how to get application path when the application start? haiwen ASP .Net Web Controls 1 01-28-2004 03:21 PM



Advertisments