'No main manifest attribute' while running JAR file built with Gradle

I wrote a small Java application using Gradle to build it. The app comes from this tutorial and contains 2 classes: HelloWorld (the main class) and Greeter. The build.gradle file contains 2 sections (and others, which are currently irrelevant):

apply plugin: 'application'
mainClassName = 'hello.HelloWorld'

and

dependencies {
compile "joda-time:joda-time:2.2"
}

(this is necessary, because the HelloWorld class imports org.joda.time.LocalTime class). I can build the app with gradle build and run it with gradle run . Everything is OK and I get the result. However, when I try to run it with java -jar HelloWorld.jar , I get the error No main manifest attribute . One of solutions found on StackOverflow suggested running the file as

java -cp HelloWorld.jar hello.HelloWorld

or

java -cp HelloWorld.jar HelloWorld

The first one returns Could not find or load main class HelloWorld , the second one returns java.lang.NotClassDefFoundError" org/joda/time/LocalTime . The JAR archive contains following files:

  0 Mon Jul 06 13:13:46 CEST 2015 META-INF/
 25 Mon Jul 06 13:13:46 CEST 2015 META-INF/MANIFEST.MF
  0 Mon Jul 06 13:13:46 CEST 2015 hello/
369 Mon Jul 06 13:13:46 CEST 2015 hello/Greeter.class
988 Mon Jul 06 13:13:46 CEST 2015 hello/HelloWorld.class

I have no idea why gradle doesn't put information about main class into the manifest file. Do you have any suggestions? The code comes directly from the tutorial so it should be OK, but it's not. I repeat that running the app via gradle makes no issues.


This error occurs because org.joda.time.LocalTime is not added to classpath while running hello.HelloWorld .

  • It can be run with classpath set with: gradle run
  • You can include this dependency in the jar, using this plugin.
  • Or prepare a distribution with distZip task, unzip it and use sh or bat script to run the prepared artifact.
  • 链接地址: http://www.djcxy.com/p/55022.html

    上一篇: 没有主清单属性:intellij java ide

    下一篇: 运行使用Gradle构建的JAR文件时'没有主清单属性'