javac编译文件和jar,但是java失败 [英] javac compiles files and jars but java fails
问题描述
我已将.java
文件放在一个文件夹中,例如主文件夹内lib文件夹中的/opt/program/
和.jar
文件,例如/opt/program/lib/jsoup-1.10.3.jar
.然后我运行这些命令
I have put the .java
files in a folder, e.g. /opt/program/
and a .jar
file in a lib folder inside the main folder, e.g. /opt/program/lib/jsoup-1.10.3.jar
. Then I ran these commands
javac -classpath lib/*jar *.java # compile is OK
java TheFrame # program runs
在其中一个Java文件中,例如Tester.java
,我使用了jsoup-1.10.3.jar
中定义的对象.像这样
In one of the java files, e.g. Tester.java
, I have used an object defined in jsoup-1.10.3.jar
. Something like this
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Tester {
Document doc;
public Tester() { }
public void doConnect(String name) throws Exception
{
doc = Jsoup.connect("http://somewhere.com").get();
...
}
}
在运行时,当涉及到Jsoup.connect
时,出现此错误
During the runtime, when it comes to Jsoup.connect
, I get this error
java.lang.NoClassDefFoundError
更新:
根据建议,我还必须在Java命令中包含jar文件.我这样做了,但仍然收到相同的错误
As suggested, I have to include the jar file in the java command as well. I did that but still get the same error
$ ls lib/
jsoup-1.10.3.jar
$ /opt/jdk1.8.0_131/bin/javac -classpath lib/*.jar *.java
$ /opt/jdk1.8.0_131/bin/java -classpath .:lib/*.jar TheFrame
phase_1
java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at javax.swing.SwingWorker.get(SwingWorker.java:602)
at TheFrame$10.propertyChange(TheFrame.java:481)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
....
Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at Tester.connectForTranscript(Tester.java:24)
at ExcelFile.analyzeSeq(ExcelFile.java:706)
at TheFrame$9.doInBackground(TheFrame.java:448)
....
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
....
Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at Tester.doConnect(Tester.java:24)
...
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
我该如何解决?
推荐答案
您使用设置了classpath的类来编译您的类:
You compile your class with the classpath set :
javac -classpath lib/*jar *.java
但是您不会在设置了classpath的情况下启动可运行类:
but you don't launch the runnable class with the classpath set :
java TheFrame
此外,在.jar
. "rel =" nofollow noreferrer>设置类路径文档以设置带有通配符的类路径.
Besides, the .
and the extension .jar
is not specified in the Setting the class path documentation to set a classpath with a wildcard.
类路径条目可以包含基本名称通配符, 这被认为等效于指定所有文件的列表 在扩展名为.jar或.JAR的目录中.例如, 类路径条目foo/指定目录中的所有JAR文件 foo. 仅由*组成的类路径条目扩展为所有列表 当前目录中的jar文件.
Class path entries can contain the basename wildcard character , which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/ specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.
包含*的类路径条目将与类文件不匹配.到 在单个目录foo中匹配类和JAR文件,请使用 foo; foo/*或foo/*; foo.选择的顺序确定是否 在foo中的JAR文件之前先加载foo中的类和资源,或者 反之亦然.
A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.
子目录不是递归搜索的.例如,foo/*看起来 仅适用于foo中的JAR文件,而不能用于foo/bar,foo/baz等中.
Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.
如果您的所有jar都位于lib文件夹的根目录下,这应该可以解决您的问题:
This should solve your problem if all your jar are located at the root of the lib folder:
java -classpath .:lib/* TheFrame
要编译您的类,应使用相同的synthax设置类路径.
令人惊讶的是,执行时没有编译错误:
To compile your class, you should use the same synthax to set the classpath.
It is surprising that you have no compilation error when you execute :
javac -classpath lib/*jar *.java
我尝试过:
javac -cp D:\repo\commons-lang3\3.1\*jar MyClass.java
我收到javac
错误:
javac:无效标志: D:\ repo \ commons-lang3 \ 3.1 \ commons-lang3-3.1-sources.jar
javac: invalid flag: D:\repo\commons-lang3\3.1\commons-lang3-3.1-sources.jar
使用
javac -cp D:\repo\commons-lang3\3.1\* MyClass.java
编译很好.
与java
命令的行为完全相同.
I have exactly the same behavior with the java
command.
这篇关于javac编译文件和jar,但是java失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!