Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 224
user3855877 Avatar asked Oct 31 '22 00:10

user3855877


1 Answers

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

  1. It can be run with classpath set with: gradle run
  2. You can include this dependency in the jar, using this plugin.
  3. Or prepare a distribution with distZip task, unzip it and use sh or bat script to run the prepared artifact.
like image 154
Opal Avatar answered Nov 15 '22 06:11

Opal