oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.38k stars 1.63k forks source link

Discovering available printer services leads to a runtime crash #3484

Open gradinac opened 3 years ago

gradinac commented 3 years ago

The following simple program attempts to find the default printer service: Printer.java:

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

public class Printer {
    public static void main(String[] args) {
        PrintService pserv = PrintServiceLookup.lookupDefaultPrintService();

        if (pserv != null) {
            System.out.println("We have a print service: " + pserv.getName());
        } else {
            System.out.println("No print service available!");
        }
    }
}

Compiling and running it on the JVM on a machine without a printing service leads to:

$ javac Printer.java
$ java Printer
No print service available!

However, building and running the same program as a native-image leads to:

$ native-image Printer
[printer:39908]    classlist:     865.33 ms,  0.96 GB
...
[printer:39908]      [total]:  33,418.31 ms,  3.99 GB
$ ./printer 
Exception in thread "main" java.lang.UnsatisfiedLinkError: sun.print.CUPSPrinter.initIDs()Z [symbol: Java_sun_print_CUPSPrinter_initIDs or Java_sun_print_CUPSPrinter_initIDs__]
    at com.oracle.svm.jni.access.JNINativeLinkage.getOrFindEntryPoint(JNINativeLinkage.java:153)
    at com.oracle.svm.jni.JNIGeneratedMethodSupport.nativeCallAddress(JNIGeneratedMethodSupport.java:57)
    at sun.print.CUPSPrinter.initIDs(CUPSPrinter.java)
    at sun.print.CUPSPrinter.<clinit>(CUPSPrinter.java:91)
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375)
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295)
    at sun.print.PrintServiceLookupProvider.getDefaultPrintService(PrintServiceLookupProvider.java:648)
    at javax.print.PrintServiceLookup.lookupDefaultPrintService(PrintServiceLookup.java:206)
    at Printer.main(Printer.java:6)

This crash shouldn't happen on native - I suspect the most likely reason for it is not adding the AWT libraries that contain this method to the image.

This issue is reproducible on Java 11, Linux-AMD64 - on master.

kirillp commented 3 years ago

I don't think java.desktop is supported. AWT/Swing is not, so you can safely assume that any dependency from java.desktop will not work. (this is not because Graal has issues, this is because awt/swing is written this way)

hemin110 commented 2 years ago

how to solve it?