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.

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