javajava-9java-platform-module-systemmodule-path

modulepath environment variable in Java similar to CLASSPATH


In previous versions of Java if you had for example a user-defined package org.mypackage in windows with directory structure

D:\myprogram\
      |
      ---> org\  
            |
            ---> mypackage\
                     |
                     ---> HelloWorld.class       
                     ---> SupportClass.class   
                     ---> UtilClass.class     

you make the JVM aware of the package by using the CLASSPATH environment variable.

For example:

set CLASSPATH=D:\myprogram

Does set MODULEPATH command instead make the JVM aware of the location of user-defined modules in the latest versions of Java ?

If so why is classpath command still around and what purpose does it serve now?


Solution

  • No, the jdk does not read any MODULEPATH environment variable or similar.

    The MODULEPATH only exists as a command-line argument --module-path, but not as an environment variable.

    See http://mail.openjdk.java.net/pipermail/jigsaw-dev/2016-September/009345.html

    Unlike CLASSPATH, there is no equivalent MODULEPATH to get the option off of the command line.

    Also see the linked question Is it possible to mix --class-path and --module-path in javac (JDK 9)? for more details on the differences between boths paths.

    If you put a normal jar on the modulepath, it becomes an automatic module, exporting its packages (and reserving those packages for itself).

    While you can put any normal jar file into the MODULEPATH, there is a restriction: If 2 jar files contain the same package, then java will not allow both of them in the modulepath. That's why Java still has the classpath, because several existing jar files still have overlapping packages, and it is not possible to put them all on the MODULEPATH. They have to be cleaned up first.

    In the far future, when all libraries have been migrated to modules (or cleaned up), maybe classpath can be removed.