Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mix --class-path and --module-path in javac (JDK 9)?

When I compile a module that depends on other modules I've compiled previously I have to specify the --module-path <directory> option. This makes modules I depend on visible.

But at the same time I would also like to make some non-modular Jar files visible. However if don't make them automatic modules and just specify the --class-path some.jar right next to --module-path <directory>, then javac seems to ignore the claspath and throws "package yyy not found" and other "not found" errors.

I can understand that using --class-path and --module-path at the same (compile) time is illegal, but javac does not warn me against it in any way.

like image 727
malloc4k Avatar asked Sep 18 '17 21:09

malloc4k


People also ask

What is classpath in javac?

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

What is Module Path and Classpath in Eclipse?

A ClassPath is a sequence of classes and packages (or JARs) which are user-defined classes and built-in classes. JVM or Java Compiler requires this to compile the application or classes. A ModulePath is a sequence of Modules (which are provided in a Folder format or JAR format).

Which commands compiles a Java program in which the class is defined as hello?

The javac command reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files.


1 Answers

You can use class path and module path in parallel, but there are a few details to consider.

Dependency Module Path ~> Class Path

Explicit modules (JARs with a module descriptor on the module path) can not read the unnamed module (JARs on the class path) - that was done on purpose to prevent modular JARs from depending on "the chaos of the class path".

Since a module must require all of its dependencies and those can only be fulfilled by other named modules (i.e. not JARs on the class path) all dependencies of a modular JAR must be placed on the module path. Yes, even non-modular JARs, which will then get turned into automatic modules.

The interesting thing is that automatic modules can read the unnamed module, so their dependencies can go on the class path.

Dependency Class Path ~> Module Path

If you compile non-modular code or launch an application from a non-modular JAR, the module system is still in play and because non-modular code does not express any dependencies, it will not resolve modules from the module path.

So if non-modular code depends on artifacts on the module path, you need to add them manually with the --add-modules option. Not necessarily all of them, just those that you directly depend on (the module system will pull in transitive dependencies) - or you can use ALL-MODULE-PATH (check the linked post, it explains this in more detail).

like image 162
Nicolai Parlog Avatar answered Sep 22 '22 00:09

Nicolai Parlog