Develop with Java on Ubuntu¶
This tutorial provides basic guidance on how to use of the Java toolchain for development on Ubuntu. It shows how to create a ‘Hello, world!’ program and explains how to build projects using Gradle or Maven.
For instructions on how to install Java and related tooling, including IDEs, see the dedicated guide on How to set up a development environment for Java on Ubuntu. This article assumes that tooling suggested in that article has been installed.
javac vs build systems¶
javac is the actual compiler, but developers usually use build systems to compile, build, and package Java projects. Gradle and Maven are popular tools for building Java projects.
To use javac directly, refer to the example Compiling Java application using javac directly.
Creating a Java project using Maven¶
Setting up and building a new Java project using the Apache Maven tool.
Prerequisites
Java Development Kit; refer to Installing Java Development Kit.
Apache Maven:
To install Maven and the default Java Development Kit from the Ubuntu archive, use:
sudo apt install maven
Alternatively, download Maven from Apache Maven Project and follow the installation instructions: Installing Apache Maven.
Maven project
Create a new Java project using the
archetype:generateMaven sub-command:mvn archetype:generate -DgroupId=com.yourcompany \ -DartifactId=helloworld -Dversion=1.0-SNAPSHOT \ -Dpackage=com.yourcompany.helloworld \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=1.4Press Enter when prompted to confirm your selection.
This creates a new project using Maven Quickstart Archetype.
Maven sets up a basic project structure:
dev@ubuntu:~$tree. └── helloworld ├── pom.xml └── src ├── main │ └── java │ └── com │ └── yourcompany │ └── helloworld │ └── App.java └── test └── java └── com └── yourcompany └── helloworld └── AppTest.javaThat includes a ‘Hello World’ application and a unit test:
src/main/java/com/yourcompany/helloworld/App.java¶package com.yourcompany.helloworld; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
src/test/java/com/yourcompany/helloworld/AppTest.java¶package com.yourcompany.helloworld; import static org.junit.Assert.assertTrue; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { /** * Rigorous Test :-) */ @Test public void shouldAnswerWithTrue() { assertTrue( true ); } }
Change to the project directory:
cd helloworld
Build and package the application:
mvn -Dmaven.compiler.release=8 package
Note
Notice the
-Dmaven.compiler.release=8option. Themaven-archetype-quickstartarchetype generates a project that targets Java 7, which is no longer supported by the Java 21 LTS release. The project target can be changed by updating themaven.compiler.targetandmaven.compiler.sourceproperties in thepom.xmlfile.This builds and runs unit tests.
Run the application:
dev@ubuntu:~$java -cp target/helloworld-1.0-SNAPSHOT.jar com.yourcompany.helloworld.AppHello World!
Creating a Java project using Gradle¶
Setting up and building a new Java project using the Gradle build tool.
Prerequisites
Java Development Kit; refer to Installing Java Development Kit.
Gradle:
Download Gradle from the Gradle Releases page and follow the provided instructions: Installing manually.
Note
Gradle introduced Java 21 support in version 8.5.
Alternatively, to install the community-maintained Gradle snap, run:
sudo snap install gradle
The snap provides Gradle version 7, which does not support Java 21.
Note
The snap requires setting up the
JAVA_HOMEvariable. For example:export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
Gradle project
Create a Java project using the
gradle initcommand:mkdir helloworld cd helloworld cd helloworld gradle init \ --type java-application \ --dsl kotlin \ --test-framework junit-jupiter \ --package com.yourcompany.helloworld \ --project-name helloworld \ --no-split-project \ --no-incubatingPress Enter when prompted for the Java version.
Gradle sets up a basic project structure:
dev@ubuntu:~$tree. └── helloworld-gradle ├── app │ ├── build.gradle.kts │ └── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── yourcompany │ │ │ └── helloworld │ │ │ └── App.java │ │ └── resources │ └── test │ ├── java │ │ └── com │ │ └── yourcompany │ │ └── helloworld │ │ └── AppTest.java │ └── resources ├── gradle │ ├── libs.versions.toml │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle.ktsThat includes a ‘Hello World’ application and a unit test:
app/src/main/java/com/yourcompany/helloworld/App.java¶/* * This Java source file was generated by the Gradle 'init' task. */ package com.yourcompany.helloworld; public class App { public String getGreeting() { return "Hello World!"; } public static void main(String[] args) { System.out.println(new App().getGreeting()); } }
app/src/test/java/com/yourcompany/helloworld/AppTest.java¶/* * This Java source file was generated by the Gradle 'init' task. */ package com.yourcompany.helloworld; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class AppTest { @Test void appHasAGreeting() { App classUnderTest = new App(); assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); } }
Build and run the project using the generated Gradle Wrapper:
dev@ubuntu:~$./gradlew runDownloading https://services.gradle.org/distributions/gradle-8.10.1-bin.zip .............10%.............20%.............30%.............40%.............50%.............60%.............70%.............80%.............90%.............100% > Task :app:run Hello World! BUILD SUCCESSFUL in 31s 2 actionable tasks: 2 executed
Compiling Java application using javac directly¶
Compiling a Java application directly using the javac tool.
Prerequisites
Java Development Kit; refer to Installing Java Development Kit.
Procedure
Create a ‘Hello World’ application in a file named
App.java:public class App { public String getGreeting() { return "Hello World!"; } public static void main(String[] args) { System.out.println(new App().getGreeting()); } }
Compile the class file in the
outdirectory:javac App.java -d out
Execute the program:
dev@ubuntu:~$java -cp out/ AppHello World!
Running Java application as a shebang script¶
Running a Java application as a script with the java interpreter specified using the ‘shebang’ (#!) interpreter directive.
Prerequisites
Java Development Kit; refer to Installing Java Development Kit.
Procedure
Create a ‘Hello World’ application in a file named
Appand include the interpreter directive on the first line:App¶#!/usr/bin/java public class App { public String getGreeting() { return "Hello World!"; } public static void main(String[] args) { System.out.println(new App().getGreeting()); } }
Note
This file does not have a
.javaextension.Make the file executable:
chmod +x App
Run the application:
dev@ubuntu:~$./AppHello World!