I have followed the way of loading the resource file by using getClass.getResource(path)
. The snippet of code is here :
String url = "Test.properties";
System.out.println("Before printing paths..");
System.out.println("Path2: "+ getClass().getResource(url).getPath());
FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
i_propConfig.load(inputStream);
inputStream.close();
I have configured it in eclipse with the hierarchy (Under source there is a folder called SwingDemo. In SwingDemo there is my java file as well as the resource file)...
- src
- SwingDemo
- CustomDialog.java
- Test.properties
- SwingDemo
When I am running this on eclipse everything is running fine. But as soon as I attempt to run the apps from cmd line null pointer exception is occuring..
Command Line deployment hierarchy is as follows:
Folder : D:\Work\Java Progrms\SwingDemo
Hierarchy:
- SwingDemo
- CustomDialog.java
- Test.properties
First of all I compiled this file inside SwingDemo
folder from command line (javac CustomDialog.java
). Then I move one step back to Java Programs folder (as I mentioned the package inside .java class) and run the apps by using the famous
java SwingDemo.CustomDialog
I used to follow similar steps when I used new FileInputStream("path") previously. After doing this fashion I am getting null pointer exception..
I think getClass().getResource(url)
cannot load file from a specific directory. That's why I put the resource in same directory as that of my java file. It ran fine in Eclipse. But why this is giving error when I run from Command Line.