简体   繁体   English

能够编译但无法使用jar文件从cmd运行Java

[英]Able to compile but unable to run Java from cmd with jar files

I have a java file which uses jfreechart libraries, uses a text file from local drive and displays graph. 我有一个使用jfreechart库的java文件,使用本地驱动器中的文本文件并显示图形。 Runs fine with eclipse. 用日食运行很好。 However, I want to run this from cmd prompt, other simple Java files are able to run successfully via cmd prmnt but not able to run this file. 但是,我想从cmd提示符运行它,其他简单的Java文件能够通过cmd prmnt成功运行但无法运行此文件。 PS: MyTool.java is able to compile without errors and class file is created, but not able to run. PS:MyTool.java能够无错误地编译并创建类文件,但无法运行。


 1) This is how I am compiling it in cmd prompt: (gives 0 errors)

C:\Documents and Settings\hello.maga\workspace\MyTool\lib>javac -cp "gnujaxp.
jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-e
xperimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool.java

  2) This is how I am running it:


 C:\Documents and Settings\hello.maga\workspace\MyTool\lib>java -cp "gnujaxp.j
 ar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-ex
 perimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool


    Error for second command: 

    Exception in thread "main" java.lang.NoClassDefFoundError: MyTool
    Caused by: java.lang.ClassNotFoundException: MyTool
    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)
    Could not find the main class: MyTool.  Program will exit.

What I don't understand is, if there are any errors, then it should not compile in first place, can someone educate me. 我不明白的是,如果有任何错误,那么它不应该首先编译,有人可以教育我。 Thank you very much. 非常感谢你。

You need to include "." 你需要包含“。” in the classpath, like so: 在类路径中,如下所示:

java -cp ".;gnujaxp.jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-experimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar"

From " Setting the class path ": "The class path tells SDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found." 从“ 设置类路径 ”:“类路径告诉SDK工具和应用程序在哪里可以找到第三方和用户定义的类 - 即不是Java扩展或Java平台的一部分的类。类路径需要找到你用javac编译器编译的任何类 - 它的默认值是当前目录,可以方便地找到这些类。“

However if you set the classpath yourself, the default no longer applies, and you're expecting it to load classes from the current directory. 但是,如果您自己设置类路径,则默认不再适用,并且您希望它从当前目录加载类。 You'll have to add it manually, like by adding "." 您必须手动添加它,例如添加“。” to the class path as Ed Staub recommended. 如Ed Staub推荐的那样。

When compiling, your class wasn't needed on the class path, so to speak, since it's what was being compiled. 在编译时,类路径上不需要您的类,可以这么说,因为它是正在编译的。 You only needed all the other classes (in the jar files) on the class path for that. 您只需要类路径上的所有其他类(在jar文件中)。 That's why you're able to compile but not run, even though you used an identical class path for both operations. 这就是为什么你能够编译但不能运行,即使你为这两个操作使用了相同的类路径。

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

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