javajava-9java-modulejava-platform-module-systemmodule-info

Is `sun.awt.image` package deprecated?


I wanted to use some codes that using sun.awt.image.PNGImageDecoder in my project. The problem is source code is based on Java 8 and my project using JDK 9+ (11). So I got this error:

Package 'sun.awt.image' is declared in module 'java.desktop', which does not export it to the unnamed module.

for import sun.awt.image.*, and:

Symbol is declared in module 'java.desktop' which does not export package 'sun.awt.image'

for using PNGImageDecoder or ImageDecoder etc.

After some efforts to find a solution, I found some similar situations that suggest adding requires java.desktop; to module-info.java file (like this case and this case). But as I realized this solution can only be useful when your project already has a module-info.java file and you missing one used module in it.

My project hadn't this file already (pay attention to "the unnamed module" in the first error message). As far as I know, this means all modules are imported automatically. So adding this file and adding that line hadn't any effect except (changing error message and) causing some similar issues for other parts of my codes (which were, of course, solvable by adding required modules to module-info.java file).

To sure I imported all modules in jmods folder (of JDK 11), but the problem didn't solve:

requires java.base;
requires java.compiler;
requires java.datatransfer;
requires java.desktop;
requires java.instrument;
requires java.logging;
requires java.management;
requires java.management.rmi;
requires java.naming;
requires java.net.http;
requires java.prefs;
requires java.rmi;
requires java.scripting;
requires java.se;
requires java.security.jgss;
requires java.security.sasl;
requires java.smartcardio;
requires java.sql;
requires java.sql.rowset;
requires java.transaction.xa;
requires java.xml.crypto;
requires java.xml;
requires jdk.accessibility;
requires jdk.aot;
requires jdk.attach;
requires jdk.charsets;
requires jdk.compiler;
requires jdk.crypto.cryptoki;
requires jdk.crypto.ec;
requires jdk.crypto.mscapi;
requires jdk.dynalink;
requires jdk.editpad;
requires jdk.hotspot.agent;
requires jdk.httpserver;
requires jdk.internal.ed;
requires jdk.internal.jvmstat;
requires jdk.internal.le;
requires jdk.internal.opt;
requires jdk.internal.vm.ci;
requires jdk.internal.vm.compiler;
requires jdk.internal.vm.compiler.management;
requires jdk.jartool;
requires jdk.javadoc;
requires jdk.jcmd;
requires jdk.jconsole;
requires jdk.jdeps;
requires jdk.jdi;
requires jdk.jdwp.agent;
requires jdk.jfr;
requires jdk.jlink;
requires jdk.jshell;
requires jdk.jsobject;
requires jdk.jstatd;
requires jdk.localedata;
requires jdk.management.agent;
requires jdk.management.jfr;
requires jdk.management;
requires jdk.naming.dns;
requires jdk.naming.rmi;
requires jdk.net;
requires jdk.pack;
requires jdk.rmic;
requires jdk.scripting.nashorn;
requires jdk.scripting.nashorn.shell;
requires jdk.sctp;
requires jdk.security.auth;
requires jdk.security.jgss;
requires jdk.unsupported.desktop;
requires jdk.unsupported;
requires jdk.xml.dom;
requires jdk.zipfs;

I know this package (sun.awt.image) exists in java.desktop module (of JDK 11), but don't know how can access it.

I use IntelliJ IDEA as IDE.


Solution

  • You should not use sun.* or com.sun.* classes in your program; they are undocumented, internal classes of the JDK and can change in any release.

    Using them possibly makes your program incompatible with other versions of Java.

    See: It is a bad practice to use Sun's proprietary Java classes?