Java 将 Jar URL 转换为文件
Java Convert Jar URL to File
我正在尝试实现以下目标:我希望能够从给定的 Class
对象中检索它所在的文件夹或文件。这也适用于 class 系统,例如 java.lang.String
(return rt.jar
的位置)。对于 'source' classes,该方法应该 return 根文件夹:
- bin
- com
- test
- Test.class
return bin
文件夹的位置 file(com.test.Test.class)
。到目前为止,这是我的实现:
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);
}
但是,此代码失败,因为 File(URI)
构造函数抛出 IllegalArgumentException
- "URI is not absolute"。我已经尝试使用 newPath
来构造文件,但是对于带有空格的目录结构,这失败了,比如这个:
- Eclipse Workspace
- MyProgram
- bin
- Test.class
这是因为 URL 表示使用 %20
表示空格,文件构造函数无法识别它。
是否有一种有效且可靠的方法来获取 Java class 的(class路径)位置,这对目录结构和 Jar 文件都有效?
请注意,我不需要 class 的确切文件 - 只需要容器!我使用这段代码来定位 rt.jar
和在编译器中使用它们的语言库。
对您的代码稍加修改应该可以在这里工作。您可以尝试以下代码:
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);
}
希望这会有所帮助。
我正在尝试实现以下目标:我希望能够从给定的 Class
对象中检索它所在的文件夹或文件。这也适用于 class 系统,例如 java.lang.String
(return rt.jar
的位置)。对于 'source' classes,该方法应该 return 根文件夹:
- bin
- com
- test
- Test.class
return bin
文件夹的位置 file(com.test.Test.class)
。到目前为止,这是我的实现:
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);
}
但是,此代码失败,因为 File(URI)
构造函数抛出 IllegalArgumentException
- "URI is not absolute"。我已经尝试使用 newPath
来构造文件,但是对于带有空格的目录结构,这失败了,比如这个:
- Eclipse Workspace
- MyProgram
- bin
- Test.class
这是因为 URL 表示使用 %20
表示空格,文件构造函数无法识别它。
是否有一种有效且可靠的方法来获取 Java class 的(class路径)位置,这对目录结构和 Jar 文件都有效?
请注意,我不需要 class 的确切文件 - 只需要容器!我使用这段代码来定位 rt.jar
和在编译器中使用它们的语言库。
对您的代码稍加修改应该可以在这里工作。您可以尝试以下代码:
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);
}
希望这会有所帮助。