Solved

Java PrintJob works on Windows and Linux but incomplete on Mac

Posted on 2004-08-23
8
1,801 Views
Last Modified: 2009-07-29
Java application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The codJava application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The code is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thankse is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thanks
0
Comment
Question by:John4343
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 11878308
I see you use 2 different JRE to execute. Try to use the same JRE on both machine (Linux, Windows, Mac OS).
Do the error remains?
0
 
LVL 20

Accepted Solution

by:
Venabili earned 500 total points
ID: 11878760
Yes,
In the J2SE 1.4.1 or 1.4.2 on Mac OS X javax.print is not fully implementeed and is not fully working (don't ask me... tok me a week to find a url that proves what I already suspectred.\

Here we go:
http://developer.apple.com/java/faq/issues.html#anchor6
The workaround is to use the old print methods from jawa.awt.print:
              PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
               PrintService service = null;
               java.awt.print.PrinterJob pjob = java.awt.print.PrinterJob.getPrinterJob();
               PrintService printService[] =  pjob.lookupPrintServices();
               if (printService == null || printService.length == 0) {
                   ERROR = 1;
                   return null;
              }
           if ( pjob.printDialog()){
               service = pjob.getPrintService();
               pJob = pjob;
               service = servicePrint;
           }else{
               service = null;
           }
if (servicePrint!=null){
               DocAttributeSet das = new HashDocAttributeSet();
              Doc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
                DocPrintJob job1 = servicePrint.createPrintJob();
                job1.print(doc, pras);
}


Let me know if you need assitance on this (I am not sure if it compiles as it is as I removed some of the code taht is specific for my application - if there are troubles let me know.

As a whole I just check if it is a MacOS or not and depending on the case use different print code (this one is for MacOS - your is ok for any other)
Checking for MacOS:
             System.getProperty("mrj.version")  is null if you are not on Mac ;)
0
 

Author Comment

by:John4343
ID: 11882640
I'm not quite certain about this so please bare with me.  This code works in Windows but crashes on the indicated line on the Mac with a null pointer exception!

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
    System.out.println ("ERR");
    return;
}
                  
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
    book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   javax.print.attribute.DocAttributeSet das = new javax.print.attribute.HashDocAttributeSet();
   javax.print.Doc doc = new javax.print.SimpleDoc(book, javax.print.DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
   javax.print.DocPrintJob job1 = service.createPrintJob();
   try
   {
      job1.print(doc, pras);  <<=== NPE
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

Thanks
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11882746
The StackTrace?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 20

Expert Comment

by:Venabili
ID: 11882824
Oops... I think I need coffee.  This is half the Win code, half the Mac code :(

Sorry

THe actual printing should be:
if (service!=null)
{
   try
   {
       pJob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

The null pointer is exactly from the not implemented method I was trying to warn you about :(

Which returns us in the almost initial position...

Venabili
0
 

Author Comment

by:John4343
ID: 11886841
Here is the current code.  Works OK on Windows but still incomplete output on Mac.

Thanks for the help.  I'll be out of town till late Friday and will try not to think too hard...

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
   System.out.println ("ERR");
   return;
}
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
   book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   try
   {
      pjob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}
0
 

Author Comment

by:John4343
ID: 11931157
Ah! Got it!!!  

awt.print runs in a separate thread so needed to do a synchronize so the page will be rendered completly.

Thanks
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11931199
If you prefer you can get your points back? After all you found it itself. However - if you think that I had helped, it's ok

Venabili
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now