Java, Maven and Gradle 25 cheatsheet

Java, Maven and Gradle 25: Compile and run

9 common Java, Maven and Gradle 25 tasks.

Check the Java version

Print the installed JDK version.

java --version

Compile a source file

Compile Hello.java to Hello.class.

javac Hello.java

Run a compiled class

Run the main method of a class in the current folder.

java Hello

Run a single source file directly

Compile and run a single file in one step, without javac.

java Hello.java

Run a jar

Run an executable jar that names its main class.

java -jar app.jar

Run with a classpath

Put a folder of jars and the current folder on the classpath.

java -cp "lib/*:." com.example.Main

Set the maximum heap size

Allow the JVM to use up to 2 GB of heap.

java -Xmx2g -jar app.jar

Open the interactive shell

Try out Java expressions without writing a class. Exit with /exit.

jshell

List a jar's contents

Show the files inside a jar.

jar tf app.jar

Was this page helpful?