简体   繁体   English

Jar文件无法加载或找到主类

[英]Jar file couldn't load or find main class

I've been trying to run a app from a Jarfile, but it keeps printing out: 我一直在尝试从Jarfile运行一个应用程序,但是它一直在打印:

"cannot find or load main class ...". “找不到或加载主类...”。

I tried to solve this problem using infos from this thread but all seemed to be useless. 我试图使用此线程中的信息来解决此问题,但所有这些似乎都没有用。 To be honest, I'm getting desperate because of the fact that this is such a trivial problem. 老实说,我感到绝望是因为这是一个微不足道的问题。

Anyways, what I did: 无论如何,我做了什么:

Main-Class: com.test.Test

my manifest attribute: 我的清单属性:

jar cfm test.jar manifest.txt <full_path>/out/com/test/*.class

which is what's packaged into the jar file (the Test.class file) 这是打包到jar文件(Test.class文件)中的文件

The Test class: 测试类:

package com.test;

public class Test {
  public static void main(String[] args) {
      System.out.println("Hello World");
   }
}

Firstly, just in case you are using Eclipse IDE, there is some tools like the Fat Jar Plugin which are able to help you to package your build. 首先,以防万一您正在使用Eclipse IDE,有一些工具,例如Fat Jar插件 ,可以帮助您打包构建。

Secondly, there is Maven, to handle your dependencies, and build the package you need with everything ok. 其次,有Maven,可以处理您的依赖关系,并在一切正常的情况下构建所需的包。 In your case, I will look for the Apache Maven Jar Plugin . 在您的情况下,我将寻找Apache Maven Jar插件

Finally, the old school way to go with the commandline, as you tried to do. 最后,按照您尝试的方法,使用命令行的旧式方法。 As Eliott Frisch has said in your question comments, you don't need to provide the fullpath to the mainclass inside your jar cfm test.jar manifest.txt <full_path>/out/ . 正如Eliott Frisch在问题评论中所说的那样,您不需要在jar cfm test.jar manifest.txt <full_path>/out/提供mainclass的完整路径。 And what's because the manifest.txt already give the package information! 这是因为manifest.txt已经提供了程序包信息!

You should package the class correctly. 您应该正确包装该类。 Do

jar cfm test.jar manifest.txt com/test/*.class

in the parent folder of the folder com . com文件夹的父文件夹中。 By providing the absolute path (the way you did), the class file is packaged incorrectly. 通过提供绝对路径(您的方式),该类文件被错误地打包。

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

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