Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gradle to build a very simple java program

Tags:

java

gradle

I am new to Java and Gradle, and have a very newbie question. I have the following Java file:

public class TestMain {
    public static void main(String[] args) {
        System.out.println("Hello.....");
    }
}

I am able to compile the above file using javac, and run it using command "java TestMain".

I am now trying to do the same using gradle build framework. I performed the following steps: run "gradle init --type java-library copied the above file into src/main/java/

When I run "./gradlew build", I get a TestMain.class file, and a also a "building-java-file.jar" (the whole gradle directory is in building-java-file directory).

$ ls -l build/classes/main/TestMain.class 
-rw-r--r--  1 user1  foo\eng  610 May 22 17:22 build/classes/main/TestMain.class

$ java  build/classes/main/TestMain
Error: Could not find or load main class build.classes.main.TestMain

How do I run the TestMain.class? Also, what is the reason for gradle creating the jar file - building-java-file.jar?

Btw, my build.gradle file is pretty empty.

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

    testCompile 'junit:junit:4.12'
}

Thank you, Ahmed.'

like image 845
Ahmed A Avatar asked Mar 28 '26 02:03

Ahmed A


1 Answers

Answers to my question are explained clearly in the Gradele documentation site.

Basically, start with: gradle init --type java-application

Link: https://guides.gradle.org/building-java-applications/?_ga=2.79084180.165016772.1527029076-64181247.1527029076

like image 161
Ahmed A Avatar answered Mar 31 '26 03:03

Ahmed A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!