Java printing issue under Sierra 10.12.4

Hi,


Since the release of Sierra 10.12.4, the users of Sweet Home 3D sandboxed version can't print anymore.

This Java program simply hangs on the call to PrinterJob#printDialog under this system, but there's no problem on previous systems or when the program isn't sandboxed.

I isolated this issue in the following example that will hang also:

import java.awt.Graphics;
import java.awt.print.*;
import javax.swing.JOptionPane;

public class PrintTest  {
  public static void main(String [] args) {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    printerJob.setPrintable(new Printable() {
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
          // Print one empty page
          return pageIndex == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
        }
      });
    JOptionPane.showMessageDialog(null, "setPrintable works!");

    if (printerJob.printDialog()) {
      try {
        printerJob.print();
        JOptionPane.showMessageDialog(null, "PrinterJob works!");
      } catch (PrinterException e) {
        JOptionPane.showMessageDialog(null, "PrinterJob exception: " + e);
      }
    }
  }
}


Entitlements are set as following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-/
<plist version="1.0">
<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.files.user-selected.read-write</key>
  <true/>
  <key>com.apple.security.print</key>
  <true/>
  <key>com.apple.security.network.client</key>
  <true/>
</dict>
</plist>


Any suggestion why this could happen and how to fix it?


Thanks for your help,

Emmanuel

Answered by puybaret in 236325022

I finally fixed the bug in Sweet Home 3D 5.4.2, by creating new native methods that use NSPageLayout, NSPrintPanel and NSPrintperation classes directly in JNI.


I would have appreciated a minimum of feedback. Too bad! 😟


Emmanuel

This discussion about printing issues in Moneydance seem to be about the same bug.

Actually, requesting print dialog box doesn't make the program hang forever, because the dialog box is finally displayed after 3 to 4 minutes on a MacBook Pro 2011. Calling PrinterJob#pageDialog method causes the same delay.

So, it looks like an issue where a timeout is involved. Hope this can help Apple to fix this issue in the next release of Sierra.

Please, give some news!

Emmanuel

Accepted Answer

I finally fixed the bug in Sweet Home 3D 5.4.2, by creating new native methods that use NSPageLayout, NSPrintPanel and NSPrintperation classes directly in JNI.


I would have appreciated a minimum of feedback. Too bad! 😟


Emmanuel

I’m glad you found a workaround for this. Just FYI, I investigated this as part of a DTS incident from another developer and found that:

  • On macOS 10.12.4 and later the sandbox prevents

    localhost:631
    access for security reasons (r. 8992907).
  • cupsServer
    , the function that returns the default printer, returns a path to CUPS’s UNIX domain socket, which works fine from within the sandbox.
  • Java calls

    cupsServer
    and then, if the result starts with
    /
    , it ignores the returned value and prints via
    localhost:631
    . This is blocked by the above-mentioned security change.

I don’t know which Java does this but it definitely seems like it’s their problem to resolve.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo for these details.

I hope Oracle will find how to fix it for other Java developers.


Emmanuel

Java printing issue under Sierra 10.12.4
 
 
Q