Search code examples
javauser-interfacejarxorgautostart

Autostart GUI Java jar Lubuntu


Hi guys I need your help. I have a PC which runs Lubuntu 14.10 without a monitor. The user is autologged in. I have created a Sysvinit script and installed it on /etc/init.d. My script amongst other things, starts a jar file that opens a GUI application that listens on serial port.

The problem is that I can't make the jar application start automatically on boot. Java complains that it cannot connect to the X11 display server. However this is the strange thing. If I ssh into the machine and run the script myself with sudo service it starts normally. Also if I have a monitor connected during boot, it also starts correctly by itself.

I need to have the script started without a monitor connected. It seems as if when a monitor is not connected, Xorg server isn't initiated. Does anyone have any suggestions?

Thanks


Solution

  • After a lot of troubleshooting, I finally managed to achieve what I wanted. The problem after all was that X server did not have enough time to load. The Xserver was started from lightdm that was an upstart service, and my script started from init.d.

    It seems that if a monitor is connected X server starts earlier and my script in init.d didn't crash.

    A simple sleep 10 command to stall the execution of the script until X server started did the trick. However this is a guess of time when X server will start. So a more elegant solution would be to check when the desktop starts and then launch my app. In order to achieve this I inserted the following lines before launching my script.

    while [ -z $(pidof lxsession) ]; do
      echo "LXSession not started yet, waiting for 2 secs"
      sleep 2
    done
    

    with -z $(pidof lxsession) I check if the returned string of pidof is null. (So No PID found for process lxsession). As soon as lxsession starts, the loop is canceled and the script moves on to the execution of my java app that now finds the X server and runs normally.

    Thank you all for your help. I hope other people are helped by this thread and not tortured like me!