获取 Could not find the main class: Java 中的错误,即使 Manifest 指定了类
这是一个非常流行的错误,因为我是 Java 新手,所以我可能会误解其他人的答案:
在带有 JRE 1.6 的 Windows 7 上,
我从 Restlet 复制了 First Steps 包,以作为独立应用程序进行尝试。我有一个名为 FirstStepsMain 的类(请参阅下面的 Class),并在我的清单中将其定义为“Main-Class:firstSteps.FirstStepsMain”(请参阅下面的Manifest)。我将 Windows 中的类路径变量设置为 \firstSteps.jar。考虑到可能没有看到外部 jar,我将它们移动到同一个文件夹,并为它们设置了 Windows 类路径。
我什至只对第一个 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
但我仍然收到错误。任何帮助将不胜感激。
类别:
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]
This is a very popular error and since I am new to Java I may be misunderstanding other people's answers:
On Windows 7 with JRE 1.6
I copied the First Steps package from Restlet to try on my own as a Stand alone app. I have a class called FirstStepsMain (see Class below) and define it in my Manifest (see Manifest below) as "Main-Class: firstSteps.FirstStepsMain". I set my class path variable in Windows to \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.
I have even used the -classpath command with just the first Jar and all three Jars:
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
Main-Class:firstSteps.FirstStepsMain
放在清单文件的第二行中。请参阅了解清单
You need to put
Main-Class: firstSteps.FirstStepsMain
in second line of the manifest file.Please see Understanding the Manifest
当您使用
-jar
选项启动Java程序时,命令行上的-classpath
选项将被忽略。所以你在那里指定什么并不重要。相反,您必须在清单文件中指定类路径。将程序所需的所有 JAR 添加到清单文件中的
Class-Path
属性中,但不添加firstSteps.jar
本身。例如,它应该如下所示:请参阅 将类添加到 JAR 文件的类路径< /a> 来自教程在 JAR 文件中打包程序。
然后您应该能够使用以下命令运行它:
When you start a Java program with the
-jar
option, the-classpath
option on the command line will be ignored. 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 notfirstSteps.jar
itself. It should look for example like this:See Adding Classes to the JAR File's Classpath from the tutorial Packaging Programs in JAR Files.
You should then be able to run it with: