简体   繁体   English

命令行Java错误:找不到或加载主类

[英]Command line java error: Could not find or load main class

I'm trying to run a project from the command line (Ubuntu 14.04). 我正在尝试从命令行(Ubuntu 14.04)运行项目。 The main class is called Demo, and I have a Demo.java, a Demo.class, and a Demo$1.class all in the same directory (I know it would be better to seperate them). 主要类称为Demo,我在同一个目录中都有一个Demo.java,一个Demo.class和一个Demo $ 1.class(我知道将它们分开会更好)。

I wrote my own Manifest file, MANIFEST.MF, which just looks like this: 我编写了自己的清单文件MANIFEST.MF,如下所示:

Main-Class:Demo

I made sure to end it with a newline. 我确保以换行符结束。

Next, I want to create my .jar file. 接下来,我要创建我的.jar文件。 I did so with this command: 我是通过以下命令这样做的:

jar -cfm example.jar MANIFEST.MF *.class

Then, I try to run my project like so: 然后,我尝试像这样运行我的项目:

java -Djava.library.path=/path/to/dependencies -jar example.jar

I seem to get the following error no matter what I try: 无论尝试如何,我似乎都会遇到以下错误:

Error: Could not find or load main class Demo

I've actually never compiled/run a Java project from the command line before, it's possible I'm making a stupid mistake and just can't figure it out. 实际上,我以前从未从命令行编译/运行Java项目,这很可能是我犯了一个愚蠢的错误,只是无法弄清楚。 Any help is appreciated! 任何帮助表示赞赏!

EDIT: Here are the contents of example.jar, according to vim: 编辑:根据vim,这是example.jar的内容:

    " zip.vim version v27
" Browsing zipfile /home/ellen/bendersexample2/src/bendersexample/example.jar
" Select a file with cursor and press ENTER

META-INF/
META-INF/MANIFEST.MF
AnnotatedBenders.class
Demo$1.class
Demo.class
Demo$ModelType.class
ManualBenders$1.class
ManualBenders$BendersCallback.class
ManualBenders.class
Model.class
Problem.class
Solution.class
Solution$Verbosity.class
StandardModel.class

Here are the contents of the META-INF/MANIFEST.MF which is in the jar: 以下是罐中META-INF / MANIFEST.MF的内容:

Manifest-Version: 1.0
Created-By: 1.8.0_161 (Oracle Corporation)
Main-Class: Demo 

UPDATE: Here are the interesting parts of Demo.java: 更新:这是Demo.java的有趣部分:

package bendersexample;

public final class Demo {

   /* Some functions */

  public static void main(final String[] args) {

    /* Some code */

 }

}

I changed my MANIFEST.MF to the following: 我将MANIFEST.MF更改为以下内容:

Main-Class:bendersexample.Demo

And re-generated the example.jar file. 并重新生成example.jar文件。 I still get the following: 我仍然得到以下信息:

Error: Could not find or load main class bendersexample.Demo

Could there be an issue with how I generate my class files? 我如何生成班级文件可能会有问题吗? To generate the class files initially, I did the following: 为了最初生成类文件,我执行了以下操作:

javac -classpath .:/opt/ibm/ILOG/CPLEX_Studio_Community128/cplex/lib/cplex.jar *.java

Please let me know what else I should try! 请让我知道我还应该尝试什么! Thank you 谢谢

The problem was just that had the Manifest in the /bendersexample folder and the was creating the .jar in this folder as well! 问题是,在/bendersexample文件夹中有Manifest,并且也在该文件夹中创建了.jar! I just needed to move that stuff into the parent directory and everything worked fine! 我只需要将这些内容移到父目录中,一切就可以正常工作!

The final Manifest file used bendersexample.Demo as the Main-Class, and the jar was created and run from /bendersexample 's parent directory. 最终的清单文件使用bendersexample.Demo作为Main-Class,然后从/bendersexample的父目录创建并运行该jar。

If anyone runs into this problem I would recommend taking a look at your project's structure before trying anything else, because this turned out to be a very easy fix! 如果有人遇到这个问题,我建议您在尝试任何其他操作之前先看一下您项目的结构,因为事实证明这很容易解决!

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

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