gpt4 book ai didi

java - Jar 无法加载包含类文件的文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:34 25 4
gpt4 key购买 nike

我正在使用 Jython 制作一个 Maven 项目。在 Eclipse 中它运行良好,但是当我运行导出的 jar 时,它会引发异常。

这是我在 src/main/resources 中的目录:

src/main/resources:
mylib
file_one.py
file_two.py
pylib
sys.py
sys$py.class
os.py
os$py.class
...

我一直在尝试使用以下代码片段加载包含 src/main/resources 中的类文件的文件夹 pylib:

public class MyClass {

// ...

public static void init() {
// ...
ClassLoader classLoader = Prompt.class.getClassLoader();
addToPath(path, classLoader.getResource("mylib").getPath());
System.out.println("Found mylib");
addToPath(path, classLoader.getResource("pylib").getPath());
System.out.println("Found pylib");
// ...
}

// ...

}

导出到 jar 时我收到此错误:

JAR export finished with warnings. See details for additional information.
Problem writing project/src/main/resources/.DS_Store to JAR: duplicate entry: .DS_Store
duplicate entry: .DS_Store

我测试了多次,但 jar 只包含不包含类文件的文件夹 mylib 。如果我包含 pylib 我收到此错误:

java.lang.NullPointerException
at *project*.Prompt.init(Prompt.java:53)
at *project*.Prompt.start(Prompt.java:76)
at *project*.Main.main(Main.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

Note: I replaced my group id with *package* to hide the identity of my project.

有人知道如何加载类文件吗?

Edit:

  • Corrected code by adding semicolons.
  • Added println expression to make clear where the executed jar failed.
  • Added path information.
  • Changed class and project name.
  • Added export error.
  • Extended runtime error

最佳答案

问题肯定是 JAR 文件是如何创建的或者其中包含什么内容。

我刚刚做了一个最简单的完整样本。适用于 Eclipse、Windows 命令提示符、Git Bash 和 SUSE Linux!

目录结构:

./bin
./bin/classes
./bin/classes/project
./bin/classes/project/Prompt.class
./bin/classes/pylib
./bin/classes/pylib/file.txt
./py.jar
./py.jardesc
./src
./src/java
./src/java/project
./src/java/project/Prompt.java
./src/java/pylib
./src/java/pylib/file.txt

提示.java:

package project;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Prompt {

public static void main(String[] args) {
ClassLoader cl = Prompt.class.getClassLoader();
URL url;
url = cl.getResource("pylib");
System.out.println(url);

url = cl.getResource("pylib/file.txt");
System.out.println(url);
try (InputStream in = url.openStream()) {
int ch;
while ((ch = in.read()) != -1) {
System.out.print((char) ch);
}
System.out.println();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

file.txt(用于测试):

Just a test

由 Eclipses JAR 导出向导使用以下设置创建的 JAR:

  1. 选择要导出的资源:“project”和“pylib”目录
  2. 激活“导出生成的类文件和资源
  3. 选择“添加目录条目”选项
  4. 选择“生成 list 文件”
  5. 输入“project.Prompt”作为主类

JAR 的内容是(第一行是提示符和生成该列表的命令):

$ jar -tvf py.jar
53 Fri Jun 02 07:42:02 UTC 2017 META-INF/MANIFEST.MF
0 Fri Jun 02 07:34:10 UTC 2017 pylib/
11 Fri Jun 02 07:34:20 UTC 2017 pylib/file.txt
0 Fri Jun 02 07:36:34 UTC 2017 project/
1480 Fri Jun 02 07:41:54 UTC 2017 project/Prompt.class

执行者:

  1. Eclipse:运行为 > Java 应用程序
  2. DOS/命令提示符:java -jar py.jar
  3. DOS/命令提示符:java -cp py.jar project.Prompt
  4. Git Bash:java -jar py.jar
  5. Git Bash:java -cp py.jar 项目.提示
  6. SUSE Linux,不同的计算机,空文件夹:java -jar py.jar
  7. SUSE Linux,不同的计算机,空文件夹:java -cp py.jar project.Prompt

它为上述所有情况生成几乎相同的输出,只是路径不同:

Eclipse:

file:/C:/DEVELOP/ECLIPSE/test/bin/classes/pylib
file:/C:/DEVELOP/ECLIPSE/test/bin/classes/pylib/file.txt
Just a test

DOS prompt, Git Bash:

jar:file:/C:/DEVELOP/ECLIPSE/test/py.jar!/pylib
jar:file:/C:/DEVELOP/ECLIPSE/test/py.jar!/pylib/file.txt
Just a test

SUSE Linux:

jar:file:/home/user/pytest/py.jar!/pylib
jar:file:/home/user/pytest/py.jar!/pylib/file.txt
Just a test

在Windows上用Java 8编译并执行;在 SUSE Linux 上使用 Java 9 执行。

关于java - Jar 无法加载包含类文件的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com