官术网_书友最值得收藏!

Compiling two modules

Now the module path (the src directory) has two modules. We can now run the javac command to compile all classes in both modules, since we are supplying src as the module source path.

Here, packt.sortutil is the simpler of the two modules. Since it doesn't have any external dependencies, this should just work like the packt.addressbook module did in the previous chapter. Let's look at the packt.addressbook module. Things are more interesting here. Since we've moved the sort related classes to the packt.sortutil module, the packt.addressbook module doesn't have the classes in the packt.util package anymore. But there is code in the module that still uses it. Notice what happens when we try to compile both the modules with the same command as the previous chapter. This time, since we are compiling two modules, we specify both the module names to the  --module argument separated by a comma.

$ javac -d out --module-source-path src --module packt.addressbook,packt.sortutil

The error you get should look something like this:

./src/packt.addressbook/packt/addressbook/Main.java:6: error: SortUtil is not visible because package packt.util is not visible
import packt.util.SortUtil;
         ^
./src/packt.addressbook/packt/addressbook/Main.java:13: error: cannot find symbol
SortUtil sortUtil = new SortUtil();
    ^
symbol:   class SortUtil
location: class Main
./src/packt.addressbook/packt/addressbook/Main.java:13: error: cannot find symbol
SortUtil sortUtil = new SortUtil();
                ^
symbol:   class SortUtil
location: class Main

Firstly, note that the errors are only from compiling the packt.addressbook module. This means that the compilation of the packt.sortutil module succeeded! The three errors in the preceding output are specifically about the Main.java file. The compiler clearly doesn't see the SortUtil class and it complains it is missing. But is it really missing? We do have a public SortUtil class available in the src folder, but since it is in a different module folder, the compiler doesn't seem to see it, even though the class itself is public. This brings up an important new change relating to class visibility in Java 9.

Just because a class is marked public in Java 9, it doesn't necessarily mean it is visible to all other types.

This is a radical change from the way public types work in Java 8 and earlier. Before Java 9, if you had a public class or interface in the classpath, it was available for any other type in the classpath to import and use. This is no longer the case with the module source path in Java 9!

主站蜘蛛池模板: 会同县| 耒阳市| 图木舒克市| 东乌珠穆沁旗| 河池市| 建德市| 安泽县| 隆子县| 苏尼特右旗| 滦平县| 河西区| 金沙县| 凌海市| 雅安市| 杭州市| 息烽县| 湖州市| 达州市| 黎城县| 海原县| 康乐县| 博乐市| 镶黄旗| 江阴市| 芦山县| 南华县| 乌鲁木齐县| 五大连池市| 柘荣县| 依安县| 调兵山市| 潜江市| 潍坊市| 崇礼县| 岳阳市| 海安县| 昌黎县| 隆安县| 临夏县| 班戈县| 布尔津县|