Quick revision
The short answer
The launcher finds classes on the class path given by -cp (or --class-path), a list of directories and jars separated by : on Unix and ; on Windows, where dir/* adds every jar in that directory; the CLASSPATH variable is used only when no -cp is given, and java -jar ignores both and uses the jar manifest's Class-Path. -D sets system properties, JAVA_TOOL_OPTIONS is read by the JVM itself and processed before the command line, JDK_JAVA_OPTIONS is added only by the java launcher, and @argument files hold long option lists. When an option appears more than once, the later one generally wins, so the command line normally beats the environment variables.
What to remember
In this answer, we saw how the java launcher builds the class path from -cp or CLASSPATH, what the dir/* wildcard does and does not include, and why java -jar ignores both and uses the manifest. We covered -D system properties and Spring Boot's view of them, the difference between JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS, argument files, and the rule that later options generally win, so the explicit command line beats the environment.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The launcher finds classes on the class path given by -cp (or --class-path), a list of directories and jars separated by : on Unix and ; on Windows, where dir/* adds every jar in that directory; the CLASSPATH variable is used only when no -cp is given, and java -jar ignores both and uses the jar manifest's Class-Path. -D sets system properties, JAVA_TOOL_OPTIONS is read by the JVM itself and processed before the command line, JDK_JAVA_OPTIONS is added only by the java launcher, and @argument files hold long option lists. When an option appears more than once, the later one generally wins, so the command line normally beats the environment variables.
Why this question is asked
Most of the time a build tool or a container image starts our Java process for us, so we rarely think about how the java command finds classes and settings. Then something surprising happens: a property seems to be ignored, a jar is not found, or the process prints Picked up JAVA_TOOL_OPTIONS and behaves differently in one environment. Interviewers ask this to check that we understand the launcher's inputs and which one wins. In this answer, we will go through the class path and its wildcard, the CLASSPATH variable and java -jar, system properties, the two options variables, argument files, and finally the precedence rules.
1The Class Path and Its Wildcard
The class path is the list of places where the application class loader looks for classes and resources. We give it with -cp, -classpath or --class-path, all meaning the same thing. Each entry is a directory that contains class files in package folders, or a jar file. Entries are separated by a colon on Linux and macOS and by a semicolon on Windows, and they are searched in order, so the first match wins.
An entry that ends in /* is a wildcard: it adds every file ending in .jar or .JAR in that directory. It does not search subdirectories, and it does not add loose class files in the directory, only jars. The order of the jars it adds is not specified, which matters if two jars contain the same class. Notice the quotes in the example below: without them, the shell would expand the * itself into a space-separated list, which breaks the command.
2CLASSPATH and java -jar
The CLASSPATH environment variable is the old way to set the class path. The launcher uses it only when there is no -cp option on the command line; as soon as we pass -cp, CLASSPATH is ignored completely, not merged. If neither is set, the class path is just the current directory.
java -jar app.jar is different again. The launcher reads the Main-Class from the jar's manifest, and the class path becomes the jar itself plus any entries in the manifest's Class-Path attribute. Both -cp and CLASSPATH are ignored. This surprises people who try java -cp extra.jar -jar app.jar and wonder why extra.jar is not found. A Spring Boot executable jar goes one step further: its manifest points to Boot's own launcher, which builds a class loader for the nested jars inside BOOT-INF/lib.
3System Properties With -D
A system property is a key and value that the JVM holds and code reads with System.getProperty. We set one on the command line with -Dname=value, which must come before the main class or -jar, because anything after them is passed to the application as its arguments.
Spring Boot also reads system properties as configuration, so -Dserver.port=8081 changes the port. In Boot's order of property sources, command-line arguments such as --server.port=8082 win over Java system properties, which win over operating system environment variables, which win over application.properties. Within the JVM, if the same -D appears twice, the later one wins.
4JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS and @Argument Files
JAVA_TOOL_OPTIONS is an environment variable read by the JVM itself while it starts, not by the java command. Because of that it also affects every other program that starts a JVM, including jcmd, jstack, keytool and Maven. Its options are processed before the command-line options, so the command line overrides them, and the JVM announces it on standard error with Picked up JAVA_TOOL_OPTIONS: followed by the value. It is the standard way for tools and platforms to inject options, such as a monitoring agent, without touching the start command.
JDK_JAVA_OPTIONS, added in JDK 9, is read only by the java launcher. Its content is inserted before the command-line arguments, so again the explicit command line wins, and the launcher prints NOTE: Picked up JDK_JAVA_OPTIONS. Because only the java launcher reads it, it does not leak into jcmd or other tools.
Argument files, also from JDK 9, solve the problem of very long command lines. We write options into a file and pass @file. The launcher reads the file as if its content were typed at that position.
5Which Value Wins?
Putting it together, the JVM sees one long list of options, and for most options the last occurrence wins. JAVA_TOOL_OPTIONS comes first, then the launcher's command line, which is JDK_JAVA_OPTIONS followed by the real arguments, with @files expanded in place. So an explicit command-line option beats both variables, and within the command line, later beats earlier. Options that add rather than replace, such as -javaagent, accumulate instead of overriding, so the same agent given in two places is loaded twice. There is also an older, unsupported _JAVA_OPTIONS variable that some JVMs read after the command line; avoid it, and check your vendor if you find it in old scripts.
- JAVA_TOOL_OPTIONSread by the JVM itself; affects every JVM-based tool
- JDK_JAVA_OPTIONSinserted by the java launcher only
- Command line and @argfilesexpanded in place, left to right
- Main class or -jareverything after it goes to the application
4 layers, from the top down. Step through them one at a time.
For a repeated option, the last value seen is the one used.
6Follow-up questions
Why can JAVA_TOOL_OPTIONS surprise you in containers?
Base images and buildpacks often set it, for example to add memory flags or an APM agent, so the service runs with options that appear in no Dockerfile or Helm chart the team wrote. Because the JVM reads it, it also applies to jcmd or jstack when we exec into the container, so a debug agent with a fixed port can make those tools fail with an address already in use error, and an agent can be loaded twice. Its Picked up line goes to standard error, which can break scripts that parse java -version output. We check it with env and with jcmd VM.command_line.
How do we see which options and class path a running JVM really used?
jcmd <pid> VM.command_line prints the arguments the JVM received (options from the environment variables may be listed separately, so also check env inside the container), jcmd <pid> VM.system_properties shows java.class.path and every property, and jcmd <pid> VM.flags shows the effective JVM flags after defaults and ergonomics. Logging RuntimeMXBean.getInputArguments() at startup does the same from inside the application.
Does the module path follow the same rules?
It is given separately with --module-path or -p, and it holds modules rather than a flat list of classes. The CLASSPATH variable does not apply to it, and argument files and the options variables work the same way for it.
7Conclusion
In this answer, we saw how the java launcher builds the class path from -cp or CLASSPATH, what the dir/* wildcard does and does not include, and why java -jar ignores both and uses the manifest. We covered -D system properties and Spring Boot's view of them, the difference between JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS, argument files, and the rule that later options generally win, so the explicit command line beats the environment.
Open with
The launcher takes the class path from -cp or --class-path, a list of directories and jars separated by colons on Unix and semicolons on Windows, searched in order.
Then, if they want more
An entry like lib/* adds every jar in that directory, but not subdirectories and not loose class files, and I quote it so the shell does not expand it. The CLASSPATH variable is only used when there is no -cp at all, and java -jar ignores both: it uses the jar itself plus the Class-Path in its manifest.
System properties come from -Dname=value before the main class, read with System.getProperty, and Spring Boot treats them as configuration, below command-line arguments but above environment variables. Then there are two option variables. JAVA_TOOL_OPTIONS is read by the JVM itself, so it affects every JVM tool, including jcmd, and prints Picked up JAVA_TOOL_OPTIONS on standard error.
JDK_JAVA_OPTIONS is only read by the java launcher and inserted before the command-line arguments. For long lists, an @argfile is expanded in place. As for precedence, the JVM processes JAVA_TOOL_OPTIONS first and then the command line, and for a repeated option the last one wins, so an explicit command-line option beats both variables. Additive options like -javaagent accumulate instead. In containers I check JAVA_TOOL_OPTIONS first, because base images often set it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the java launcher finds classes and configuration: the -cp option and its...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/28653047d5/