[Java] How to output jar with main class specified in gradle in Intellij IDEA

less than 1 minute read

Purpose

When I create a gradle project, do a jar output and run java -jar ***.jar It becomes ***.jar does not have main manifest attribute. Specify the main class to solve this.

environment

  • Windows10 64bit
  • IntelliJ IDEA 2020.2 (Ultimate Edition)

initial state

plugins {
     id'java'
}

group'xyz.miyayu' //Depends on environment
version '1.0-SNAPSHOT' //Depends on environment

repositories {
     mavenCentral()
}

dependencies {
     testCompile group:'junit', name:'junit', version: '4.12'
}

I will specify the main class

//~
version '1.0-SNAPSHOT'

//to add
jar{
     manifest{
         attributes'Main-Class':'xyz.miyayu.hogehogeProject.MainClass' //Specify the class name
     }
}
// add here

repositories {
//~

End

Thank you for your hard work.