简体   繁体   English

如何通过java代码设置或导出显示变量?

[英]How to set or export display variable through java code?

We are required to add export DISPLAY=:0.0 in tomcat's startup file and batch server's startup file. 我们需要在tomcat的启动文件和批处理服务器的启动文件中添加export DISPLAY=:0.0 This is to make java see the X11 Display libraries on Unix and run our applet. 这是为了让java在Unix上看到X11 Display库并运行我们的applet。 Without this export in the startup files, the applet throws a Headless Exception. 如果没有在启动文件中导出,则applet会抛出无头异常。

Though this explicit export makes the java applet run, it disrupts the other applications running on the server. 虽然这种显式导出使java applet运行,但它会破坏服务器上运行的其他应用程序。 Is there a way where I can make this export DISPLAY=:0.0 run from within java code instead of adding it to startup files? 有没有办法让我可以在java代码中运行此export DISPLAY=:0.0而不是将其添加到启动文件中? And if it is possible, would that be a good approach? 如果有可能,这会是一个好方法吗?

I have already tried setting the system property to -Djava.awt.headless=true , but it didn't work. 我已经尝试将系统属性设置为-Djava.awt.headless=true ,但它不起作用。 As the link given above http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/ also says that setting headless=true would work only for few components like Canvas, Panel but it will not work for the top level components. 正如上面给出的链接http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/也说设置headless = true只适用于Canvas,Panel这样的少数组件,但它不适用于顶层级别组件。

So I feel the only option left for me is using export DISPLAY=:0.0 . 所以我觉得唯一的选择就是使用export DISPLAY=:0.0 This is making my applet work when set in the startup files but causes problem for other applications running in the server. 这使得我的applet在启动文件中设置时工作,但会导致服务器中运行的其他应用程序出现问题。 So if anybody could help me to make export DISPLAY=:0.0 work such that it doesn't interfere with other applications in the server. 因此,如果有人可以帮助我进行export DISPLAY=:0.0工作,使其不会干扰服务器中的其他应用程序。 One way I thought was to export the display through code. 我认为的一种方法是通过代码导出显示。

Any help would be highly appreciated. 任何帮助将受到高度赞赏。

I believe you can actually set the system property -Djava.awt.headless=true which will allow access to the graphic libraries without actually needing a display. 我相信您实际上可以设置系统属性-Djava.awt.headless=true ,这将允许访问图形库而无需实际需要显示。

See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/ for more info. 有关详细信息,请参阅http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/

From your question it seems that there is something seriously wrong with your configuration. 从您的问题来看,您的配置似乎存在严重问题。 Tomcat should always be able to run server-side without a display. Tomcat应始终能够在没有显示器的情况下运行服务器端。 Applets always run in browser and get the x11 environment from the browser. Applet 总是在浏览器中运行,并从浏览器获取x11环境。 The applet's jar could be served by tomcat, or apache, or something else, but that's irrelevant. applet的jar可以由tomcat,apache或其他东西提供,但那是无关紧要的。

If your applets communicate with the server, make sure that the server code is completely separate from your applet code (keep them in separate projects) and that it doesn't use any awt code. 如果您的applet与服务器通信,请确保服务器代码与您的applet代码完全分开(将它们保存在单独的项目中),并且它不使用任何awt代码。 If it does (for image manipulation, etc.), then use -Djava.awt.headless as jdewald said. 如果它(用于图像处理等),则使用-Djava.awt.headless作为jdewald说。

How is this affecting other applications? 这对其他应用程序有何影响? How are you defining the environment variable in your start up scripts? 您是如何在启动脚本中定义环境变量的? If you're defining the variable correctly, it should only affect programs started by your start up script, ie, Tomcat and the batch server. 如果您正确定义变量,它应该只影响由启动脚本启动的程序,即Tomcat和批处理服务器。

Also, your original question doesn't really add up. 此外,您的原始问题并没有真正加起来。 Are you running both the server and client (Tomcat and the web browser) on the same machine? 您是否在同一台计算机上运行服务器和客户端(Tomcat和Web浏览器)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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