简体   繁体   English

找不到主类:即使Manifest指定了该类,Java也会出错

[英]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.

相关问题 即使我在清单文件中定义了Main-class,也会出现“错误:找不到或加载类” - Getting “Error: Could not find or load class” even though I have the Main-class defined in a Manifest File Java'找不到.jar的主类',即使它是用manifest.mf编写的 - Java 'Could not find main class' for .jar even though its written in manifest.mf 即使存在主类,Java 也找不到主类 - Java cannot find main class even though main class is present 在 Eclipse IDE 中出现“错误:无法找到或加载主 class aQute.launcher.Launcher”,即使它在命令行中工作 - Getting "Error: Could not find or load main class aQute.launcher.Launcher" in Eclipse IDE even though it works in command line 即使清单正确,Java 也无法找到或加载主 class - Java couldn't find or load main class even if manifest is correct gradle“没有指定主要 class”,即使主要 class 存在 - gradle "No main class specified" even though main class exists 找不到主要类别…罐子或清单 - Could not find main class…jar or manifest 尽管明显,罐子找不到主类 - jar could not find main class, despite manifest Java 错误:无法找到或加载主类 - Java Error: Could not find or load main class Java-错误:找不到或加载主类 - Java - Error: Could not find or load main class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM