java 即使jar文件在工作目录中也找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7469711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:06:55  来源:igfitidea点击:

Cannot find class even when jar file is in working directory

javaclasspathaix

提问by Michael Sandler

I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jarbelow. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.

我正在努力让我的 Java 程序在 AIX 上运行。我在 Windows 上使用 Eclipse 创建了一个可运行的 Jar 文件,jRams.jar如下所示。我不断收到未找到类的错误,直到最后我将所有外部库都放在同一目录中。

$ ls
JAXB2_20081030.jar
JAXB2_20110601.jar
activation.jar
asjava.jar
commons-beanutils-1.8.3.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.5.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-2.1.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-adapters-1.1.1.jar
commons-logging-api-1.1.1.jar
jRams.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxb1-impl.jar
jremote.jar
jsr173_1.0_api.jar
log4j-1.2.16.jar
netty-3.2.4.Final.jar
$

Still, I get the class not found error.

尽管如此,我还是收到了未找到类的错误。

$ java -jar jRams.jar
The java class is not found:  com.jbase.jremote.JRemoteException

jremote.jardefinitely contains JRemoteException. Why isn't this working?

jremote.jar肯定包含JRemoteException。为什么这不起作用?

UPDATE

更新

Thank you for your straight-to-the-point answers. I now understand the nature of a java application and a manifest file far better.

感谢您的直截了当的回答。我现在更好地理解了 Java 应用程序和清单文件的性质。

Turns out my ftp client was transferring in ASCII mode and not Binary, so the jar files were corrupt. I have learned a great deal, nonetheless.

结果我的 ftp 客户端以 ASCII 模式传输而不是二进制模式,所以 jar 文件已损坏。尽管如此,我还是学到了很多东西。

回答by aioobe

When using the -jaroption, you need to specify which jar-files should be on your class path in the manifest file. Just having the required jar files in the same directory won't do it.

使用该-jar选项时,您需要指定清单文件中的类路径上应包含哪些 jar文件。仅将所需的 jar 文件放在同一目录中是行不通的。

Add a line in your manifest that says:

在清单中添加一行内容:

Class-Path: JAXB2_20081030.jar:JAXB2_20110601.jar:....:netty-3.2.4.Final.jar

or skip the -jaroption and launch using

或跳过该-jar选项并使用

java -cp JAXB2_20081030.jar:....:netty-3.2.4.Final.jar:jRams.jar pkg.JRamsMain

and it should work fine.

它应该可以正常工作。

(Note that on *nix systems, as opposed to Windows machines, the jar files in the class paths should be separated using :instead of ;.)

(请注意,在 *nix 系统上,与 Windows 机器相反,类路径中的 jar 文件应使用:代替来分隔;。)

Further reading:

进一步阅读:

回答by duffymo

You need to add all those JARs to the runtime CLASSPATHby adding the -classpathparameter. AIX requires you to separate the JARs using :

您需要CLASSPATH通过添加-classpath参数将所有这些 JAR 添加到运行时。AIX 要求您使用:

回答by Bitmap

You will have to specify the full path(if libraries not in the same directory as jRams) or just the names of the jar file in a manifest file (If all dependency jars are in the same folder). Alternative specify the path to all the dependent jarsusing -cpargument.

您必须指定完整路径(如果库与 不在同一目录中jRams)或仅在清单文件中指定 jar 文件的名称(如果所有依赖 jar 都在同一文件夹中)。替代方法使用参数指定所有依赖jar的路径-cp

Example(This assume every dependency is in the same directory you are executing java command from):

示例假设每个依赖项都在您从中执行 java 命令的同一目录中):

java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java

java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java

Where package_to_classis example: com.myproj.example.

package_to_class示例在哪里:com.myproj.example

EDITED.

已编辑