Create Java console app
Install Java, Gradle on WSL2 (Ubuntu) and VS Code extensions
- You can skip this section if you have already setup Java development environment.
- Follow this document to install Java, Gradle build tool and VS Code extensions.
Create a new Java console app with Gradle
- Open a terminal and execute the following commands to reate a project folder:
Terminal window mkdir democd demo - Init Java project with Gradle
gradle init
- Then select the following options and press enter to create a new Java application project.
Terminal window Select type of project to generate:1: basic2: application3: library4: Gradle pluginEnter selection (default: basic) [1..4] 2Select implementation language:1: C++2: Groovy3: Java4: Kotlin5: Scala6: SwiftEnter selection (default: Java) [1..6] 3Split functionality across multiple subprojects?:1: no - only one application project2: yes - application and library projectsEnter selection (default: no - only one application project) [1..2] 1Select build script DSL:1: Groovy2: KotlinEnter selection (default: Groovy) [1..2] 1Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]Select test framework:1: JUnit 42: TestNG3: Spock4: JUnit JupiterEnter selection (default: JUnit Jupiter) [1..4] 3Project name (default: demo):Source package (default: demo): - The
init
task generates the project with the following structure:Terminal window tree .├── app│ ├── build.gradle│ └── src│ ├── main│ │ ├── java│ │ │ └── demo│ │ │ └── App.java│ │ └── resources│ └── test│ ├── groovy│ │ └── demo│ │ └── AppTest.groovy│ └── resources├── gradle│ └── wrapper│ ├── gradle-wrapper.jar│ └── gradle-wrapper.properties├── gradlew├── gradlew.bat└── settings.gradle
Run Java console app
- Execute the following comand to run the console app
./gradlew run
- You will find
Hello World!
message in a terminal as following:Terminal window > Task :app:runHello World!BUILD SUCCESSFUL2 actionable tasks: 2 executed
Run a unit test case
- Execute the following comand to run a unit test case
Terminal window ./gradlew test