PrintServiceLookup.lookupDefaultPrintService() 返回 null
PrintServiceLookup.lookupDefaultPrintService()
返回 NULL
因为我安装了打印机并将其设置为默认打印机。
如果我在一个简单的程序中使用它,它工作得很好,但是当我尝试在基于小程序的程序中使用它时,它会返回NULL
。
请给我一些解决这个问题的好方法。
PrintServiceLookup.lookupDefaultPrintService()
returns NULL
as I have a printer installed and set to default printer.
If I am using this in a simple program it works fine, but when I try to use it in my applet-based program it returns NULL
.
Please send me some good solution for this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了访问打印机(或主机上的任何资源),必须对小程序代码所在的 jar 文件进行签名,并且用户必须接受签名者作为可信方。
要对 jar 文件进行签名,请使用 jarsigner 程序,该程序是 JDK 的一部分。 Jarsigner 使用自己的密钥库,因此如果您有自己的证书,则必须先将证书导入到密钥库。 如果您没有任何其他证书来签署 jar 文件,它也可以生成证书。
可以在此处找到 jarsigner 工具的文档。
较新的 Java 运行时确实会询问用户是否允许代码访问打印机,但我发现无论答案如何,未签名的 jar 文件中的代码都无法访问资源。
In order to access the printer (or any resource on the host computer for that matter) the jar file in which the applet code resides has to be signed, and the user must accept the signer as a trusted party.
To sign the jar file, use the jarsigner program, which is part of he JDK. Jarsigner uses its own keystore, so if you have your own certificate, you must import the certificate to a keystore first. It can generate certificates as well, in the case you don't have any other certificate to sign the jar file with.
Documentation of the jarsigner tool can be found here.
Note that newer Java runtimes do ask the user if (s)he allows the code to access the printer, but I found that regardless of the answer, code in an unsigned jar file is prevented from accessing resources.
此代码在 Windows 1.7.0_55 中的签名小程序中运行:
This code is working in an signed applet in windows with 1.7.0_55:
您必须首先更改 java applet 的安全设置。 默认情况下,java applet 无法打印。
You must change the security settings for java applets first. By default, java applets cannot print.
确保 Printer.conf 定义
而不是
。 JVM 似乎只能找到这样定义的默认打印机。此代码片段可以帮助快速验证它是否有效:
Make sure that printer.conf defines
<DefaultPrinter name>
instead of<Printer name>
. JVM seems to only find a default printer that is defined like this.This code snippet could help out to quickly verify if it works:
在 Windows 中,确保后台打印程序服务正在运行。 另外,您可以在应用解决方案
参考时测试此代码:https://bugs.openjdk.org /浏览/JDK-8178715
In windows, make sure the Print Spooler service is running. Also, You can test this code while applying the solution
Reference: https://bugs.openjdk.org/browse/JDK-8178715