问题
I'm probably missing something stupid but still.
I created a jar file with the following inner structure:
- folder1
- folder2
- META-INF
- resources
where folder1 and folder2 hold .class files and resources has txt and png files.
In my code i have the following line
BufferedReader reader = new BufferedReader(new InputStreamReader(
ClassLoader.getSystemResourceAsStream(txtFile)));
where txtFile is a string holding a file name that is located within the resource folder. (directly in the folder).
The problem is when i try to run the program from the command line.
This is the command i run.
java -cp externalJar.jar;myJar.jar;resources com.my.package.MyMainClass
The program runs and fine unit i get to the above code line where i get a null pointer exception:
java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at com.my.package.MyMainClass.generateSomething(Unknown Source)
at com.my.package.MyMainClass.main
which i guess means that there is something wrong with my classpath - but i can't figure what...
EDIT
I'm using ant to compile and package my program to an executable jar.
This is my part of my build.xml:
<target name="jar" depends="compile">
<jar destfile="${executable.jar}" basedir="${bin}">
<zipfileset dir="${src}\resources" prefix="resources" />
<manifest>
<attribute name="Main-Class" value="MyMainClass" />
<attribute name="Class-Path" value="externalJar.jar" />
</manifest>
</jar>
</target>
EDIT - 2
Because it's supposed to be an executable jar the value of the txtFile variable is hard coded (unless given as an argument) to be the file name inside the resources directory. So eventually, i would like to run this command:java -jar myJar.jar
Unfortunately - this also results in the same manner (NullPointerException on the same line)
EDIT - 3 (FOUND UN-ELEGANT SOLUTION)
I found that if i concatenate to all the paths of files that are read by a class loader the prefix "resources/" then it works fine - but i find this to be un-elegant. Is there no way to add the resources folder to the classpath?
回答1:
First, if you only plan to access files that are within the same jar as your MyMainclass, you can call getResource() or getResourceAsStream() on its class loader rather than getSystemResource() / getSystemResourceAsStream().
public class MyMainClass {
public static void main(String[] args) throws Exception {
InputStream inputStream = MyMainClass.class.getResourceAsStream(args[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
}
}
Second, you should pass the path to the file rather than passing the resources folder on the classpath:
java -cp externalJar.jar;myJar.jar com.my.package.MyMainClass "/resources/myTextFileInUtf8.txt"
Please also note that it is (almost) always a good idea to specify the encoding when creating an InputStreamReader, otherwise you rely on the JVM default encoding.
回答2:
Well one thing is clear, txtFile variable would be a null or non existing path, as the NPE is originating from there.
OR
Another chance of problem would be the invocation of ClassLoader.getSystemResourceAsStream(txtFile)
Inorder to make the proper loader access your resource, change it to
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getSystemResourceAsStream(txtFile);
and use the stream.
来源:https://stackoverflow.com/questions/24018149/run-jar-with-classpath-parameters