【问题标题】:manifest error, Error: Could not find or load main class test.Class1清单错误,错误:无法找到或加载主类 test.Class1
【发布时间】:2018-09-24 18:09:45
【问题描述】:

我有三个类,都在同一个包中。我只需要运行时的执行, java -jar jarName.jar

Class1.java

package test;

public class Class1 {

     public static void main(String[] args) throws Exception
       {

     System.out.println("Enter Class1");
     //Class1 bs = new Class1();

     Class2 objClass2 = new Class2();
     objClass2.method1();

     System.out.println("Exit Class1");
   }
 //hello

}

Class2.java

package test;

public class Class2 {

    int exitValue=100;

    public static int doIt() throws Exception{
        System.out.println("Enter doIt");
        int exitValue=10;
        if(exitValue>0) {
            throw new TestInstallException("Got an Exception here!");
        }       

    return 0;
}

public void method1() throws Exception {
    System.out.println("Enter Class2");
    int exitValue = Class2.doIt();
    System.exit(exitValue); 
}

}

TestInstallException.java

package test;

public class TestInstallException extends java.lang.Exception {

    public TestInstallException()
    {
        super();
    }

    public TestInstallException(String s) {
        super(s);
    }

public TestInstallException(String s, Exception e) {
    super(s, e);
}

public TestInstallException(Throwable cause)
{
    super(cause);
}

}

我什至用以下内容修改了清单文件,

>cat dd/META-INF/MANIFEST.MF
清单版本:1.0 .
创建者:1.8.0_141 (Oracle Corporation)。
主类:test.Class1

所以,我们可以看到它定义了主类。我将所有内容捆绑在一个名为 new.jar 的 jar 文件中。但是,当我运行 java -jar new.jar 时,我得到以下异常。

java -jar new.jar
错误:无法找到或加载主类 test.Class1

jar -tvf test.jar
2018 年 4 月 14 日星期六 20:17:54 IST 2018 META-INF/ .
89 星期六 4 月 14 日 20:29:48 IST 2018 META-INF/MANIFEST.MF .
562 星期六 4 月 14 日 20:11:02 IST 2018 Class1.class .
760 星期六 4 月 14 日 20:11:06 IST 2018 Class2.class .
542 星期六 4 月 14 日 20:10:50 IST 2018 TestInstallException.class 。

【问题讨论】:

  • 我可以从 Eclipse 运行它,也可以使用 java test.Class1。但是,想将其作为 jar 文件运行。
  • 能否请您管理您的代码,使其易于阅读,并在上面的评论中提出问题。
  • 创建 jar 时是否在 cmd 中包含正确的参数:stackoverflow.com/questions/5258159/…
  • 检查 new.jar 的输出。 jar -tvf new.jar
  • 发布命令的输出。我们无法知道您的期望是否正确。

标签: java jar manifest


【解决方案1】:

通过将类捆绑到测试文件夹中来解决问题。

测试/类*.class

另外,在清单中添加了这个
类路径:.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 2016-03-16
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多