[英]Getting Could not find the main class: error in Java even though Manifest has the class specified
This is a very popular error and since I am new to Java I may be misunderstanding other people's answers: 这是一个非常普遍的错误,由于我是Java新手,所以我可能会误解其他人的答案:
On Windows 7 with JRE 1.6 在具有JRE 1.6的Windows 7上
I copied the First Steps package from Restlet to try on my own as a Stand alone app. 我从Restlet复制了“第一步”程序包,以作为独立应用程序单独尝试。 I have a class called FirstStepsMain (see Class below) and define it in my Manifest (see Manifest below) as "Main-Class: firstSteps.FirstStepsMain".
我有一个名为FirstStepsMain的类(请参见下面的类 ),并在清单(请参见下面的清单 )中将其定义为“ Main-Class:firstSteps.FirstStepsMain”。 I set my class path variable in Windows to \\firstSteps.jar.
我在Windows中将类路径变量设置为\\ firstSteps.jar。 Thinking that it might be that the external jars were not being seen I moved them to the same folder and set Windows class paths for them too.
考虑到可能没有看到外部jar,我将它们移到了相同的文件夹并为其设置了Windows类路径。
I have even used the -classpath command with just the first Jar and all three Jars: 我什至只对第一个Jar和所有三个Jar使用了-classpath命令:
E:\ResultsDashboard>java -verbose -classpath e:\ResultsDashboard\firstSteps.jar;e:\resultsdashboard\org.restlet.jar;E:\ResultsDashboardorg.restlet.ext.servlet.jar -jar firstSteps.jar
And yet I still get the error. 但是我仍然得到错误。 Any help would be appreciated.
任何帮助,将不胜感激。
Class : 类 :
package firstSteps;
import org.restlet.Component;
import org.restlet.data.Protocol;
public class FirstStepsMain {
public static void main(String[] args) throws Exception {
// Create a new Component.
Component component = new Component();
// Add a new HTTP server listening on port 8182.
component.getServers().add(Protocol.HTTP, 8182);
// Attach the sample application.
component.getDefaultHost().attach("/firstSteps",
new FirstStepsApplication());
// Start the component.
component.start();
}
}
Manifest : 清单 :
Manifest-Version: 1.0
Main-Class: firstSteps.FirstStepsMain
Class-Path: firstSteps.jar [note: I added this as a desperate attempt]
You need to put Main-Class: firstSteps.FirstStepsMain
in second line of the manifest file. 您需要将
Main-Class: firstSteps.FirstStepsMain
放在清单文件的第二行。
Please see Understanding the Manifest 请参阅了解清单
When you start a Java program with the -jar
option, the -classpath
option on the command line will be ignored. 当使用
-jar
选项启动Java程序时,命令行上的-classpath
选项将被忽略。 So it doesn't matter what you specify there. 因此,在此处指定的内容都没有关系。
Instead, you must specify the classpath in the manifest file. 而是必须在清单文件中指定类路径。 Add all the JARs that the program needs to the
Class-Path
attribute in the manifest file, but not firstSteps.jar
itself. 将程序需要的所有JAR添加到清单文件中的
Class-Path
属性,而不是firstSteps.jar
本身。 It should look for example like this: 它应该看起来像这样:
Class-Path: org.restlet.jar org.restlet.ext.servlet.jar
See Adding Classes to the JAR File's Classpath from the tutorial Packaging Programs in JAR Files . 请参阅教程打包程序 到JAR文件中的将类添加到JAR文件的类路径 中 。
You should then be able to run it with: 然后,您应该可以使用以下命令运行它:
java -jar firstSteps.jar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.