NOTE This method is quite advanced and is not recommended for beginners. It uses Gradle, a build automation tool, to compile and run complex programs that consist of multiple source-code files.
Project structure:
.
├── build.gradle
└── src/
└── main/
└── java/
└── com/
└── myproject/
├── Main.java
└── Greet.java
You can create the project structure manually or use the following command to create the project structure:
mkdir -p src/main/java/com/myproject
-
Create a file named
build.gradle
and copy the following code into it:plugins { id 'java' } task execute(type: JavaExec, dependsOn: 'compileJava') { main = 'com.myproject.Main' classpath = sourceSets.main.runtimeClasspath }
-
Main.java
andGreet.java
are located in thesrc/main/java/com/myproject
directory.Main.java:
public class Main { public static void main(String[] args) { HelloWorld.sayHello(); } }
Greet.java:
package com.myproject; public class Greet { public static void sayHello() { System.out.println("Hello, World!"); } }
-
Run the program using the
gradle
command:gradle execute