简体   繁体   English

无法使用 Jar 文件找到或加载主 class

[英]Could not find or load main class with a Jar File

I'm trying to load a jar using我正在尝试使用加载 jar

@echo off
java -jar Test.jar
pause

With the manifest of随着清单

Manifest-Version: 1.0
Main-Class: classes.TestClass

In the Jar directory, I can clearly see a classes\TestClass file when I extract it.在Jar目录下,我解压的时候可以清楚的看到一个classes\TestClass文件。

Edit: classes.TestClass does have a public static void main(String[] args) .编辑: classes.TestClass确实有一个public static void main(String[] args)

Package Deceleration in classes.TestClass is package classes; Package班级减速classes.TestClasspackage classes;

But I still keep getting the error message但我仍然不断收到错误消息

Could not find or load main class classes.TestClass

I've been through everything I've been able to find with this problem, and none of it seems to help.我已经解决了这个问题所能找到的所有问题,但似乎都无济于事。

I've tried editing the classpath, redoing the manifest, installing the new JRE.我试过编辑类路径、重做清单、安装新的 JRE。

What else should I be doing?我还应该做什么?

I got it working like this:我让它像这样工作:

TestClass.Java测试类.Java

package classes;

public class TestClass {

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

}

Use javac on the command line to produce TestClass.class .在命令行上使用javac生成TestClass.class Put TestClass.class in a folder classes/ .TestClass.class放在文件夹classes/

MANIFEST.MF清单文件

Manifest-Version: 1.0
Main-Class: classes.TestClass

Then run然后运行

jar cfm test.jar MANIFEST.MF classes/

Then run it as然后运行它

java -jar test.jar

java -cp "full-path-of-your-jar" Main java -cp "full-path-of-your-jar" Main

to run any other class having "public static void main" in some package,在某个包中运行具有“public static void main”的任何其他类,

java -cp "full-path-of-your-jar" package1.package2.packages-hierarchy.ClassHavingMain java -cp "full-path-of-your-jar" package1.package2.packages-hierarchy.ClassHavingMain

This error comes even if you miss "-" by mistake before the word jar即使您在单词 jar 之前错误地错过了“-”,也会出现此错误

Wrong command java jar test.jar错误的命令java jar test.jar

Correct command java -jar test.jar正确的命令java -jar test.jar

1.Create a text file calles Manifest.txt and provide the value as 1.创建一个名为 Manifest.txt 的文本文件并提供值作为

Main-Class: classes.TestClass主类:classes.TestClass

2.Create the jar as 2.创建罐子

