从 jar 加载文件时出现问题(类路径问题)

发布于 2024-11-30 23:55:54 字数 2470 浏览 1 评论 0原文

当我尝试执行这段代码时: 包 uk.org.infectogenomics.agent;

import uk.org.infectogenomics.Agent.qa.Quasr;

public static void main(String[] args) {
//ommited some code
Runnable step = new Quasr(wf.getStepByPos(1));
}

我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: uk/org/infectogenomic
s/Agent/qa/Quasr
at uk.org.infectogenomics.agent.Agent.main(Agent.java:55)
Caused by: java.lang.ClassNotFoundException: uk.org.infectogenomics.Agent.qa.Quasr
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

这是我的清单文件:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Class-Path: lib/mysql-connector-java-5.1.17-bin.jar lib/aws-java-sdk-1.2.6.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: uk.org.infectogenomics.agent.Agent

这是 jar 文件本身的列表:

META-INF/
META-INF/MANIFEST.MF
uk/
uk/org/
uk/org/infectogenomics/
uk/org/infectogenomics/agent/
uk/org/infectogenomics/agent/Assembly/
uk/org/infectogenomics/agent/hostEl/
uk/org/infectogenomics/agent/qa/
uk/org/infectogenomics/agent/taxonomy/
uk/org/infectogenomics/agent/Agent.class
uk/org/infectogenomics/agent/Assembly/Newbler.class
uk/org/infectogenomics/agent/Assembly/Velvet.class
uk/org/infectogenomics/agent/DBAccessor.class
uk/org/infectogenomics/agent/DateUtils.class
uk/org/infectogenomics/agent/Status$1.class
uk/org/infectogenomics/agent/Status$2.class
uk/org/infectogenomics/agent/Status$3.class
uk/org/infectogenomics/agent/Status$4.class
uk/org/infectogenomics/agent/Status.class
uk/org/infectogenomics/agent/Workflow.class
uk/org/infectogenomics/agent/WorkflowStep.class
uk/org/infectogenomics/agent/agent.properties
uk/org/infectogenomics/agent/hostEl/Fastq2FQone.class
uk/org/infectogenomics/agent/hostEl/RepeatMasker.class
uk/org/infectogenomics/agent/hostEl/SplitFiles.class
uk/org/infectogenomics/agent/qa/Quasr.class
uk/org/infectogenomics/agent/taxonomy/Phymbl.class

那么类路径应该是什么,因为我使用的是 Netbeans,所以我认为这是 IDE 的工作来实际处理这个问题。仅当我将所有内容打包到 jar 中时才会发生这种情况,当我从 IDE 运行代码时,一切都会按预期工作。

When I try to execute this code:
package uk.org.infectogenomics.agent;

import uk.org.infectogenomics.Agent.qa.Quasr;

public static void main(String[] args) {
//ommited some code
Runnable step = new Quasr(wf.getStepByPos(1));
}

I get:

Exception in thread "main" java.lang.NoClassDefFoundError: uk/org/infectogenomic
s/Agent/qa/Quasr
at uk.org.infectogenomics.agent.Agent.main(Agent.java:55)
Caused by: java.lang.ClassNotFoundException: uk.org.infectogenomics.Agent.qa.Quasr
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

Here is my manifest file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Class-Path: lib/mysql-connector-java-5.1.17-bin.jar lib/aws-java-sdk-1.2.6.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: uk.org.infectogenomics.agent.Agent

And here is the listing of the jar file itself:

META-INF/
META-INF/MANIFEST.MF
uk/
uk/org/
uk/org/infectogenomics/
uk/org/infectogenomics/agent/
uk/org/infectogenomics/agent/Assembly/
uk/org/infectogenomics/agent/hostEl/
uk/org/infectogenomics/agent/qa/
uk/org/infectogenomics/agent/taxonomy/
uk/org/infectogenomics/agent/Agent.class
uk/org/infectogenomics/agent/Assembly/Newbler.class
uk/org/infectogenomics/agent/Assembly/Velvet.class
uk/org/infectogenomics/agent/DBAccessor.class
uk/org/infectogenomics/agent/DateUtils.class
uk/org/infectogenomics/agent/Status$1.class
uk/org/infectogenomics/agent/Status$2.class
uk/org/infectogenomics/agent/Status$3.class
uk/org/infectogenomics/agent/Status$4.class
uk/org/infectogenomics/agent/Status.class
uk/org/infectogenomics/agent/Workflow.class
uk/org/infectogenomics/agent/WorkflowStep.class
uk/org/infectogenomics/agent/agent.properties
uk/org/infectogenomics/agent/hostEl/Fastq2FQone.class
uk/org/infectogenomics/agent/hostEl/RepeatMasker.class
uk/org/infectogenomics/agent/hostEl/SplitFiles.class
uk/org/infectogenomics/agent/qa/Quasr.class
uk/org/infectogenomics/agent/taxonomy/Phymbl.class

So what should the classpath be and since I'm using Netbeans I would assume this is the job of the IDE to actually take care of this. This happens only when I package everything inside a jar, when I run the code from the IDE everything works as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-12-07 23:55:54

看看它找不到的类名:

uk/org/infectogenomics/Agent/qa/Quasr

现在看看你的 jar 文件内容:

uk/org/infectogenomics/agent/qa/Quasr.class

大小写有区别。我的猜测是,在某个时刻,您已经将包名称从小写切换了(在我看来,应该是这样),但您使用的是 Windows(大小写为不敏感的文件系统),因此现有的“代理”目录被保留,而不是被擦除并替换为“代理”。

如果你从头开始重建,很可能会解决所有问题。另一方面,我建议更改包名称以遵循正常的 Java 命名约定。

Look at the class name it can't find:

uk/org/infectogenomics/Agent/qa/Quasr

Now look at your jar file contents:

uk/org/infectogenomics/agent/qa/Quasr.class

There's a casing difference. My guess is that at some point you've switched over the package name from lowercase (which it should be, IMO) but that you're on Windows (with case-insensitive file system), so the existing "agent" directory was preserved rather than it being wiped and replaced with "Agent".

If you rebuild from scratch, it may well sort everything out. On the other hand, I'd suggest changing the package name to follow the normal Java naming conventions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文