.
Lifepaths.class.getClass()
's classloader is bootstrap classloader
, so getResourceAsStream
will search $JAVA_HOME only, regardless of user provided classpath
. Obviously, Lifepaths.txt is not there.
Lifepaths.class
's classloader is system classpath classloader
, so getResourceAsStream
will search user-defined classpath
and Lifepaths.txt is there.
When using java.lang.Class#getResourceAsStream(String name)
, a name which doesn't start with '/' will be added with package name
as a prefix. To avoid this use java.lang.ClassLoader#getResourceAsStream
instead.
For example:
ClassLoader loader = Thread.currentThread().getContextClassLoader();String resourceName = "Lifepaths.txt";InputStream resourceStream = loader.getResourceAsStream(resourceName);
What worked for me was to add the file under My Project/Java Resources/src
and then use
this.getClass().getClassLoader().getResourceAsStream("myfile.txt");
I didn't need to explicitly add this file to the path (adding it to /src
does that apparently)
What you really need is a full absolute classPath for the file. So instead of guessing it, try to find out the ROOT and then move the file to a better location base one <.war> file structures...
URL test1 = getClass().getResource("/");URL test2 = getClass().getClassLoader().getResource("/"); URL test3 = getClass().getClassLoader().getResource("../");logger.info(test1.getPath()); logger.info(test2.getPath());logger.info(test3.getPath());
Make sure your resource directory (e.g. "src") is in your classpath (make sure it's a source directory in your build path in eclipse).
Make sure clazz is loaded from the main classloader.
Then, to load src/initialization/Lifepaths.txt, use
clazz.getResourceAsStream("/initialization/Lifepaths.txt");
Why:clazz.getResourcesAsStream(foo)
looks up foo from within the classpath of clazz, relative to the directory clazz lives in. The leading "/" makes it load from the root of any directory in the classpath of clazz.
Unless you're in a container of some kind, like Tomcat, or are doing something with ClassLoaders directly, you can just treat your eclipse/command line classpath as the only classloader classpath.
if you are using Maven make sure your packing is 'jar' not 'pom'.
<packaging>jar</packaging>
Don't know if of help, but in my case I had my resource in the /src/ folder and was getting this error.I then moved the picture to the bin folder and it fixed the issue.
If you are using IDEA, make sure to mark your resources folder as 'Resources' so that the path can be recognized by IDE correctly.
Let's say you have a file named 'book.json' in 'resources/', then to use the resources in it, you can do it like this
InputStream input = Main.class.getResourceAsStream("/book.json");
I know there are different answers here. Mine worked like this.Added the file in the resources directory.And in the executable code I mentioned the path for that file with "/" as prefix within the quotes of the filename.
InputStream resourceStream = loader.getResourceAsStream("/LifePaths.txt");
What worked for me is I placed the file under
src/main/java/myfile.log
and
InputStream is = getClass().getClassLoader().getResourceAsStream("myfile.log");if (is == null) {throw new FileNotFoundException("Log file not provided");}
My caller class was in src/main/...
What helped me to load resource from src/test/resources/folder/file.properties
`properties.load(getClass().getClassLoader().getResourceAsStream("folder/file.properties"));`
https://howtodoinjava.com/java/io/read-file-from-resources-folder/
Java 11
jdk >=9 需要在module-info模块中把非根目录/非共父目录的包给它open了。
jdk >=9 need open resource dir in module-info.java , such as :
If you need read conf/config.json , you need two step :
// in module-info.javamodule your.mod.name {open conf;}// then in java codegetClassLoader().getResourceAsStream("conf/config.json");
Else if you need read other in root , it's only :
getClassLoader().getResourceAsStream("openapi.yaml");
you can see {@link java.lang.ClassLoader#getResourceAsStream(String)} known why ~
While not intended to be an answer, I'm presenting this suggestion as an answer so I can get the screenshot in.
For those of you developing on a Windows platform, I highly recommend the Microsoft utility ProcMon, or Process Monitor.
Process Monitor is one utility in a suite of utilities Microsoft gives away (you don't even need to be signed in to download). They are all available at sysinternals.com, the original host that Microsoft acquired and just kept.
Process Monitor can monitor a ton of events that are associated with any running process. If you specify the File Open event and provide the file name (or part of the file name), it will log which process attempted to open the file and it will show every directory searched. This can be useful when you don't know where your running code is looking for the file you specified in your source/config.Here's an example:
Here I'm looking for (and not finding) a schema file (XSD) so that the code can use it to validate some user supplied XML.
Lifepaths.class.getClass().getResourceAsStream("Lifepaths.txt"));
Problem with me was that resource was not in src folder.
Coming late to the party. Depending on the JDK version you are using (maybe it is time to review the StackOverflow 'question already answered'?) JPMS can impact on the resources you are able to load (or not).
Packaging my code as module I had to do that:
Thread.currentThread().getClass().getModule().getResourceAsStream("resourceName");
And then it worked
What happened to me was an intelliJ (2023.1) issue. I was trying to get a .json file from my /resources folder and I was getting null
. The problem was the .json pattern was not in Settings > Build, Execution, Deployment > Compiler > Resource Patterns.
After I added it, all worked fine (without the initial "/", by the way).
See: Java - class.getResource returns null
Please remove
../src/main/resourcesor include file you are trying to read
In pom.xml manage or remove../src/main/resources
@Emracool... I'd suggest you an alternative. Since you seem to be trying to load a *.txt file. Better to use FileInputStream()
rather then this annoying getClass().getClassLoader().getResourceAsStream()
or getClass().getResourceAsStream()
. At least your code will execute properly.