user2779311
user2779311

Reputation: 1874

Calling a Non Module Class from a Module Class in java 9

I am trying to call a non-module class from a module class. I have created a folder structure

moduledemo > allclasses > moduleC > packageC > MyMethods.class

is the path to my module class file

moduledemo > moduleC > packageC > MyMethods.java 

and

moduledemo > nomodule > packageD > DemoNoModule.class

is the no module class that I am calling from MyMethods.java

I am able to compile the DemoNoModule file. I am able to compile MyMethods.java into allclasses folder moduleC.

When I am running MyMethods I am getting error moduleC not found. Can anyone update? I am using the following command to run

java --module-path allclasses -m moduleC/packageC.MyMethods

Both files code -> Non-Module Class

package packageD;

public class DemoNoModule {
    public void showD() {
        System.out.println("this is show of D in No Module");
    }
}

Module class calling class

package packageC;
import packageD.*;

public class MyMethods {
    public static void main(String s[]) {
        DemoNoModule d=new DemoNoModule();
        d.showD();
    }
}

Module info in module C

module moduleC {    
    exports packageC; 
}

enter image description here

Upvotes: 5

Views: 3193

Answers (2)

Naman
Naman

Reputation: 31868

On one hand, the moduleC(mind improving naming?) is a named module.

While on another, the "no module class" termed by you is nothing but as stated by Alan a class present on the classpath. The classes present on the classpath during the execution are part of an unnamed module in JPMS.

Quoting the documentation further:-

The unnamed module exports all of its packages. This enables flexible migration... It does not, however, mean that code in a named module can access types in the unnamed module. A named module cannot, in fact, even declare a dependence upon the unnamed module.

This is intentional to preserve the reliable configuration in the module system. As stated further :

If a package is defined in both a named module and the unnamed module then the package in the unnamed module is ignored. This preserves reliable configuration even in the face of the chaos of the class path, ensuring that every module still reads at most one module defining a given package.

Still, to make use of a class from the unnamed module in your named module moduleC, you can follow the suggestion of making use of the flag to add ALL-UNNAMED module to be read by modules on the module path using the following command:

--add-reads <source-module>=<target-module> // moduleC=ALL-UNNAMED

As a special case, if the <target-module> is ALL-UNNAMED then readability edges will be added from the source module to all present and future unnamed modules, including that corresponding to the class path.

PS: Do take into consideration the highlighted portion(above) of the documentation as you do so.

Also note the long-term solution would be to revise your design here, for which you can plan to move your code in the class DemoNoModule into an explicit module or package it separately to be converted into an automatic module.

Upvotes: 1

zlakad
zlakad

Reputation: 1384

Java 9 programs are supposed to be modular. That is how I understood jigsaw in JDK-9. So, IMHO, you'll have to 'wrap' your packageD in another module and in the module-info for moduleC write requires moduleD. Also moduleD should export packageD. ALL-UNNAMED is added for backward compatibility, and I suppose it will be removed in some point of Java evolution.

Upvotes: 0

Related Questions