Search Authority

Master Running Java on Command Line: Pro Tips & Commands

Running Java on the command line lets you start, manage, and troubleshoot JVM applications without an IDE. The command line gives you precise control over classpaths, JVM option...

Mara Ellison Jul 25, 2026
Master Running Java on Command Line: Pro Tips & Commands

Running Java on the command line lets you start, manage, and troubleshoot JVM applications without an IDE. The command line gives you precise control over classpaths, JVM options, and build tool integration, which is essential for automation and production workflows.

This guide walks through practical steps, common commands, and configuration tips so you can confidently execute Java programs, scripts, and services directly from the terminal.

Command Purpose Typical Use Case Key Option
javac Compile Java source files to bytecode Build Java projects from .java files -d, -source, -target
java Launch the Java runtime and execute a class Run compiled applications and standalone jars -cp, -Xmx, -D
jar Package and manage JAR files Create executable jars and view contents -cvf, -xf, -e
jlink Create custom runtime images Reduce size for self-contained deployment --module, --output
jcmd Send diagnostic commands to a running JVM Thread dumps, heap inspection, GC control GC.run, Thread.print

Compiling Java Source Files from Terminal

The javac command translates human readable Java files into platform independent bytecode. You typically run javac with file names or wildcards to compile one or several source files at once. This step is mandatory before you can run Java on the command line unless you are using a runtime image produced by jlink.

You can control output directories, language level, and encoding with standard options. Keeping consistent compiler settings across environments reduces build surprises and makes your builds reproducible.

Use relative paths or module descriptors when working inside structured projects to ensure dependencies are resolved correctly during compilation.

Executing Programs with the Java Command

The java command starts the JVM and loads a main class or an executable jar. Properly defining the classpath and module path is essential so the runtime can locate application classes and libraries. You can pass system properties, adjust memory settings, and control runtime behavior through JVM flags.

For simple applications, java -jar is convenient, while modular projects may require module and classpath configuration. Understanding how java resolves main class and dependencies helps you avoid common runtime errors.

Combining java with scripts or process managers makes it easy to integrate Java into automated pipelines and service workflows.

Working with JAR Files and Packaging

A JAR file bundles compiled classes, resources, and an optional manifest that can declare a main class for execution. The jar command lets you create, extract, and update JAR archives from the command line. This is useful for packaging applications and inspecting contents without external tools.

Executable JARs simplify deployment because they carry entry point information, reducing the need for manual classpath configuration. You can sign JARs, specify version information, and control compression to optimize distribution.

When working with dependencies, you can either pack them into a fat jar or manage multiple jars on the classpath or module path.

Troubleshooting and Runtime Diagnostics

When a Java process behaves unexpectedly, command line tools provide insight into threads, memory, and runtime metrics. jcmd and jstack help you capture thread dumps, while jmap supports heap inspection. These tools are invaluable for diagnosing hangs, memory leaks, or high CPU usage in production environments.

You can enable detailed GC logging, configure error files, and set heap size limits directly from the command line. Consistent logging and structured output make it easier to analyze issues over time.

Building simple diagnostic scripts with java, jcmd, and jstack streamlines repetitive troubleshooting tasks and improves response speed.

FAQ

Reader questions

How do I run a JAR file from the command line with a specific Java version?

Use the full path to the desired Java executable or set JAVA_HOME and invoke java -jar your-application.jar to ensure the correct runtime version is used.

What does ‘Error: Could not find or load main class’ mean when running java?

This usually indicates a classpath or module path issue; verify that your class or module name is correct, that the classpath includes necessary directories and jars, and that the manifest specifies the main class if using -jar.

How can I pass system properties to a Java application started from the command line?

Use the -D flag before the main class name, for example java -Denv=prod -Dlog.level=info -jar app.jar to make those properties available to the JVM.

Can I run multiple Java processes on the same machine using different ports from the command line?

Yes, configure each process with distinct server ports and unique logging or data directories, and manage them with scripts or process supervisors to avoid conflicts.

Related Reading

More pages in this topic cluster.

How to Tell the Difference Between Silver and Aluminum (Silver vs Aluminum)

Spotting the difference between silver and aluminum helps you verify purchases, appraise items, and avoid overpaying for misidentified metals. While they look similar at first g...

Read next
Excel Keyboard Shortcut for Strikethrough: Easy Step-by-Step Guide

Mastering the Excel keyboard shortcut for strikethrough helps you track completed tasks, revisions, and action items without leaving the keyboard. This small efficiency habit sp...

Read next
Durham NC News Today: Latest Headlines & Updates

Durham NC news keeps the Research Triangle region informed about breakthrough healthcare, education, and downtown development. Local reporting connects residents and visitors to...

Read next