即使 jar 文件位于工作目录中也无法找到类
我正在努力让我的 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
$
尽管如此,我还是收到了类未找到错误。
$ java -jar jRams.jar
The java class is not found: com.jbase.jremote.JRemoteException
jremote.jar
肯定包含 JRemoteException。为什么这不起作用?
更新
感谢您直截了当的回答。我现在更好地理解了 java 应用程序和清单文件的本质。
结果我的 ftp 客户端以 ASCII 模式而不是二进制模式传输,因此 jar 文件已损坏。尽管如此,我还是学到了很多东西。
I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar
below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.
$ 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.jar
definitely contains JRemoteException. Why isn't this working?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
-jar
选项时,您需要指定哪些 jar 文件应位于 清单文件。仅将所需的 jar 文件放在同一目录中是不行的。在清单中添加一行内容:
或跳过
-jar
选项并启动 using,它应该可以正常工作。
(请注意,在 *nix 系统上,与 Windows 机器相反,类路径中的 jar 文件应使用
:
而不是;
分隔。)进一步阅读:
When using the
-jar
option, 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.Add a line in your manifest that says:
or skip the
-jar
option and launch usingand 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;
.)Further reading:
您需要通过添加
-classpath
参数将所有这些 JAR 添加到运行时CLASSPATH
中。 AIX 要求您使用:
分隔 JARYou need to add all those JARs to the runtime
CLASSPATH
by adding the-classpath
parameter. AIX requires you to separate the JARs using:
您必须指定完整路径(如果库与 jRams 不在同一目录中)或仅指定清单文件中 jar 文件的名称(如果所有依赖项 jar 都在同一文件夹中)。或者使用
-cp
参数指定所有依赖 jar 的路径。示例(假设每个依赖项都位于您执行 java 命令的同一目录中):
java -cp commons-collections-3.2.1.jar; jaxb-impl.jar;等等..;jRams.jar package_to_class.MyMainClass.java
其中
package_to_class
是示例:com.myproj.example
。已编辑。
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 jars using-cp
argument.Example (This assume every dependency is in the same directory you are executing java command from):
java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java
Where
package_to_class
is example:com.myproj.example
.EDITED.
按照以下步骤将“Class-Path”添加到现有 jar 文件 -
使用以下条目创建“newmanifest”文件
类路径:additional/jars
更新现有的 jar 文件,例如“classes.jar”
jar --update --manifest=newmanifest --file classes.jar
膨胀 jar 文件
jar -xvf 类.jar
创建:META-INF/
膨胀:META-INF/MANIFEST.MF
验证“Class-Path”是否已添加到 MANIFEST.MF
cat META-INF/MANIFEST.MF
清单版本:1.0
主类:CLASSNAME
创建者:15.0.2(Oracle 公司)
类路径:additional/jars
Follow these steps to add "Class-Path" to existing jar file -
Create "newmanifest" file with following entry
Class-Path: additional/jars
Update existing jar file e.g. "classes.jar"
jar --update --manifest=newmanifest --file classes.jar
Inflate jar file
jar -xvf classes.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
Verify "Class-Path" is added to MANIFEST.MF
cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
main-class: CLASSNAME
Created-By: 15.0.2 (Oracle Corporation)
Class-Path: additional/jars