Could not find or load main class with a Jar File

后端 未结 16 1065
有刺的猬
有刺的猬 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:38

    I had this error because I wrote a wrong Class-Path in my MANIFEST.MF

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

    I had a weird issue when an incorrect entry in MANIFEST.MF was causing loading failure. This was when I was trying to launch a very simply scala program:

    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. Probably it should say something like malformed url exception while parsing manifest file.

    Using an absolute path in the manifest file is what IntelliJ uses to provide a long classpath for a program.

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

    I got it working like this:

    TestClass.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. Put TestClass.class in a folder 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
    
    0 讨论(0)
  • 2020-12-01 09:46

    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.

    You need to be storing TestClass.class - some people new at this store the source file, TestClass.java.

    And you need to create the jar file so that TestClass.class appears with a path of classes. Make sure it is not "/classes". Use zip to look at the file and make sure it has a path of "classes".

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