繁体   English   中英

在系统上运行jar文件时出错

[英]Error while running jar file on system

我有一个像以下这样的java文件:

import org.xBaseJ.DBF;
import org.xBaseJ.fields.CharField;
import org.xBaseJ.fields.NumField;
import org.apache.log4j.*;

public class Example2 {

public static void main(String args[]){
..........
}
}

我创建了这个'Example2.jar'文件,按照以下步骤运行:

1) javac Example2.java
2) java Example2
3) This will produce a .class file needed for the JAR file.

4) Next create a manifest file (saved using the extension .txt) using the text editor and input        the following

Main-Class: Example2
or whatever your file's name is.

5) Next create the JAR file using this code:

jar cfe Example2.jar Example2 Example2.class

在第5步之后,我得到了一个名为'Example2.jar'的jar文件。 我尝试使用以下命令运行jar文件:

java -jar HelloWorld.jar

但我得到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/xBaseJ/DBF          at            Example2.main(Example2.java:14)                                 Caused by: java.lang.ClassNotFoundException: org.xBaseJ.DBF                        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)                  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)                  at java.security.AccessController.doPrivileged(Native Method)              at java.net.URLClassLoader.findClass(URLClassLoader.java:360)              at java.lang.ClassLoader.loadClass(ClassLoader.java:424)                   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)           at java.lang.ClassLoader.loadClass(ClassLoader.java:357)                   ... 1 more                                                         

我不明白,是什么原因? 请指导我?

有一件事是,当你创建Jar时,首先看看这个清单,这对包括外部库有什么帮助。

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

在创建并赋予清单时,只需使用配置运行该类

http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html

所以看到这两个链接。

看看这个例子。

运行你的java并获取类文件

javac Test.java

如果你有一些其他外部库工作,那么这样做。

javac -classpath xxx.jar Test.java 

并查看清单配置并使用这样的外部更改来生成该文件。

menifest.txt
Main-Class: Test
Class-Path: xxx.jar xxxx.jar

那么你需要像这样制作jar文件。 运行此命令

jar cfm jarName.jar manifest.txt Test.class

因此我们完成了你可以在同一条路径中使用jarfile。

您看到的错误是由于具有不正确的类路径引起的。 我假设在编译类时,你以某种方式提供了一个类路径(通过传递'-classpath'arg或通过设置'CLASSPATH'环境变量)。 问题是编译类路径与运行时类路径是分开的。 因此,您只需要确保在编译类时类路径上的所有依赖项(最可能是其他jar文件)在运行 jar时也会添加到类路径中。 对于jar文件,通常通过向清单添加“Class-Path”标头来完成

另一种方法是使用命令行arg或环境变量指定类路径,并在java Example2路径中包含Example2.jar文件,并运行java Example2 (不带'-jar')。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM