wood burning stoves 2.0*
The moose likes Java in General and the fly likes Problem while executing jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem while executing jar file" Watch "Problem while executing jar file" New topic
Author

Problem while executing jar file

Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi,

I have an executable jar file which uses the SingleFrameApplication class. I am using netbeans 6.1 as my IDE.
In my code I have a reference to a properties file which is located in the src folder inside some package. When I run the application from netbeans, it runs fine but when i double click on the jar file though the application runs fine but when i try to access the properties file an exception is thrown..
The jar file is in the following path C:\Documents and Settings\15760\My Documents\NetBeansProjects\UTS\dist
Since user.dir is a system property which gives the current system directory i m in the above directory and there is no path saying src\\uts\\config\\envConfig.properties so i m unable to reach the file.

How do i fix this problem? My basic intention is to run the project with the help of jar file in some other machine which does not have an IDE or anything..

Following are the lines of code which i m using to access the properties file..


Regards,
Suhas


Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10425
    
    8

Check out http://www.coderanch.com/t/382171/Java-General/java/Reading-properties-file-jar-META
Notice the use of getResourceAsStream()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
The link is not of much help because the problem is not in my code. When i run the code from the ide itself everything runs fine. The problem is while executing the jar i need the path that i have specified in the above code to be inside the same directory of the jar.

I m getting file not found exception.
As i said my objective is to run the jar file in some other machine.

Please advise.

Regards,
Suhas
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What Maneesh meant was to stop using File and FileInputStream and use the InputStream returned by Class.getResourceAsStream instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi,

Ok. But I am also using a storing operation as follows. How do I tweak this code?



where my propFile is the file object that I am using in the code.

Regards,
Suhas
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

System.getProperty("user.dir") returns the path from which the application was launched. That doesn't have to be the path where the JAR file is located. Therefore, it perhaps is a better idea to choose a different folder; something based on System.getProperty("user.home") perhaps. The user's home folder rarely changes, but it does mean that each user has its own version of the file. You can combine both the system and user specific files using cascading Properties objects: you put the system properties in a resource inside the JAR file which you then load into one Properties object. You then create a second Properties object with the first Properties object as its defaults. The second Properties object will then return the first Properties object's values if it doesn't have the properties itself. In code (ignoring closing of streams and null checks for clarity):
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Thanks!!!
 
GeeCON Prague 2014
 
subject: Problem while executing jar file