Can't execute jar- file: “no main manifest attribute”

后端 未结 30 1906
不知归路
不知归路 2020-11-21 22:30

I have installed an application, when I try to run it (it\'s an executable jar) nothing happens. When I run it from the commandline with:

java -jar \

30条回答
  •  渐次进展
    2020-11-21 22:48

    First, it's kind of weird, to see you run java -jar "app" and not java -jar app.jar

    Second, to make a jar executable... you need to jar a file called META-INF/MANIFEST.MF

    the file itself should have (at least) this one liner:

    Main-Class: com.mypackage.MyClass
    

    Where com.mypackage.MyClass is the class holding the public static void main(String[] args) entry point.

    Note that there are several ways to get this done either with the CLI, Maven, Ant or Gradle:

    For CLI, the following command will do: (tks @dvvrt)

    jar cmvf META-INF/MANIFEST.MF .jar  
    

    For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:

    
      
        
          
          org.apache.maven.plugins
          maven-jar-plugin
          3.1.0
          
            
              
                true
                lib/
                com.mypackage.MyClass
              
            
          
        
      
    
    

    (Pick a appropriate to your project.)

    For Ant, the snippet below should help:

    
      
      
      
        
      
    
    

    Credits Michael Niemand -

    For Gradle:

    plugins {
        id 'java'
    }
    
    jar {
        manifest {
            attributes(
                    'Main-Class': 'com.mypackage.MyClass'
            )
        }
    }
    

提交回复
热议问题