简体   繁体   中英

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. This is to make java see the X11 Display libraries on Unix and run our applet. Without this export in the startup files, the applet throws a Headless Exception.

Though this explicit export makes the java applet run, it disrupts the other applications running on the server. 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? 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. 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.

So I feel the only option left for me is using 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. 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. 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.

See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/ for more info.

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. Applets always run in browser and get the x11 environment from the browser. The applet's jar could be served by tomcat, or apache, or something else, but that's irrelevant.

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. If it does (for image manipulation, etc.), then use -Djava.awt.headless as jdewald said.

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.

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?

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