I have the following 2 files in my project. It’s just a simple hello world app:
build.gradle src -> main -> java -> com -> MyTest -> Application.java
My build.gradle looks like this:
apply plugin: ‘java’
apply plugin: ‘application’
apply plugin: ‘idea’
mainClassName = “com.MyTest.Application”
jar {
baseName = ‘MyTest’
version = ‘0.1.0’
}
Application.java looks like this:
package com.MyTest;
public class Application {
public static void main(String[] args) throws Exception {
System.out.println(“Hello world”);
}
}
If I run the program with “./gradlew run” it works fine. But if I try to run the program with “java -jar build/libs/MyTest-0.1.0.jar” I get the following message:
no main manifest attribute, in build/libs/MyTest-0.1.0.jar
Any idea what I’m doing wrong? I’m using Gradle 2.0.
When using application plugin it’s best to use shell scripts generated as part of installation/distribution to run the application (have a look at distZip and installApp tasks added by application plugin). These scripts setup your classpath (very important if you declare any artifact dependencies for your app) and know which main class to use, allow you to specify default jvm args in your build, etc.
I actually ran into that issue where the dependencies weren’t bundled into the jar, so I had to use a 3rd-party plugin to get it to work (https://github.com/rholder/gradle-one-jar). It really seems like this should be supported by the application plugin though. Is there an easy way to do create a bundled jar without depending on some 3rd-party code? If not, I’d love it if you added that, and I’m sure others would too. That github repo has been starred 50 times