[java] 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.

This question is related to java file load embedded-resource

The answer is


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;
    }
}

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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to load

jQuery load first 3 elements, click "load more" to display next 5 elements Javascript: Load an Image from url and display Selenium wait until document is ready Calculating Page Load Time In JavaScript File loading by getClass().getResource() How do I load an url in iframe with Jquery Can I get JSON to load into an OrderedDict? changing source on html5 video tag Load content of a div on another page JavaScript load a page on button click

Examples related to embedded-resource

Reading a resource file from within jar File loading by getClass().getResource() How do I add an image to a JButton How to read embedded resource text file Could not find any resources appropriate for the specified culture or the neutral culture Loop through all the resources in a .resx file Storing WPF Image Resources