问题
"it work fine in eclipse but when i create jar and run it will give me this exception.This is non web spring boot application i am using which i supposed to run as standalone jar"
java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source) ~[na:1.8.0_171]
at sun.misc.URLClassPath.findResource(Unknown Source) ~[na:1.8.0_171]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_171]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_171]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_171]
at java.net.URLClassLoader.findResource(Unknown Source) ~[na:1.8.0_171]
at org.springframework.boot.loader.LaunchedURLClassLoader.findResource(LaunchedURLClassLoader.java:58) ~[extension-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at java.lang.ClassLoader.getResource(Unknown Source) ~[na:1.8.0_171]
at java.net.URLClassLoader.getResourceAsStream(Unknown Source) ~[na:1.8.0_171]
回答1:
It might be a bit late but I encounted the same error recently on Windows and fixed it with a dirtly hack. I hope future people looking for the same question will find this useful for their problem.
First of all, eclipse "run" your Spring Boot app in a different way. It does not do java -jar foo.jar
but reference all classes and libraries directly i.e. java -classpath xxx/target/classes:a.jar:b.jar...
. So soetimes running in eclipse will behave differently.
I suppose you are running into the same situtation as I did which is you have a Spring Boot app and you want to package it as executable jar and you are referencing external dependencies and could not fix what went wrong inside those external dependencies.
What I found out was that somewhere inside the external library I was using is generating a temporary file and reading it using Java's ClassLoader
but Spring Boot's LaunchedURLClassLoader
could not handle Windows's "x:\" syntax
In my case, it was
URL findResource(name): C:\foo\bar.xml
So the hack is to build your own LaunchedURLClassLoader
and replace it in the executable jar. If you unpack the excutable jar, you will find LaunchedURLClassLoader.class
under org\springframework\boot\loader
. The idea is to reaplce C:\foo\bar.xml
to file:///C:/foo/bar.xml
so Java ClassLoader will know how to deal with the file.
...
package org.springframework.boot.loader;
...
public class LaunchedURLClassLoader extends URLClassLoader {
...
@Override
public URL findResource(String name) {
// This is where I added the dirty hack
if (name.contains(":\\") && !name.startsWith("file:///")) {
System.out.println("URL findResource(name): " + name); // just for you to debug
name = "file:///" + name.replace("\\", "/");
System.out.println("URL findResource(name): Reaplced to " + name); // just for you to debug
}
Handler.setUseFastConnectionExceptions(true);
try {
return super.findResource(name);
}
finally {
Handler.setUseFastConnectionExceptions(false);
}
}
....
}
来源:https://stackoverflow.com/questions/51203893/java-lang-illegalargumentexception-name