Using the plus sign (+) in a file path exported to a jar file
The title might not be entirely clear, but I'll try to explain.
I'm trying to access a file with a path like /net/blm50+hmm/synlist/
, which works fine when I don't export to a jar file and just run it from within my IDE (eclipse). However, if I try to run it when I have exported it, I get a null pointer exception. It runs without a problem if I rename the path to not have the plus sign in it. 开发者_StackOverflowCan I escape the plus sign or something like that?
You might ask why I don't just rename the folder, and the reason for this is laziness. There is ALOT of folders to rename and I'd rather avoid it.
I hope You can help,
shalmon
EDIT:
I have a class FileUtils I use for accessing resources in the application jar:
public class FileUtils {
public static InputStream getInputStreamForResource(String resourcePath) throws IOException {
// Try to get the file from the application jar first.
InputStream result = FileUtils.class.getResourceAsStream(resourcePath);
return result;
}
public static Scanner getScanner(String resourcePath) throws IOException {
return new Scanner(getInputStreamForResource(resourcePath));
}
}
If I call getScanner("/net/blm50+hmm/synlist/");
I get the null pointer exception.
The stacktrace is (the call to getScanner happens in NetworkCollection.fromSynapseList):
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
at java.util.Scanner.<init>(Scanner.java:590)
at persistence.FileUtils.getScanner(FileUtils.java:34)
at calculation.NetworkCollection.fromSynapseList(NetworkCollection.java:89)
at processes.JobDispatcher.doInBackground(JobDispatcher.java:136)
at processes.JobDispatcher.doInBackground(JobDispatcher.java:1)
at javax.swing.SwingWorker$1.call(SwingWorker.java:277)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
For that code to work, the jar file must be available to the ClassLoader that loaded FileUtils (or simply, it should be on the classpath).
I once had a problem that could be somehow related: I got an exception when I tried to access a resource inside a jar file. Some code that worked with Java 1.4.2 but not with Java 1.6. At least I found a workaround...
Bet it's not a solution but maybe it's close enough to provide some help on your problem.
Try the following in your machine.
That is, create a file insulating the code that is failing and test it with a small subset of the input ( that is just one file, one with the + and one without it )
#create two folders: code and code+plus containing one file each
$ls -l code code+plus/
code:
total 8
-rw-r--r-- 1 oscarreyes staff 2 Jul 5 18:19 x
code+plus/:
total 8
-rw-r--r-- 1 oscarreyes staff 2 Jul 5 18:18 x
# create a sample with the code in question
$cat FileUtils.java
import java.io.*;
import java.util.*;
public class FileUtils {
public static InputStream getInputStreamForResource(String resourcePath) throws IOException {
// Try to get the file from the application jar first.
InputStream result = FileUtils.class.getResourceAsStream(resourcePath);
return result;
}
public static Scanner getScanner(String resourcePath) throws IOException {
return new Scanner(getInputStreamForResource(resourcePath));
}
public static void main( String [] args ) throws IOException {
Scanner scanner = getScanner( args[0] );
System.out.println( args[0] + " : OK");
}
}
#compile it
$javac FileUtils.java
# package it with the subfodlers
$jar -cmf mf f.jar FileUtils.class code+plus/ code
# execute it without "+"
$java -jar f.jar /code/x
/code/x : OK
#execute it with "+"
$java -jar f.jar /code+plus/x
/code+plus/x : OK
#To get Npe, use a non existing file
$java -jar f.jar /code+plus/y
Let me know if you still having the problem.
I'm pretty sure now your problem is while creating the jar file, you're now including the "+" file you think you're including.
To verify, run jar -tf your.jar
and see if the file with +
is listed.
精彩评论