• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem while executing jar file

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out https://coderanch.com/t/382171/Java-General/java/Reading-properties-file-jar-META
Notice the use of getResourceAsStream()
 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic