简体   繁体   English

Java将Jar URL转换为文件

[英]Java Convert Jar URL to File

I am trying to achieve the following: From a given Class object, I want to be able to retrieve the folder or file in which it is located. 我正在尝试实现以下目标:从给定的Class对象,我希望能够检索它所在的文件夹或文件。 This should also work for System classes like java.lang.String (which would return the location of rt.jar ). 这也适用于java.lang.String类的系统类(它将返回rt.jar的位置)。 For 'source' classes, the method should return the root folder: 对于“源”类,该方法应返回根文件夹:

- bin
  - com
    - test
      - Test.class

would return the location of the bin folder for file(com.test.Test.class) . 将返回file(com.test.Test.class)bin文件夹的位置。 This is my implementation so far: 到目前为止,这是我的实现:

public static File getFileLocation(Class<?> klass)
{
    String classLocation = '/' + klass.getName().replace('.', '/') + ".class";
    URL url = klass.getResource(classLocation);
    String path = url.getPath();
    int index = path.lastIndexOf(classLocation);
    if (index < 0)
    {
        return null;
    }

    // Jar Handling
    if (path.charAt(index - 1) == '!')
    {
        index--;
    }
    else
    {
        index++;
    }

    int index1 = path.lastIndexOf(':', index);
    String newPath = path.substring(index1 + 1, index);

    System.out.println(url.toExternalForm());
    URI uri = URI.create(newPath).normalize();

    return new File(uri);
}

However, this code fails because the File(URI) constructor throws an IllegalArgumentException - "URI is not absolute". 但是,此代码失败,因为File(URI)构造函数抛出IllegalArgumentException “ URI不是绝对的”。 I already tried to use the newPath to construct the file, but this failed for directory structures with spaces, like this one: 我已经尝试过使用newPath来构造文件,但是对于带有空格的目录结构,这是失败的:

- Eclipse Workspace
  - MyProgram
    - bin
      - Test.class

This is due to the fact that the URL representation uses %20 to denote a whitespace, which is not recognized by the file constructor. 这是由于URL表示使用%20表示空格,因此文件构造函数无法识别该空格。

Is there an efficient and reliable way to get the (classpath) location of a Java class, which works for both directory structures and Jar files? 是否有一种有效且可靠的方法来获取Java类的(类路径)位置,该方法对目录结构和Jar文件均有效?

Note that I don't need the exact file of the exact class - only the container! 请注意,我不需要确切类的确切文件,只需要容器! I use this code to locate rt.jar and the language library for using them in a compiler. 我使用此代码查找rt.jar和语言库,以在编译器中使用它们。

Slight modification in your code should work here. 您的代码中的轻微修改应该在这里起作用。 You can try below code: 您可以尝试以下代码:

public static File getFileLocation(Class<?> klass)
{
    String classLocation = '/' + klass.getName().replace('.', '/') + ".class";
    URL url = klass.getResource(classLocation);
    String path = url.getPath();
    int index = path.lastIndexOf(classLocation);
    if (index < 0)
    {
        return null;
    }

    String fileCol = "file:";
    //add "file:" for local files
    if (path.indexOf(fileCol) == -1)
    {
        path = fileCol + path;
        index+=fileCol.length();
    }

    // Jar Handling
    if (path.charAt(index - 1) == '!')
    {
        index--;
    }
    else
    {
        index++;
    }

    String newPath = path.substring(0, index);

    System.out.println(url.toExternalForm());
    URI uri = URI.create(newPath).normalize();

    return new File(uri);
}

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM