2

我的代码在 JAR 文件中运行,我需要获取该文件的完整路径。例如,我的 JAR 名为 example.jar,位于 D:\example\ 所以我需要通过该 jar 中的一些代码获取“D:\example\example.jar”。我已经尝试了很多方法来获得该路径,但没有一个能正常工作。

其中之一是

getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()

许多人说这对他们有用,但它为我返回“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 的东西并用它替换其他的东西,它说罐子坏了。我认为这就是它不起作用的原因。有人知道这是什么意思吗?

PS:我正在使用 BAT 文件运行我的 JAR。

4

5 回答 5

11

我也偶然发现了这个问题,并将我的调查留给那些将来问自己rsrc意味着什么的人。

我正在使用 Eclipse Mars 1 并尝试将我的项目导出为可运行的 JAR。在那里我可以选择库处理并在以下之间做出决定:

  1. 将所需的库提取到生成的 JAR 中
  2. 将所需的库打包到生成的 JAR 中
  3. 将所需的库复制到生成的 JAR 旁边的子文件夹中

要测试的线路是

System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation());

JAR 文件的名称是 MyJar.jar(将放在桌面上),项目的名称和文件夹是 MyProject。

结果:

  1. file:/C:/Users/admin/Desktop/MyJar.jar
  2. rsrc:./
  3. file:/C:/Users/admin/Desktop/MyJar.jar
  4. <表示在 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;
    }

}
于 2016-11-18T15:17:21.940 回答
0
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();
            }
        }
    }

}
于 2016-06-20T16:26:41.867 回答
0

这就是我今天遇到的确切问题。我终于找到了这个:使用eclipse打包时获取jar文件位置将所需的库打包到生成的JAR中,可以使用这个(在<>中插入调用类的名称:

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));
于 2021-12-31T13:00:25.483 回答
0

根据您的评论,您真正的问题似乎是如何从应用程序 .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 文件的内容。

于 2016-06-20T01:10:57.457 回答
0

我刚刚创建了一个新的工作区并将每个项目都移入其中,现在一切正常,我认为这是一个错误或什么的......谢谢大家的帮助!

于 2016-06-23T20:41:43.003 回答