Answer the question
In order to leave comments, you need to log in
How to read file from classpath?
A big hello to the reader!
I need help getting file from classpath.
The file is specified in the -cp option when running the jar via the console.
I run the jar using:
java -cp myjar.jar:dir1/dir2/myfile.txt com.company.Main
Exception in thread "main" java.lang.NullPointerException
at com.company.Main.main(Main.java:11)
package com.company;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
ClassLoader classLoader = Main.class.getClassLoader();
InputStream resource = classLoader.getResourceAsStream("dir1/dir2/myfile.txt");
System.out.println(resource.toString());
}
}
-- сom
---- company
------ Main.java
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question