
上QQ阅读APP看书,第一时间看更新
How to do it...
Let's go through these steps, with which we can create a self-executable JAR:
- We’ll create a simple class HelloWorld.kt having the main function, which just prints out “Hello world!”:
fun main(args:Array<String>){
println("Hello world")
}
- Now we need to configure a jar task, which Gradle build goes through to inform it of our entry to our project. In a Java project, this will be the path to the class where our main() function resides, so you will need to add this jar task in build.gradle:
jar {
manifest {
attributes 'Main-Class': 'HelloWorldKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
- After adding the preceding snippet to build.gradle, you need to run the following gradle command to create the jar file:
./gradlew clean jar
- The created jar file can be found in the build/libs folder. Now you can just run the java -jar demo.jar command to run the JAR file.
After you do that, you can see the output in the console:
