1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * @test 25 * @bug 7161283 8158520 26 * @summary Toolkit.getPrintJob throws NPE if no printer available 27 * @run main PrinterException 28 */ 29 import java.awt.Frame; 30 import java.awt.JobAttributes; 31 import java.awt.PrintJob; 32 import java.awt.Toolkit; 33 import java.awt.Robot; 34 import java.awt.event.KeyEvent; 35 36 public class PrinterException { 37 38 public static void main(String[] args) throws Exception { 39 Robot robot = new Robot(); 40 Thread t = new Thread (() -> { 41 robot.waitForIdle(); 42 robot.delay(2000); 43 robot.keyPress(KeyEvent.VK_ESCAPE); 44 robot.keyRelease(KeyEvent.VK_ESCAPE); 45 }); 46 Toolkit tk = Toolkit.getDefaultToolkit(); 47 PrintJob pj = null; 48 49 int[][] pageRange = new int[][]{new int[]{1,1}}; 50 JobAttributes ja = new JobAttributes(1, 51 java.awt.JobAttributes.DefaultSelectionType.ALL, 52 JobAttributes.DestinationType.FILE, JobAttributes.DialogType.NATIVE, 53 "filename.ps", Integer.MAX_VALUE, 1, 54 JobAttributes.MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES, 55 pageRange, "", JobAttributes.SidesType.ONE_SIDED); 56 57 Frame testFrame = new Frame("print"); 58 try { 59 if (tk != null) { 60 t.start(); 61 pj = tk.getPrintJob(testFrame, null, ja, null); 62 } 63 } finally { 64 testFrame.dispose(); 65 } 66 } 67 68 }