java - 使用 Java 1.5 的 Red Hat Linux 上的 PrintServiceLookup.lookupPrintServices 有时会返回包含 NULL 的对象的数组

标签 java linux printing cups

我们在获取 RHL 上的打印机列表时遇到了这个奇怪的问题。我编写了一小段代码来演示正在发生的事情:

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]);
      }
  }
}

这是结果:

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

(我连续多次运行代码)。

正如你所看到的,有时我们可以获得打印机列表(系统上配置了 6 个),有时我们获得长度为 6 的数组,但没有对象,只有 null :/。

请帮我诊断问题,我在 Google 上进行了快速搜索,但似乎没有任何内容适合此。

这是服务器上的java版本: java版本“1.5.0_30” Java(TM) 2 运行时环境,标准版(内部版本 1.5.0_30-b03) Java HotSpot(TM) 64 位服务器 VM(内部版本 1.5.0_30-b03,混合模式)

不幸的是,我无法改变这一点。

编辑:以下是来自 CUPS 的日志(不知道是否重要):

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

最佳答案

unix/linux 平台上的 CUPS 和 java jre 存在问题,要查看更多信息,可以将 sun.print.ippdebug 设置为 true。 在你的情况下,它看起来像 this bug在 jre 中,因此将 sun.java2d.print.polling 设置为 true 应该会有所帮助。

关于java - 使用 Java 1.5 的 Red Hat Linux 上的 PrintServiceLookup.lookupPrintServices 有时会返回包含 NULL 的对象的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159853/

相关文章:

linux - 如何指定 TeamCity 构建代理 linux 用户组

c - print_list 函数只打印链表中用户的第一个输入

java - 在 GWT 正则表达式中使用捕获组

java - 将 PostgreSQL bytea-stored serialized-java-UUID 转换为 postgresql-UUID

java - 为什么 Sorted JLists 还没有成为标准 Swing 库的一部分?

java - 如何根据表头制作pojo类?

linux - 如何搭建nginx测试环境?

linux - Snort日志文件输出格式

css - 什么最适合打印? (一个网站页面)

python-2.7 - Python Print 语句不会用\r 覆盖前一行