获取无法找到主类:Java中的错误,即使Manifest已指定类

时间:2011-02-22 23:37:29

标签: java windows-7 restlet

这是一个非常受欢迎的错误,因为我是Java的新手,我可能会误解其他人的答案:

在Windows 7上使用JRE 1.6

我从Restlet复制了First Steps包,尝试自己作为独立应用程序。我有一个名为FirstStepsMain的类(请参阅下面的 Class ),并在我的Manifest(参见下面的 Manifest )中将其定义为“Main-Class:firstSteps.FirstStepsMain”。我在Windows中将我的类路径变量设置为\ firstSteps.jar。认为可能没有看到外部罐子我将它们移动到同一文件夹并为它们设置Windows类路径。

我甚至使用-classpath命令只包含第一个Jar和所有三个Jars:

E:\ResultsDashboard>java -verbose -classpath e:\ResultsDashboard\firstSteps.jar;e:\resultsdashboard\org.restlet.jar;E:\ResultsDashboardorg.restlet.ext.servlet.jar -jar firstSteps.jar

然而我仍然得到错误。任何帮助将不胜感激。

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-Version: 1.0
Main-Class: firstSteps.FirstStepsMain

Class-Path: firstSteps.jar [note: I added this as a desperate attempt]

2 个答案:

答案 0 :(得分:0)

您需要将Main-Class: firstSteps.FirstStepsMain放在清单文件的第二行 请参阅Understanding the Manifest

答案 1 :(得分:0)

使用-jar选项启动Java程序时,命令行上的-classpath选项将被忽略。所以你在那里指定的并不重要。

相反,您必须在清单文件中指定类路径。将程序所需的所有JAR添加到清单文件中的Class-Path属性,而不是firstSteps.jar本身。它应该是这样的例子:

Class-Path: org.restlet.jar org.restlet.ext.servlet.jar

请参阅教程Adding Classes to the JAR File's Classpath中的Packaging Programs in JAR Files

然后您应该能够:

运行它
java -jar firstSteps.jar