开发者

Can I use java.awt.Robot from within a daemon?

开发者 https://www.devze.com 2023-03-07 08:05 出处:网络
I have written a server in Java that allows clients connected to it to control the mouse and keyboard of the computer. To do this it uses the java.awt.Robot class.

I have written a server in Java that allows clients connected to it to control the mouse and keyboard of the computer. To do this it uses the java.awt.Robot class.

I need this server to run in the background and start automatically. The first OS I am tackling this problem on is Debian based (Ubuntu 11.04) and a daemon seems like the obvious choice. The problem is that when the daemon is started dur开发者_JS百科ing boot or during the installation of my debian package (whose postinst script starts it using /etc/init.d/pc-remote-server start) I get this error:

java.awt.AWTException: headless environment
    at java.awt.Robot.<init>(Robot.java:97)
    at com.se.pcremote.server.CommandExecuter.<init>(CommandExecuter.java:72)
    at com.se.pcremote.server.PCRemoteServer.<init>(PCRemoteServer.java:215)
    at com.se.pcremote.server.PCRemoteServer.main(PCRemoteServer.java:122)

Is there any way I can use the java.awt.Robot class from within a daemon process? Could I spawn a secondary process from the daemon process that is not a 'headless environment'? Or is there a better way for me to get a 'service' like result that does not have this limitation?


"Headless" means that this code needs access to a graphics environment, and it hasn't.

You can run in headless mode by supplying a system property which provides a crude implementation which gives just the basics for running applications, but which most likely cannot support Robot. Try it however first.

If you cannot do that, you need a graphics environment for your process. The usual way to do this is to run a VNC X-server as it doesn't require physical hardware, and then connect to it.


I assume, you must set the DISPLAY variable correctly (in the environment of the robot process at the time when the robot process is started) for this to work -- in your case you would need to specify a display in your DISPLAY variable which is created some time after the program is started. --

No idea whether this really works, but you could give it a try and report back here whether it works.


Alright, after doing some more research and trying some more options here is what I came up with:

Can I use java.awt.Robot from within a daemon? No.

Further down in my question I elaborated a little:

Is there any way I can use the java.awt.Robot class from within a daemon process? No. As above.

Could I spawn a secondary process from the daemon process that is not a 'headless environment'? Not that I could figure out. It was going to be a lot of work if I did do it anyway.

Or is there a better way for me to get a 'service' like result that does not have this limitation? Yes! Use the desktop environment! In my case since I was using Ubuntu the desktop environment was Gnome. Gnome has a Startup Applications feature that runs off .desktop files on a global and per-user basis as described here. They also provide information on the structure of these .desktop files here. I added a .desktop file to /etc/xdg/autostart (the global autostart folder) that ran my Java 'service' and it worked like a treat.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号