获取 运行 JAR 文件的路径 returns "rsrc:./"
Getting the path of a running JAR file returns "rsrc:./"
我的代码在 JAR 文件中运行,我需要获取该文件的完整路径。
例如,我的 JAR 名为 example.jar,位于 D:\example\
所以我需要通过那个罐子里的一些代码来获得 "D:\example\example.jar" 。
我已经尝试了很多方法来获取该路径,但是 none 其中的方法工作正常。
其中之一是
getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()
很多人说这对他们有用,但 returns "rsrc:./" 对我来说。
之后我进行了搜索,发现我的 MANIFEST.MF 包含以下内容:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: Example
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
我不知道那是什么意思,但是如果我删除那个 Rsrc 东西并用它替换其他东西,它说 jar 坏了。我认为这就是它不起作用的原因。有人知道这是什么意思吗?
PS:我 运行 我的 JAR 使用 BAT 文件。
根据您的评论,您真正的问题似乎是如何从应用程序 .jar 中复制文件。你可以这样做:
String jarEntry = "/files/data.txt";
Path destination = Paths.get(
System.getProperty("user.home"), "Downloads", "data.txt");
try (InputStream stream = getClass().getResourceAsStream(jarEntry)) {
Files.copy(stream, destination);
}
Class.getResource 和 Class.getResourceAsStream 从类路径中读取数据,通常作为类路径上的 .jar 文件中的条目,例如您自己的应用程序的 .jar 文件。
类路径中嵌入的文件通常称为应用程序资源或简称为“资源”。在所有平台上,资源始终使用正斜杠 (/
) 作为目录分隔符指定,甚至 Windows.
如果您不确定应该将什么字符串传递给 getResource 或 getResourceAsStream,请检查 .jar 文件的内容。
package com.example;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class HomeJar {
public static void main(String[] args) throws IOException {
URL u = HomeJar.class.getProtectionDomain().getCodeSource().getLocation();
File f = new File(u.getPath().toString());
if (!f.isFile()) {
throw new RuntimeException("'" + u + "' isn't a jar");
}
try (JarInputStream jis = new JarInputStream(new BufferedInputStream(new FileInputStream(f)))) {
JarEntry je = jis.getNextJarEntry();
while (je != null) {
System.out.println(je.getName());
je = jis.getNextJarEntry();
}
}
}
}
我刚刚创建了一个新的工作区并将每个项目移入其中,现在一切正常,我认为这是一个错误或其他问题...谢谢大家的帮助!
我也偶然发现了这个问题,并把我的调查留给以后问自己 rsrc
是什么意思的人。
我正在使用 Eclipse Mars 1 并尝试将我的项目导出为可运行的 JAR。在那里我可以选择库处理并在以下两者之间做出决定:
- 将所需的库提取到生成的 JAR
- 将所需的库打包到生成的 JAR 文件中
- 将所需的库复制到生成的 JAR 旁边的子文件夹中
要测试的线路是
System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation());
JAR 文件的名称是MyJar.jar(将放在桌面上),项目的名称和文件夹是MyProject。
结果:
file:/C:/Users/admin/Desktop/MyJar.jar
rsrc:./
file:/C:/Users/admin/Desktop/MyJar.jar
- <在 Eclipse 中表示 运行>
file:/C:/Development/workspace/MyProject/target/classes/
我为此写了一个方便的方法:
public class SystemUtils {
/**
* Let no one instanciate this class.
*/
private SystemUtils() {}
/**
* If the current JVM was started from within a JAR file.
* @return <code>Boolean.TRUE</code> if it is, <code>Boolean.FALSE</code> if it is not, <code>null</code> if unknown.
*/
public static Boolean executedFromWithinJar() {
Boolean withinJar = null;
try {
String location = SystemUtils.class.getProtectionDomain().getCodeSource().getLocation().toString();
if (location.startsWith("rsrc:")
|| location.endsWith(".jar") && !new File(location.substring(location.indexOf(':') + 1)).isDirectory())
withinJar = Boolean.TRUE;
else
withinJar = Boolean.FALSE;
}
catch (Exception ex) {/* value is still null */}
return withinJar;
}
}
这正是我今天遇到的问题。我终于找到了这个:
要在使用 eclipses 打包时获取 jar 文件位置 将所需的库打包到生成的 JAR 中,可以使用这个(在 <> 中插入调用名称 class:
var thisClassesResourcePath = <CLASSNAME>.class.getName().replace('.', '/') + ".class";
var resource = ClassLoader.getSystemResource(thisClassesResourcePath);
var path = resource.getPath();
var jarUrl = new URL(path.substring(0, path.lastIndexOf("jar!") + 3));
我的代码在 JAR 文件中运行,我需要获取该文件的完整路径。 例如,我的 JAR 名为 example.jar,位于 D:\example\ 所以我需要通过那个罐子里的一些代码来获得 "D:\example\example.jar" 。 我已经尝试了很多方法来获取该路径,但是 none 其中的方法工作正常。
其中之一是
getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()
很多人说这对他们有用,但 returns "rsrc:./" 对我来说。
之后我进行了搜索,发现我的 MANIFEST.MF 包含以下内容:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: Example
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
我不知道那是什么意思,但是如果我删除那个 Rsrc 东西并用它替换其他东西,它说 jar 坏了。我认为这就是它不起作用的原因。有人知道这是什么意思吗?
PS:我 运行 我的 JAR 使用 BAT 文件。
根据您的评论,您真正的问题似乎是如何从应用程序 .jar 中复制文件。你可以这样做:
String jarEntry = "/files/data.txt";
Path destination = Paths.get(
System.getProperty("user.home"), "Downloads", "data.txt");
try (InputStream stream = getClass().getResourceAsStream(jarEntry)) {
Files.copy(stream, destination);
}
Class.getResource 和 Class.getResourceAsStream 从类路径中读取数据,通常作为类路径上的 .jar 文件中的条目,例如您自己的应用程序的 .jar 文件。
类路径中嵌入的文件通常称为应用程序资源或简称为“资源”。在所有平台上,资源始终使用正斜杠 (/
) 作为目录分隔符指定,甚至 Windows.
如果您不确定应该将什么字符串传递给 getResource 或 getResourceAsStream,请检查 .jar 文件的内容。
package com.example;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class HomeJar {
public static void main(String[] args) throws IOException {
URL u = HomeJar.class.getProtectionDomain().getCodeSource().getLocation();
File f = new File(u.getPath().toString());
if (!f.isFile()) {
throw new RuntimeException("'" + u + "' isn't a jar");
}
try (JarInputStream jis = new JarInputStream(new BufferedInputStream(new FileInputStream(f)))) {
JarEntry je = jis.getNextJarEntry();
while (je != null) {
System.out.println(je.getName());
je = jis.getNextJarEntry();
}
}
}
}
我刚刚创建了一个新的工作区并将每个项目移入其中,现在一切正常,我认为这是一个错误或其他问题...谢谢大家的帮助!
我也偶然发现了这个问题,并把我的调查留给以后问自己 rsrc
是什么意思的人。
我正在使用 Eclipse Mars 1 并尝试将我的项目导出为可运行的 JAR。在那里我可以选择库处理并在以下两者之间做出决定:
- 将所需的库提取到生成的 JAR
- 将所需的库打包到生成的 JAR 文件中
- 将所需的库复制到生成的 JAR 旁边的子文件夹中
要测试的线路是
System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation());
JAR 文件的名称是MyJar.jar(将放在桌面上),项目的名称和文件夹是MyProject。
结果:
file:/C:/Users/admin/Desktop/MyJar.jar
rsrc:./
file:/C:/Users/admin/Desktop/MyJar.jar
- <在 Eclipse 中表示 运行>
file:/C:/Development/workspace/MyProject/target/classes/
我为此写了一个方便的方法:
public class SystemUtils {
/**
* Let no one instanciate this class.
*/
private SystemUtils() {}
/**
* If the current JVM was started from within a JAR file.
* @return <code>Boolean.TRUE</code> if it is, <code>Boolean.FALSE</code> if it is not, <code>null</code> if unknown.
*/
public static Boolean executedFromWithinJar() {
Boolean withinJar = null;
try {
String location = SystemUtils.class.getProtectionDomain().getCodeSource().getLocation().toString();
if (location.startsWith("rsrc:")
|| location.endsWith(".jar") && !new File(location.substring(location.indexOf(':') + 1)).isDirectory())
withinJar = Boolean.TRUE;
else
withinJar = Boolean.FALSE;
}
catch (Exception ex) {/* value is still null */}
return withinJar;
}
}
这正是我今天遇到的问题。我终于找到了这个: 要在使用 eclipses 打包时获取 jar 文件位置 将所需的库打包到生成的 JAR 中,可以使用这个(在 <> 中插入调用名称 class:
var thisClassesResourcePath = <CLASSNAME>.class.getName().replace('.', '/') + ".class";
var resource = ClassLoader.getSystemResource(thisClassesResourcePath);
var path = resource.getPath();
var jarUrl = new URL(path.substring(0, path.lastIndexOf("jar!") + 3));