Menu

#71 Classloading fails when path contains spaces (Windows only?)

open
nobody
5
2013-09-13
2013-05-13
No

C:\dev\path with spaces> java -jar myproject\build\libs\myproject-0.1-SNAPSHOT-standalone.jar

fails to start with the following root cause exception:

Caused by: java.io.FileNotFoundException: C:\dev\path%20with%20spaces\myproject\build\libs\myproject-0.1-SNAPSHOT-standalone.jar (The system cannot find the path specified)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:214)
at java.util.zip.ZipFile.<init>(ZipFile.java:144)
at java.util.jar.JarFile.<init>(JarFile.java:153)
at java.util.jar.JarFile.<init>(JarFile.java:90)
at com.simontuffs.onejar.OneJarFile.<init>(OneJarFile.java:36)
at com.simontuffs.onejar.OneJarURLConnection.connect(OneJarURLConnection.java:46)
at com.simontuffs.onejar.JarClassLoader$FileURLFactory$1.openConnection(JarClassLoader.java:1105)
at java.net.URL.openConnection(URL.java:971)
at java.net.URL.openStream(URL.java:1037)
at javax.validation.Validation$DefaultValidationProviderResolver.getValidationProviders(Validation.java:320)
... 14 more

The project works normally if the directory "C:\dev\path with spaces\..." is renamed to "C:\dev\pathnospaces\...". It does not seem to matter if the command line path to the JAR is relative or absolute as it gets transformed to the absolute path regardless.

Discussion

  • William Price

    William Price - 2013-05-13

    Full stacktrace (sanitized)

     
  • Jamie

    Jamie - 2013-09-13

    The patch #8 I submitted should fix this problem.
    I tracked it down to the OneJarURLConnection.java class in 0.98 (and possibly 0.97?) which uses:

    //can create problems with spaces etc.
    String codebase = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

    I found a method on Stackoverflow that mentioned if you utilize the URL returned by the Location and then use File's getPath() then it will be properly encoded.
    (reference: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file#answer-12733172)

    eg:

    URL codebaseURL = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    String codebase = null;
    try{
    codebase = new java.io.File(codebaseURL.toURI()).getPath();
    }catch(URISyntaxException ue){
    throw new IOException("Could not convert URL to URI",ue);
    }

     

Log in to post a comment.