0

I have created an app in which all labels are fetched from a file called text.properties, it is running fine in my IDE, but when i run the jar file using command prompt, the error below is raised.

Oct 04, 2013 9:28:14 AM Main.LoginFrame <init>
SEVERE: null
java.io.FileNotFoundException: text.properties (The system cannot find the file
specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at GraphicUserInterface.LoginFrameGUI.initializingLoginElements(LoginFra
meGUI.java:30)
    at Main.LoginFrame.<init>(LoginFrame.java:72)
    at Main.Main.main(Main.java:41)

Exception in thread "main" java.lang.NullPointerException
    at Main.LoginFrame.<init>(LoginFrame.java:81)
    at Main.Main.main(Main.java:41)

The sample piece of code that calls this properties file is as given below

Properties prop=new Properties();
prop.load(new FileInputStream("text.properties"));
logFrame.usernamelbl = new JLabel(prop.getProperty("lusernamelbl"));

The same approach has worked for me in the past. I have copied the properties type into the netbeans project folder. Please help me to find a solution for this.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2655318
  • 67
  • 4
  • 14
  • 1
    The `text.properties` file needs to be in the same execution context as the application (ie it's start directory). I would add a `System.out.println(new File(".").getCanonicalPath());` before you try and load the file to determine the working directory context – MadProgrammer Oct 04 '13 at 04:21
  • 3
    did you try something like `LoginFrame.class.getClassLoader().getResourceAsStream("text.properties")`?, this way you only have to be sure the file is included on the distribution jar, and here it is assuming it will be found on the root directory of the project (src) – OscarG Oct 04 '13 at 04:21
  • By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Oct 04 '13 at 05:30

2 Answers2

1

You can to either set the classpath in your command prompt or give the absolute path of your properties file for it to work.

prop.load(new FileInputStream("full/path/where/the/file/is/text.properties"));
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 1
    Setting the classpath won't help unless you use .getClassLoader().getResourceAsStream("text.properties") – Jason Oct 04 '13 at 04:27
  • I gave the full path and it worked, will it have any problem when the application is run on another system. – user2655318 Oct 04 '13 at 04:41
  • Yeah, it'll definitely be a problem. You can instead set the `classpath` and try loading it as suggested by Jason and OscasG. – Rahul Oct 04 '13 at 04:46
0

I solved the problem. You just have to copy your properties file to the location where the jar file is present.

user2655318
  • 67
  • 4
  • 14