Java Cheatsheet - Java & JVM Command Reference

This reference is for developers writing Spring Boot/WAR apps or operating them in production: from compiling and packing with javac/java/jar, one-command builds with Maven/Gradle, heap sizing with -Xms/-Xmx, G1 GC and GC logging, to grabbing threads and heap with jstack/jmap/jstat when the service stalls. Unlike a generic command list, it is organized around building the service, running it stably, tuning it faster, and debugging its failures. After reading you should be able to complete a build → deploy → GC tune → deadlock hunt cycle on your own.

Languages·34 commands·Last updated 2026-07-21
javajvmmavengradle

Compile & Run 6

javac Main.java
Compile a single Java file
java -cp .:lib.jar Main
Run with classpath (Linux/Mac)
java -cp ".;lib.jar" Main
Run with classpath (Windows)
jar cvf app.jar *.class
Package classes into a jar
jar tf app.jar
List jar contents
jar xf app.jar
Extract a jar

Maven Commands 8

mvn clean
Clean the target directory
mvn compile
Compile main sources
mvn test
Run unit tests
mvn package
Package (jar/war)
mvn install
Install to local repo
mvn dependency:tree
Show the dependency tree
mvn -U clean package
Force snapshot update and package
mvn spring-boot:run
Run a Spring Boot project

Gradle Commands 6

./gradlew build
Build the project
./gradlew clean
Clean the build dir
./gradlew test
Run tests
./gradlew bootRun
Run Spring Boot project
./gradlew dependencies
Show dependencies
./gradlew tasks
List all available tasks

JVM Flags & Tuning 6

java -Xms512m -Xmx2g -jar app.jar
Set heap min 512M / max 2G
java -XX:+UseG1GC -jar app.jar
Use the G1 garbage collector
java -XX:+PrintGCDetails -Xloggc:gc.log
Log GC details to a file
java -XX:+HeapDumpOnOutOfMemoryError
Dump heap on OutOfMemoryError
java -XX:HeapDumpPath=/tmp/dump.hprof
Set the heap dump path
java -verbose:gc
Print brief GC info

JVM Troubleshooting 8

jps -l
List all Java processes
jstack <pid>
Dump thread stack (find deadlocks)
jmap -heap <pid>
Inspect heap usage
jmap -dump:format=b,file=dump.hprof <pid>
Dump a heap snapshot
jstat -gcutil <pid> 1000
Print GC stats every second
jinfo <pid>
View JVM flags
jconsole
Graphical monitoring tool
visualvm
Visual JVM profiler

Tips

  • With jstack, look for "Found one Java-level deadlock" to spot deadlocks.
  • Heap dump files can be huge - check disk space in production first.
  • G1 GC suits large heaps (>6G); Parallel GC gives higher throughput on small heaps.

FAQ

How do I debug a hung Java program?

Start with jps -l to list Java processes and get the PID, then jstack <pid> to dump thread stacks and look for "Found one Java-level deadlock" or threads stuck in BLOCKED/WAITING. For memory, jmap -heap <pid> shows configuration and usage against the ceiling; if memory is the issue, export a snapshot with jmap -dump:format=b,file=dump.hprof <pid> and analyze it offline.

What is the difference between mvn install and mvn package?

package produces the jar/war into the project target directory. install additionally places it in the local ~/.m2 repository so other modules on the same machine can depend on it by coordinates. A single module only needs package; in a multi-module build, modules that others depend on need install. Prefix builds with mvn clean so stale classes from the previous run do not leak in.

When should I use Gradle instead of Maven?

Base it on ecosystem rather than speed. New projects around Spring Boot 3, Kotlin and Android generally favor Gradle for its plugin ecosystem and incremental build work, while many existing large enterprise projects remain on Maven. Do not mix both build tools in one repository — the configuration fragments. To inspect resolved versions, ./gradlew dependencies plays the role of mvn dependency:tree.

How should JVM memory flags be set?

Set -Xms512m -Xmx2g so the initial heap equals the maximum, avoiding repeated resizing that causes pauses. Equal values let the JVM request memory from the OS in one go and skip dynamic adjustment. For the collector, G1 is sufficient for most cases in modern JDKs; specify it explicitly with -XX:+UseG1GC if you need to.

How do I tell whether the application is stuck in Full GC?

Run jstat -gcutil <pid> 1000 to print generation usage and GC counts each second, and watch whether old-gen usage approaches its ceiling while the Full GC counter keeps climbing. You can also add -XX:+PrintGCDetails to send GC detail to the log. Once confirmed, look at memory configuration and possible leaks before tuning anything.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us