Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File loading by getClass().getResource()

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)...

  1. src
    • SwingDemo
      1. CustomDialog.java
      2. Test.properties

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:

  1. 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.

like image 279
AnjanN Avatar asked Dec 30 '12 07:12

AnjanN


People also ask

How does getResource work in Java?

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.

How do you specify a resource path in Java?

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.


1 Answers

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:

  • .java files are compiled into .class files that go the the bin directory (by default)
  • other files are copied to the bin directory (respecting the package/folder hirearchy)

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.

like image 60
JB Nizet Avatar answered Oct 02 '22 14:10

JB Nizet