File loading by getClass().getResource()

JavaFileLoadEmbedded Resource

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

The best way to access files from resource folder inside a jar is it to use the InputStream via getResourceAsStream. If you still need a the resource as a file instance you can copy the resource as a stream into a temporary file (the temp file will be deleted when the JVM exits):

public static File getResourceAsFile(String resourcePath) {
    try {
        InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
        if (in == null) {
            return null;
        }

        File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
        tempFile.deleteOnExit();

        try (FileOutputStream out = new FileOutputStream(tempFile)) {
            //copy stream
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
        }
        return tempFile;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnjanNView Question on Stackoverflow
Solution 1 - JavaJB NizetView Answer on Stackoverflow
Solution 2 - JavaLukas MasuchView Answer on Stackoverflow