• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem while executing jar file

 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Maneesh meant was to stop using File and FileInputStream and use the InputStream returned by Class.getResourceAsStream instead.
 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic