javac not enforcing module boundaries?

Peter Levart peter.levart at gmail.com
Mon Sep 14 08:22:09 UTC 2015


Hi,

I experimented a little with EA build and found javac behaving a little 
strange. Is this known or expected behaviour?

For example, having the following layout of sources:

modsrc/modA/module-info.java

     module modA {
         requires modB;
     }

modsrc/modA/pkgA/TestA.java

     package pkgA;

     import pkgB.TypeB;

     public class TestA {
         public static void main(String[] args) {
             TypeB b = new TypeB();
             b.getC().getD().helloD();
         }
     }

modsrc/modB/module-info.java

     module modB {
         exports pkgB;
         requires modC;
     }

modsrc/modB/pkgB/TypeB.java

     package pkgB;

     import pkgC.TypeC;

     public class TypeB {
         public TypeC getC() {
             return new TypeC();
         }
     }

modsrc/modC/module-info.java

     module modC {
         exports pkgC;
         requires modD;
     }

modsrc/modC/pkgC/TypeC.java

     package pkgC;

     import pkgD.TypeD;

     public class TypeC {
         public TypeD getD() {
             return new TypeD();
         }
     }

modsrc/modD/module-info.java

     module modD {
         exports pkgD;
     }

modsrc/modD/pkgD/TypeD.java

     package pkgD;

     public class TypeD {java -modulepath modout -m modA/pkgA.TestA
         public void helloD() {
             System.out.println("Hello from " + 
this.getClass().getModule());
         }
     }


and using the following command to compile them:

javac -modulesourcepath modsrc -d modout modsrc/modA/pkgA/TestA.java

They compile without error and produce the following output files:

modout/modA/module-info.class
modout/modA/pkgA/TestA.class
modout/modB/module-info.class
modout/modB/pkgB/TypeB.class
modout/modC/module-info.class
modout/modC/pkgC/TypeC.class
modout/modD/module-info.class
modout/modD/pkgD/TypeD.class


Running the above with the following command:

java -modulepath modout -m modA/pkgA.TestA


Gives the following runtime error:

Exception in thread "main" java.lang.IllegalAccessError: class 
pkgA.TestA (in module: modA) cannot access class pkgC.TypeC (in module: 
modC), modA cannot read modC
         at pkgA.TestA.main(modA@/TestA.java:8)


Which is expected. What I didn't expect is that javac did not figure 
this out correctly. Am I missing something and not using javac in the 
right way?


Regards, Peter



More information about the jigsaw-dev mailing list