3

I have a resource file I need to load at runtime...it is in src/main/resources

I have successfully loaded the file to an inputStream using :

LoadSAC.class.getClassLoader().getResourceAsStream("someFile.txt");

LoadSAC is the class name ...

However, PMD complains about this suggests I use

Thread.currentThread().getContextClassLoader().getResource(...)

I have tried numerous combinations and can never get the file to be located... Any thoughts... I have trolled a number of searches with plenty of suggestions but none seem to work...

Any thoughts ?

Stéphane Millien
  • 3,238
  • 22
  • 36
R M
  • 133
  • 10
  • 1
    Possible duplicate of [PMD rule "Use Proper Class Loader" explaination?](https://stackoverflow.com/questions/34787419/pmd-rule-use-proper-class-loader-explaination) – Johannes Kuhn Sep 02 '18 at 00:32

1 Answers1

0

If someFile.txt is in the same folder than LoadSAC.java, you can do:

InputStream is = LoadSAC.class.getResourceAsStream("someFile.txt");

If someFile.txt is in a subfolder subdir, you can do:

InputStream is = LoadSAC.class.getResourceAsStream("subdir/someFile.txt");

If your method in LoadSAC.java is non-static, you can replace LoadSAC.class.getResourceAsStream... by getClass().getResourceAsStream...

Be careful when compiling with ant or maven, by default, only .java file are copied as .class files in the build directory.

You have to write some rules to include someFile.txt in the final jar.

In your resource directory, you can add a little helper class like this:

import java.io.InputStream;
import java.net.URL;

import javax.activation.DataSource;
import javax.activation.URLDataSource;

public abstract class ResourceHelper {

    public static URL getURL(String name) {
        return ResourceHelper.class.getResource(name);
    }

    public static InputStream getInputStream(String name) {
        return ResourceHelper.class.getResourceAsStream(name);
    }

    public static DataSource getDataSource(String name) {
        return new URLDataSource(getURL(name));
    }
}

In LoadSAC.java just call:

InputStream is = ResourceHelper.getInputStream("someFile.txt");
Arthur
  • 77
  • 6
  • Yes ...the file I need is in the src/main/resources folder ...it is not co-located with the LoadSAC class ...the method is NON STATIC – R M Sep 02 '18 at 00:24
  • 1
    I have completed my response to show you a simple way to find a resource. Hope it's help you. – Arthur Sep 02 '18 at 00:38
  • 1
    You can remove the method `getDataSource` (and the 2 imports `javax.activation.*`), I use this with Javamail api. – Arthur Sep 02 '18 at 00:46
  • Perfect ...Thank you ! – R M Sep 02 '18 at 01:33
  • `"If someFile.txt is in the same folder than LoadSAC.java..."` -- no, not true. This should read, `"If someFile.txt is in the same folder than LoadSAC.class..."`. The class loader looks in the location of the `.class` files and doesn't care a whit for where the `.java` files are located, or any files held with the `.java` files. That's why it is called a "class" loader. – Hovercraft Full Of Eels Sep 02 '18 at 11:37