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)...
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:
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.
The getResource method finds a resource with the specified name. It returns a URL to the resource or null if it does not find the resource. Calling java. net.
The simplest approach uses an instance of the java. io. File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file.
getClass().getResource()
uses the class loader to load the resource. This means that the resource must be in the classpath to be loaded.
When doing it with Eclipse, everything you put in the source folder is "compiled" by Eclipse:
When launching the program with Eclipse, the bin directory is thus in the classpath, and since it contains the Test.properties file, this file can be loaded by the class loader, using getResource()
or getResourceAsStream()
.
If it doesn't work from the command line, it's thus because the file is not in the classpath.
Note that you should NOT do
FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
to load a resource. Because that can work only if the file is loaded from the file system. If you package your app into a jar file, or if you load the classes over a network, it won't work. To get an InputStream, just use
getClass().getResourceAsStream("Test.properties")
And finally, as the documentation indicates,
Foo.class.getResourceAsStream("Test.properties")
will load a Test.properties file located in the same package as the class Foo.
Foo.class.getResourceAsStream("/com/foo/bar/Test.properties")
will load a Test.properties file located in the package com.foo.bar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With