Java command line options control how the JVM starts and how your program runs. You pass them on the command line between the java command and the class or JAR name: java [options] ClassName [args] or java [options] -jar app.jar [args]. Options that begin with -X or -XX go to the JVM itself, options that define properties like -Dkey=value are read inside your code through System.getProperty, and anything after the class or JAR name is handed to your main(String[] args) method untouched. Getting that ordering right is the single most common source of confusion, so it is worth learning carefully.

What You'll Need

  • A Java Development Kit (JDK) installed, with the java command available on your PATH. You can confirm this by running java -version in a terminal.
  • A compiled class or a runnable JAR to run. For experimentation, a one-line Hello World class is enough.
  • A terminal: Command Prompt or PowerShell on Windows, Terminal on macOS or Linux.
  • Some options only take effect if the JVM was built with a feature; such as certain garbage collectors; so keep the vendor's documentation handy.

Step-by-Step

  1. Check what your JVM supports. Run java -version and note the output. Run java --help for the list of standard options. For JVM-specific options, use java -X, which prints the non-standard options available on that build. Labels and available flags can differ slightly by version, vendor and platform, so trust the output on your machine over any generic guide.

  2. Set the memory limits with -Xmx and -Xms. -Xmx caps the maximum heap, and -Xms sets the initial heap. For example, java -Xms256m -Xmx1g -jar app.jar starts with 256 MB and allows growth up to 1 GB. Set -Xms and -Xmx to the same value if you want a predictable footprint and are willing to pay a slower start while the heap commits. Keep both below the physical memory of the machine so the operating system is not starved.

  3. Pass application settings as system properties. Use -Dname=value, for example java -Dserver.port=8081 -jar app.jar. Your code reads it with System.getProperty("server.port"). If the value contains spaces, quote the whole argument on the shell, for example -D"app.name=My App". Properties set on the command line override those loaded from a properties file only if your code reads them in that order; Java does not do this automatically.

  4. Choose a garbage collector where it matters. Common options are -XX:+UseG1GC, -XX:+UseSerialGC, -XX:+UseParallelGC and, on supported builds, -XX:+UseZGC. Only use a collector your JDK lists in its documentation. On larger heaps and latency sensitive services, G1 or ZGC are typical choices; small tools often run fine on the default.

  5. Enable logging of what the JVM does. -XX:+PrintFlagsFinal -version prints the effective value of every JVM flag, which settles arguments about whether your -Xmx was actually applied; search the output for the flag's name and look at the value column. -Xlog:gc (on recent versions) or -verbose:gc shows garbage collection activity. Logging options are the ones most likely to change wording between versions, so check java --help first.

  6. Add assertions and debugging flags during development. -ea enables assertions, so assert statements actually run; they are off by default. -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 opens a debugger port, which tools such as an IDE attach to. Never leave a debug port open on a machine reachable from outside your own environment.

  7. Put the class and the program arguments last. Everything after the main class or JAR name belongs to your program. In java -Xmx512m -jar app.jar --mode fast file.txt, only --mode fast file.txt arrives in args. If you put -Xmx512m after the JAR name, it is silently treated as an application argument and the memory limit is not applied. This is easy to miss because nothing errors.

  8. Move long option lists into a file. If the command line grows unwieldy, you can control many settings through an argument file, using @args.txt to read arguments from a file, one or more per line, and pass the whole set as java @args.txt -jar app.jar. Confirm the exact behaviour in your JDK's java tool documentation before relying on it in scripts.

  9. Document the options you use. In a team or on a server, put the exact command in a start script or a README, including memory limits. Options that live only in someone's shell history are lost at the next deploy, and a service that ran with a 2 GB heap by accident will fail under load in a way that looks like an application bug.

Common Mistakes

  • Putting JVM options after the class name. They become program arguments. Check that every -X, -XX and -D option comes before the class or JAR.
  • Assuming -Xmx is the total memory the process uses. It is only the heap. Thread stacks, metaspace, code cache, direct buffers and native libraries add on top, so a process with -Xmx1g may use noticeably more memory.
  • Setting -Xmx larger than available RAM. The machine starts swapping or the OS kills the process. Size the total across all Java processes, not each one in isolation.
  • Ignoring shell quoting. A property value with spaces or an ampersand splits into several arguments. Quote each -D argument in the shell.
  • Using -XX flags copied from another JDK. Unrecognised -XX flags can make the JVM refuse to start. Standard options are portable; vendor-specific ones are not.
  • Deploying with a debug agent enabled. Remove development-only flags from production start scripts.

Tips

  • Use -XX:+PrintFlagsFinal -version as your first check whenever you doubt that an option took effect; the value shown is the one the JVM will use.
  • Prefer environment variables or a properties file for values that change between environments, such as database URLs, and reserve -D for a small, stable set of overrides.
  • Keep -Xms and -Xmx close together in containerised services so the process fits its memory budget predictably.
  • Name your start scripts after the service and include a comment explaining each non-default flag, so the next person knows why it is there.

Troubleshooting

The JVM prints "Unrecognized option" and exits. The flag does not exist on this JDK, or you typed it wrong. Run java -X and java --help on the same machine to see the valid spelling.

The program never sees your property. Check ordering first, then quoting. Print System.getProperty("name") early in your program to confirm what arrived.

The application runs out of memory despite a large -Xmx. Look at the failure text: heap exhaustion, metaspace exhaustion, or a native out-of-memory message point to different fixes. Heap errors mean raising -Xmx or fixing a leak; native errors often mean the container or OS limits the process regardless of the flag.

The process is killed by the operating system. That is usually the OS memory killer, not the JVM. Reduce the heap and check how many Java processes share the machine.

Nothing changes when you edit the start script. Restart the process. JVM options are read once at startup and cannot be changed at runtime.

Frequently Asked Questions

Where do JVM options go in the java command?

Before the class name or the -jar and JAR path. Anything after the class or JAR is passed to your program's main method.

What is the difference between -D and -X options?

-Dkey=value sets a system property that your code reads through the Java API. -X options configure the virtual machine itself, such as memory and garbage collection.

Do command line options override values in a properties file?

Not automatically. They are a separate source. Your code decides which source wins, so check how the application loads its configuration.

Can these options differ between JDKs?

Yes. -XX and logging flags vary by version and vendor, and some are removed over time. Standard options are the most portable.

Is -Xmx the total memory my program can use?

No. It caps the heap only. Total process memory also includes thread stacks, metaspace and native memory.