From 24889e2f7795b4191d77acc127e01952f9db7d31 Mon Sep 17 00:00:00 2001 From: duncanmcqueen Date: Wed, 14 Oct 2026 08:26:59 -0500 Subject: [PATCH] PrinterJob.getPrinterJob(): use the java.awt.printerjob property As in the JDK, the system property java.awt.printerjob names the PrinterJob class. The user sets it, for example next to awt.toolkit. Load the class through the system class loader, as Toolkit.getDefaultToolkit() loads awt.toolkit, so that a class on the class path also works. Without the property, use JavaPrinterJob as before. If the class cannot be loaded or made (also for a LinkageError, for example NoClassDefFoundError), print a warning and use JavaPrinterJob. Co-Authored-By: Claude Opus 5.5 Signed-off-by: duncanmcqueen --- java/awt/print/PrinterJob.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/java/awt/print/PrinterJob.java b/java/awt/print/PrinterJob.java index 1a4b8c8e20..bbc58d03cf 100644 --- a/java/awt/print/PrinterJob.java +++ b/java/awt/print/PrinterJob.java @@ -41,6 +41,8 @@ import gnu.java.awt.print.JavaPrinterJob; import java.awt.HeadlessException; +import java.security.AccessController; +import java.security.PrivilegedAction; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.DocFlavor; @@ -64,6 +66,36 @@ public abstract class PrinterJob */ public static PrinterJob getPrinterJob() { + // As in the JDK, the system property java.awt.printerjob can name + // the class. Load it as Toolkit loads awt.toolkit, so that a class + // on the class path also works. + String name = gnu.classpath.SystemProperties.getProperty("java.awt.printerjob"); + if (name != null) + { + Throwable error; + try + { + ClassLoader cl = (ClassLoader) AccessController.doPrivileged + (new PrivilegedAction() + { + public Object run() + { + return ClassLoader.getSystemClassLoader(); + } + }); + return (PrinterJob) Class.forName(name, true, cl).newInstance(); + } + catch (Exception e) + { + error = e; + } + catch (LinkageError e) + { + error = e; + } + System.err.println("PrinterJob: cannot use java.awt.printerjob=" + + name + " (" + error + "), using the default"); + } return new JavaPrinterJob(); }