Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#10 schemas option fails when called from maven

open
nobody
None
5
2012-05-13
2012-05-13
Stuart Turton
No

When called from a wrapping framework, such as Maven function getLoadedFromJar() does not return schemaspy*.jar, but return a JAR file associated with the calling framework.

When the schemas option is used, incorrect java command-lines are generated: they fail because they are referencing the wrong JAR file.

The canonical solution for this seems to be described at http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file.

I tried it and it works (including in directories with spaces).

Discussion

  • Stuart Turton
    Stuart Turton
    2012-06-17

    The original change produced an incorrect path on Windows; this works:-

    "
    public static String getLoadedFromJar() {
    String classpath = System.getProperty("java.class.path");
    //return new StringTokenizer(classpath, File.pathSeparator).nextToken();
    String originalPath = new StringTokenizer(classpath, File.pathSeparator).nextToken();
    String loadedFrom = null;

    /*
    *Get the JAR file path from the URL.
    *We canot simply use location.getPath() because the returned value is invalid in Windows
    *
    *First we try to get a File from the URL, returning the canonical path.
    *If we cannot create a File from the URL we fall back to the original return value
    *
    */
    URL location = Config.class.getProtectionDomain().getCodeSource().getLocation();
    File urlFile = null;
    try
    {
    try
    {
    urlFile= new File(location.toURI());
    }
    catch (URISyntaxException use)
    {
    urlFile= new File(location.getPath());
    }
    loadedFrom=urlFile.getCanonicalPath();
    }
    catch (IOException ioe)
    {
    //Fallback to the original path returned
    loadedFrom=originalPath;
    }
    return loadedFrom ;
    }

    "