Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package sun.awt does not exist

When compiling with ant, calls to sun.awt.AppContext work, however the same code compiled with IntelliJ fails using AdoptOpenJDK11.

  • sun.awt.AppContext with Oracle JDK8 works with IntelliJ
  • sun.awt.AppContext with AdoptOpenJDK11 does NOT work with IntelliJ

Although Sun/Oracle has warned for a while about avoiding the sun.* packages, there are certain features (bugs?) in Java that still require them and stranger, the command line seems happy.

Sample code:

package test;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import sun.awt.AppContext;

public class Main {

    public static void main(String[] args) {
        //Update printer list in CUPS immediately
        AppContext.getAppContext().put(PrintServiceLookup.class.getDeclaredClasses()[0], null);

        PrintService[] printers = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService p : printers) {
            System.out.println(p.getName());
        }
    }
}

enter image description here

How does one configure IntelliJ to behave like ant and allow access to sun.awt.* packages?

like image 266
tresf Avatar asked May 31 '19 02:05

tresf


People also ask

Why is the Sun* package not supported in Java?

The sun.* packages are not part of the supported, public interface. A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.

Are the Sun* packages part of the supported public interface?

The sun.* packages are not part of the supported, public interface. A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms.

Where is the AWT Utilities folder in the JDK?

Show activity on this post. Your JDK should have com.sun.awt.AWTUtilities. It is in rt.jar. Maybe the problem (for that file) is that your IDE excludes the parent the build path ... on the grounds that it is a bad idea to use those classes directly.

Does the package exist during recompile?

The package does not exist during recompile, but the code editor does not say (because the package exists and is in the same directory level). Thanks. Is that a Maven or Gradle project? What's the scope of the dependency in the build system? How did you define it in the build script? What's the exact error?


Video Answer


1 Answers

Disable the Use '--release' option...:

release

See this answer for details.

like image 159
CrazyCoder Avatar answered Sep 21 '22 08:09

CrazyCoder