Swing Issue on Java 10

2020-02-12 14:10发布

问题:

I am currently trying to move our application from Java 8 to Java 10 and in few months will be moving to Java 11.

We have a legacy system which uses a bit of API's which are removed or will be removed I guess. We are using ant script (1.10.1) for our application compilation.

Below is the same of the part that build our application:

  <target name="javac-setup" depends="flags, with.clover, jaxb-apply, flamingo.javac"/>

    <target name="javac" depends="javac-setup">
        <mkdir dir="${workmanager.dir.classes}"/>
        <javac destdir="${workmanager.dir.classes}" deprecation="on" debug="${javac.debug}" encoding="iso-8859-1" includeantruntime="false">
            <compilerarg line="-Xlint:static"/>
            <compilerarg line="-Xlint:unchecked"/>
            <compilerarg line="-Xlint:overrides"/>
            <compilerarg line="-Xmaxerrs 9999" />
            <compilerarg line="-Xmaxwarns 9999" />
            <compilerarg value="-Werror"/>
            <src path="${workmanager.dir.src}"/>
            <src path="${webservices.dir.java.src}"/>
            <patternset refid="java.sources"/>
            <classpath refid="classes.classpath"/>
        </javac>
    </target>

During compilation I am getting few errors related to Java Swing (again bit older one, but have to live with it) after moving to Java 10. As I am still not completely aware of all the changes happened in Java 10, to me it looks like the issue is related to modularization concept. Not sure though still learning. I know your suggestion to know if these kinds of error are related to modularization. Also any suggestion or tips to fix these?

Below is the error:

[javac] Compiling 3253 source files to C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\classes
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\datamgmt\report\ui\AssignPosNumberToolbar.java:26: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsButtonUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\datamgmt\report\ui\AssignPosNumberToolbar.java:27: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsToolBarUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\utils\WMClosableTabbedPane.java:33: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\MultiColumnComboBox.java:58: error: package com.sun.java.swing.plaf.motif is not visible
    [javac] import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.motif is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\datamgmt\ui\SimplificationComboBox.java:31: error: package com.sun.java.swing.plaf.motif is not visible
    [javac] import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.motif is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPComboBoxUI.java:21: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\datamgmt\ui\favorites\FavoritesToolBar.java:28: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsButtonUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\datamgmt\ui\favorites\FavoritesToolBar.java:29: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsToolBarUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\ptc\PtcLaf.java:14: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\ptc\PtcMenuUI.java:31: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\ptc\PtcRadioButtonMenuItemUI.java:14: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsIconFactory;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\ptc\PtcSeparatorUI.java:20: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsSeparatorUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPButtonUI.java:26: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsButtonUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPLookAndFeel.java:24: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsIconFactory;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPLookAndFeel.java:25: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPMenuUI.java:25: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPRadioButtonMenuItemUI.java:14: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsIconFactory;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPSeparatorUI.java:17: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsSeparatorUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPToggleButtonUI.java:23: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsToggleButtonUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\laf\xplookandfeel\XPToolBarUI.java:17: error: package com.sun.java.swing.plaf.windows is not visible
    [javac] import com.sun.java.swing.plaf.windows.WindowsToolBarUI;
    [javac]                               ^
    [javac]   (package com.sun.java.swing.plaf.windows is declared in module java.desktop, which does not export it)
    [javac] 20 errors 

回答1:

Yes, this is due to the module system. You are accessing internal APIs, which the JPMS won't let you do by default.

Quick fix

To quick-fix this, use the command line option --add-exports %module/%package=ALL-UNNAMED for each package mentioned by the messages, e.g.:

--add-exports java.desktop/com.sun.java.swing.plaf.windows=ALL-UNNAMED

(No, there is no wildcard) You have to add these to your build command for Java 9+ and at some point (likely Java 12+) also to your execution command.

Proper fix

As mentioned, this is just a quick-fix. With the module system in place, the JDK team might more aggressively refactor internals like the API you are using, so chances are that you will run into problems with changed or removed classes in the future.

The proper fix is to remove your dependencies on these internals. A first step is to check whether the features you're using there might in the meantime have gotten a standardized API.