Question
Why am I seeing a Java warning about the system modules path not being set when using the -source 11 option?
javac --source 11 MyClass.java
Answer
When compiling Java code using the `-source` option to specify Java 11, you may encounter a warning stating that the system modules path is not set. This warning is primarily related to the modularization introduced in Java 9, leading to the need for specifying module paths correctly during compilation.
// Example command to compile with module path
javac --source 11 --module-path /path/to/modules MyClass.java
Causes
- Using an outdated version of the Java compiler (javac) that does not correctly support Java modules.
- Incorrect Java SDK setup or installation, causing the modules path to be unrecognized.
- Running compile commands without setting the `--module-path` when your Java source code uses modules.
Solutions
- Ensure you are using a compatible version of JDK (Java Development Kit) that supports the modular system introduced in Java 9.
- Explicitly set the modules path in the compile command using the `--module-path` option.
- For example, use: `javac --source 11 --module-path /path/to/modules MyClass.java`.
Common Mistakes
Mistake: Not specifying the `--module-path` when using modules in Java 11 and above.
Solution: Always include the `--module-path` followed by the correct path of the modules when compiling with modules.
Mistake: Using an outdated Java version for projects targeting more recent Java features.
Solution: Upgrade to at least JDK 11 to properly support Java 11 features including modules.
Helpers
- Java warning
- system modules path
- -source 11 option
- Java compiler warning
- Java 11
- javac options