Could not find or load main class with a Jar File

后端 未结 16 1073
有刺的猬
有刺的猬 2020-12-01 09:09

I\'m trying to load a jar using

@echo off
java -jar Test.jar
pause

With the manifest of

Manifest-Version: 1.0
Main-Class:          


        
相关标签:
16条回答
  • 2020-12-01 09:29

    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.

    I had all my compiled class files in target/classes, which are not packages in my case. 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.

    This doesn't work, as the newly created App.jar will have the class App.class in the directory target/classes. If you try to run this jar with java -jar App.jar, it will complain that it cannot find the App class. This is because the packages inside App.jar don't match the actual packages in the project.

    This could be solved by creating the jar directly from the target/classes directory, using 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. So, in my case, the correct command is 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.

    0 讨论(0)
  • 2020-12-01 09:32

    This error comes even if you miss "-" by mistake before the word jar

    Wrong command java jar test.jar

    Correct command java -jar test.jar

    0 讨论(0)
  • 2020-12-01 09:32

    If you use package names, it won't work with folders containing dots (Error: Could not find or load main class). Even though it compiles and creates the jar file successfully.

    Instead it requires a complete folder hierarchy.

    Fails:

    com.example.mypackage/
        Main.java
    

    Works:

    com/
        example/
            mypackage/
                Main.java
    

    To compile in Linux:

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

    0 讨论(0)
  • 2020-12-01 09:33

    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).

    Example:

    /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.

    0 讨论(0)
  • 2020-12-01 09:35

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

    to run any other class having "public static void main" in some package,

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

    0 讨论(0)
  • 2020-12-01 09:35

    I had the same problem due to copying and pasting code from a Microsoft Word Document. 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

    0 讨论(0)
提交回复
热议问题