jar cfm test.jar Manifest.txt classes/*.class jar cfm test.jar Manifest.txt classes/*.class

3.Run the jar as 3.运行jar作为

java -jar test.jar java -jar 测试.jar

I know this is an old question, but I had this problem recently and none of the answers helped me.我知道这是一个老问题,但我最近遇到了这个问题,但没有一个答案对我有帮助。 However, Corral's comment on Ryan Atkinson's answer did tip me off to the problem.然而,Corral 对 Ryan Atkinson 回答的评论确实让我注意到了这个问题。

I had all my compiled class files in target/classes , which are not packages in my case.我的所有编译类文件都在target/classes ,在我的情况下它们不是包。 I was trying to package it with jar cvfe App.jar target/classes App , from the root directory of my project, as my App class was in the default unnamed package.我试图从我的项目的根目录使用jar cvfe App.jar target/classes App打包它,因为我的 App 类位于默认的未命名包中。

This doesn't work, as the newly created App.jar will have the class App.class in the directory target/classes .这不起作用,因为新创建的App.jar将在目录target/classes有类App.class If you try to run this jar with java -jar App.jar , it will complain that it cannot find the App class.如果您尝试使用java -jar App.jar运行此 jar,它会抱怨找不到App类。 This is because the packages inside App.jar don't match the actual packages in the project.这是因为App.jar中的包与项目中的实际包不匹配。

This could be solved by creating the jar directly from the target/classes directory, using jar cvfe App.jar . App这可以通过使用jar cvfe App.jar . App直接从target/classes目录创建 jar 来解决jar cvfe App.jar . App jar cvfe App.jar . App . jar cvfe App.jar . App This is rather cumbersome in my opinion.这在我看来是相当麻烦的。

The simple solution is to list the folders you want to add with the -C option instead of using the default way of listing folders.简单的解决方案是使用-C选项列出要添加的文件夹,而不是使用列出文件夹的默认方式。 So, in my case, the correct command is java cvfe App.jar App -C target/classes .所以,就我而言,正确的命令是java cvfe App.jar App -C target/classes . . . This will directly add all files in the target/classes directory to the root of App.jar , thus solving the problem.这将直接将target/classes目录中的所有文件添加到App.jar的根目录,从而解决问题。

I had the same problem due to copying and pasting code from a Microsoft Word Document.由于从 Microsoft Word 文档复制和粘贴代码,我遇到了同样的问题。 Seems there was a slightly different type of dash - character used, when I replaced the longer dash character with the correct - the program executed properly似乎有一种稍微不同类型的破折号-使用的字符,当我用正确的字符替换较长的破折号时-程序正确执行

This is very difficult to debug without complete information.如果没有完整的信息,这是很难调试的。

The two most likely-looking things at this point are that either the file in the jar is not stored in a directory WITHIN THE JAR, or that it is not the correct file.此时最可能出现的两个情况是,jar 中的文件未存储在 WITHIN THE JAR 的目录中,或者它不是正确的文件。

You need to be storing TestClass.class - some people new at this store the source file, TestClass.java.您需要存储 TestClass.class - 一些新人在此存储源文件 TestClass.java。

And you need to create the jar file so that TestClass.class appears with a path of classes.并且您需要创建 jar 文件,以便 TestClass.class 与类路径一起出现。 Make sure it is not "/classes".确保它不是“/classes”。 Use zip to look at the file and make sure it has a path of "classes".使用 zip 查看文件并确保它具有“类”路径。

I had a similar problem which I could solve by granting execute-privilege for all parent folders in which the jar-file is located (on a linux system).我有一个类似的问题,我可以通过为 jar 文件所在的所有父文件夹(在 linux 系统上)授予执行权限来解决这个问题。

Example:例子:

/folder1/folder2/folder3/executable.jar /folder1/folder2/folder3/executable.jar

all 3 folders (folder1, folder2 and folder3) as well as the executable.jar need execute-privilege for the current user, otherwise the error "Could not find or load main class ..." is returned.所有 3 个文件夹(文件夹 1、文件夹 2 和文件夹 3)以及 executable.jar 都需要当前用户的执行权限,否则将返回错误“无法找到或加载主类...”。

I had a weird issue when an incorrect entry in MANIFEST.MF was causing loading failure.MANIFEST.MF 中的错误条目导致加载失败时我遇到了一个奇怪的问题。 This was when I was trying to launch a very simply scala program:这是当我试图启动一个非常简单的 scala 程序时:

Incorrect:不正确:

Main-Class: jarek.ResourceCache
Class-Path: D:/lang/scala/lib/scala-library.jar

Correct:正确的:

Main-Class: jarek.ResourceCache
Class-Path: file:///D:/lang/scala/lib/scala-library.jar

With an incorrect version, I was getting a cryptic message, the same the OP did.由于版本不正确,我收到了一条神秘消息,与 OP 一样。 Probably it should say something like malformed url exception while parsing manifest file .可能它应该在解析清单文件时说类似格式错误的 url 异常

Using an absolute path in the manifest file is what IntelliJ uses to provide a long classpath for a program.在清单文件中使用绝对路径是 IntelliJ 用来为程序提供长类路径的方法。

At least the way I've done this is as follows:至少我这样做的方式如下:

If you have a nested src tree (say com.test.myclass.MyClass) and you are compiling from a root directory you need to do the following:如果您有一个嵌套的 src 树(比如 com.test.myclass.MyClass)并且您是从根目录编译,则需要执行以下操作:

1) when you create the jar (usually put this in a script): jar -cvfm my.jar com/test/myclass/manifest.txt com/test/myclass/MyClass.class 1) 当您创建 jar 时(通常将其放在脚本中): jar -cvfm my.jar com/test/myclass/manifest.txt com/test/myclass/MyClass.class

2) The manifest should look like: 2) 清单应如下所示:

Mainfest-version: 1.0 Main-Class: com.test.myclass.MyClass Class-Path: . Mainfest 版本:1.0 主类:com.test.myclass.MyClass 类路径:. my.jar我的.jar

3) Now you can run the jar from anywhere like this: 3)现在你可以像这样从任何地方运行jar:

java -jar my.jar java -jar my.jar

Hope this helps someone希望这有助于某人

I follow the following instruction to create a executable .jar in Eclipse.我按照以下说明在 Eclipse 中创建一个可执行的 .jar。 Then Run command "java -jar .jar " to launch the program.然后运行命令“java -jar .jar”来启动程序。

It takes care of creating mainfest and includeing main class and library files parts for you.它负责创建 mainfest 并为您包含主类和库文件部分。

http://java67.blogspot.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html http://java67.blogspot.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html

我有这个错误,因为我在我的MANIFEST.MF写了一个错误的类路径

If you use package names, it won't work with folders containing dots ( Error: Could not find or load main class ).如果您使用包名称,它将不适用于包含点的文件夹( Error: Could not find or load main class )。 Even though it compiles and creates the jar file successfully.即使它成功编译并创建了 jar 文件。

Instead it requires a complete folder hierarchy.相反,它需要完整的文件夹层次结构。

Fails:失败:

com.example.mypackage/
    Main.java

Works:作品:

com/
    example/
        mypackage/
            Main.java

To compile in Linux:在 Linux 中编译:

javac `find com/ -name '*.java'` \\ && jar cfe app.jar com.example.mypackage.Main `find com/ -name '*.class'`

in IntelliJ, I get this error when trying to add the lib folder from the main project folder to an artifact.在 IntelliJ 中,尝试将 lib 文件夹从主项目文件夹添加到工件时出现此错误。

Placing and using the lib folder in the src folder works.在 src 文件夹中放置和使用 lib 文件夹有效。

A possible reason for this error message:此错误消息的可能原因:

Could not find or load main class无法找到或加载主类

is when the main class extends (or implements) another class (or interface), which is not on the classpath.是当主类扩展(或实现)另一个不在类路径上的类(或接口)时。

我通过FTP传输jar文件并忘记设置二进制模式后收到相同的错误消息。

I had this error because I extracted JAR files from libraries to the target JAR by IntellijIDEA.我有这个错误,因为我通过 IntellijIDEA 从库中提取 JAR 文件到目标 JAR。 When I choose another option "copy to the output directory...", it solved the problem.当我选择另一个选项“复制到输出目录...”时,它解决了问题。 Hope this helps you希望这对你有帮助

I was getting this error because my main class is in test package, and I am creating artifact using IntelliJ.我收到此错误是因为我的主类在测试包中,并且我正在使用 IntelliJ 创建工件。 After I checked the box Include Tests when creating artifact, it got resolved.在创建工件时选中包含测试框后,它得到了解决。

Screenshot from IntelliJ showing Include Tests checkbox IntelliJ 的屏幕截图,显示“包含测试”复选框

Sometimes could missing the below line under <build> tag in pom.xml when packaging through maven.通过 maven 打包时,有时可能会丢失 pom.xml 中<build>标记下的以下行。 since src folder contains your java files因为src文件夹包含您的 java 文件

<sourceDirectory>src</sourceDirectory>

If you have your setup with Maven project -- make sure that the following match your JDK in the pom.xml file.如果您使用 Maven 项目进行设置——请确保以下内容与 pom.xml 文件中的 JDK 匹配。

<maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target>

in my case I had it set to 14 whereas my application was set to 14 -- and the conflict was causing my jar file to give the class not found error.在我的情况下,我将它设置为 14,而我的应用程序设置为 14——冲突导致我的 jar 文件给出类未找到错误。

For IntelliJ users - this can also happen when class that cannot be found was placed in a directory marked as Tests (indicated by green color on directory icon).对于 IntelliJ 用户 - 当无法找到的 class 放置在标记为测试的目录中(目录图标上以绿色表示)时,也会发生这种情况。

To solve this:要解决这个问题:

a.一个。 move the class to sources directory (prefered)将 class 移动到源目录(首选)

or或者

b.湾。 make sure "include tests" checkbox is selected when creating an artifact确保在创建工件时选中“包含测试”复选框

I faced the same issue while using artifact in IntelliJ.我在 IntelliJ 中使用工件时遇到了同样的问题。 After using copy all dependency jar, it works for me.使用复制所有依赖项 jar 后,它对我有用。

You need to use -jar argument to set the path to your jar:您需要使用-jar参数来设置您的 jar 的路径:

java -jar your_jar.jar

You don't need to set main class manualy if your manifest is correct.如果清单正确,则无需手动设置 main class。

我有一个incorret class-path librarie并解决相同的错误

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

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