简体   繁体   English

使用jar库包编译和执行Java程序

[英]Compiling and executing a java program with a jar library package

I am trying to compile and execute a java program which uses JTidy. 我正在尝试编译和执行使用JTidy的Java程序。 I've managed to compile the program using the following command: 我设法使用以下命令来编译程序:

javac -classpath jtidy-r938.jar @sourcefile

This seems to work just fine. 这似乎工作正常。 However, when I try to run the program with the following command (Top is the name of the class containing the main portion of the program): 但是,当我尝试使用以下命令运行程序时(Top是包含程序主要部分的类的名称):

java -classpath jtidy-r938.jar Top

I get this error: 我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Top
Caused by: java.lang.ClassNotFoundException: Top
...
Could not find the main class: Top. Program will exit.

This is mostly likely a very dumb question with a simple solution, but it is driving me insane. 通过简单的解决方案,这很可能是一个非常愚蠢的问题,但这却使我发疯。 Please help!! 请帮忙!!

Are you using the fully qualified name of the class Top here ie is your class Top packageless? 您是否在使用Top类的完全限定名称,即您的Top类没有包装? If not, you need to provide the fully qualified class name for the above command to work. 如果不是,则需要提供完全限定的类名才能使上述命令起作用。 Make sure you also put in other JAR dependencies in the classpath by separating them with ; 确保您还通过将其他JAR依赖项与之分开来将它们放入类路径中; or : depending on your target platform. :取决于您的目标平台。

EDIT: Also, as mentioned below, make sure you also include . 编辑:另外,如下所述,请确保还包括. (or the appropriate location) in your classpath if your class dependencies are not just in the JAR but also .class files on your file system. (或适当的位置)在类路径中,如果您的类依赖关系不仅在JAR中,而且在文件系统中也为.class文件。

You are removing the path "." 您正在删除路径“。” (current directory) from the classpath when you set it. (当前目录)在设置时来自类路径。

Try java -classpath .;jtidy-r938.jar Top . 试试java -classpath .;jtidy-r938.jar Top

That way it should be able to find your class. 这样,它应该可以找到您的班级。

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

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