简体   繁体   中英

PrintServiceLookup.lookupPrintServices on Red Hat Linux with Java 1.5 sometimes returns array with NULLs insted of objects

We have this weird problem with obtaining printer list on RHL. I wrote a small peace of code to demonstrate what's happening:

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PrintTest
{
  public static void main(String[] args)
  {
      PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null, null);
      System.out.println(pservices.length);

      for( int y = 0; y < pservices.length; y++ ){
        System.out.print(y + " - " );
        System.out.println(pservices[y]);
      }
  }
}

This is the result:

0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - null
1 - null
2 - null
3 - null
4 - null
5 - null
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - IPP Printer : PPR403_2
1 - IPP Printer : PPR413_2
2 - IPP Printer : PPR506_7
3 - IPP Printer : PPR509_7
4 - IPP Printer : PPR510_2
5 - IPP Printer : PPR516_1
6
0 - null
1 - null
2 - null
3 - null
4 - null
5 - null

(i ran the code multiple times in a row).

As you can see, sometimes we can get a list of printers (6 configured on the system) and sometimes we get an array of lenght 6 but there are no objects, only nulls :/.

Please help me diagnose the problem I've done a quick search on google, but nothing seems to fit this.

This is the java version on server: java version "1.5.0_30" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_30-b03) Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_30-b03, mixed mode)

I cannot change this, unfortunatly.

EDIT: Here are logs from CUPS (don't know if it matters):

D [04/Jan/2013:16:31:25 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:25 +0100] cupsdAcceptClient: 8 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:25 +0100] cupsdCloseClient: 8
D [04/Jan/2013:16:31:25 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:25 +0100] cupsdAcceptClient: 8 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:25 +0100] cupsdCloseClient: 8
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 8 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdReadClient: 8 POST / HTTP/1.1
D [04/Jan/2013:16:31:26 +0100] cupsdAuthorize: No authentication data provided.
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Default
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Default client-error-not-found: No default printer
D [04/Jan/2013:16:31:26 +0100] cupsdProcessIPPRequest: 8 status_code=406 (client-error-not-found)
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdReadClient: 8 POST / HTTP/1.1
D [04/Jan/2013:16:31:26 +0100] cupsdAuthorize: No authentication data provided.
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Printers
D [04/Jan/2013:16:31:26 +0100] cupsdProcessIPPRequest: 8 status_code=0 (successful-ok)
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdReadClient: 8 POST / HTTP/1.1
D [04/Jan/2013:16:31:26 +0100] cupsdAuthorize: No authentication data provided.
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Default
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Default client-error-not-found: No default printer
D [04/Jan/2013:16:31:26 +0100] cupsdProcessIPPRequest: 8 status_code=406 (client-error-not-found)
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: skipping getpeercon()
D [04/Jan/2013:16:31:26 +0100] cupsdAcceptClient: 10 from localhost:631 (IPv4)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 10
D [04/Jan/2013:16:31:26 +0100] cupsdReadClient: 8 POST / HTTP/1.1
D [04/Jan/2013:16:31:26 +0100] cupsdAuthorize: No authentication data provided.
D [04/Jan/2013:16:31:26 +0100] CUPS-Get-Printers
D [04/Jan/2013:16:31:26 +0100] cupsdProcessIPPRequest: 8 status_code=0 (successful-ok)
D [04/Jan/2013:16:31:26 +0100] cupsdCloseClient: 8

There are problems with CUPS and java jre on unix/linux platform, to see more info you can set sun.print.ippdebug to true . In your case it looks like this bug in jre, so setting sun.java2d.print.polling to true should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM