I'm fairly new to the world of java and the Pi so please bear with me and explain to me in great detail how to fix my problem.

So as the title says, I'm trying to run a Swing GUI at start up and have managed to get it to partially work. I used the file /etc/rc.local to run a quick execution

export DISPLAY=0.0
sudo java -jar /home/pi/NetBeansProjects/FanController/dist/FanCOntroller.jar

NOTE: Wont run without the export DISPLAY=0.0 part.

On initial operation everything looked great but when I started to play with the program I noticed that it was acting strangely when I changed a few values. Basically the program allows a user to set a temperature for turning a fan on and set another setting for turning the fan back off.

What I noticed is that when I set the value to less than the current temperature it started to switch the output on and off rapidly as if there was something conflicting with it. From further investigation I can only assume that there are 2 instances of the program running. One on the touch screen display I have plugged in and the other in the background somewhere.

I have VNC on the Pi so maybe even when I'm not connected to it, there's still a hidden version of it running as when I do start the VNC connection I get the same issue. If I update the settings on the touch screen, it doesn't automatically update on the VNC connection and visa versa.

Is there a way of ensuring only one instance of the program is running or is there a better way of auto starting the application. I've tried many different approaches but none work.

1. Create a file named /etc/xdg/autostart/startup.desktop
2. Put the following contents into it

[Desktop Entry] Type=Application
Exec=/user/home/jre/java -jar javafile.jar

3. Restart.

Attempted to add it to the following file


With no luck

Attempted this

sudo update-rc.d {path of the script}/FanController.sh defaults

Created a .sh file with the command to start the program and again wouldn't start.

Any help would be great please.


Got this sorted. The issues was as I suspected, I was running 2 instances of the same program and they where fighting against each other. To this end I stopped using tightVNC and started using x11vnc. x11vnc doesn't start up a second instance of the desktop. It allows me to view the actual output of the display adapter. Downside is that the remote connection was a small window but i'll live with it.

Thanks again for all the suggestions.

3 Answers 3


Assumption: You have access to the source codes of FanController application.

Even if you sorted out everything on the system layer, I suggest you to do some changes on the application layer.

The most convenient and solid way of making a single instance Java application is to use the exclusive file lock mechanism.

Example Java Code (exception handling is not included):

File file = new File("/var/run/FanController.singleinstance");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileLock lock = raf.getChannel().tryLock();
if (null == lock) {
    System.err.println("There is another running instance of the application");

To run a GUI, you need to execute it within a graphical user session. If your Pi is configured to boot into graphical mode and auto-login, it should suffice to put a .desktop file in one of the Desktop Autostart directories, e.g. ~/.config/autostart/ or `/etc/xdg/autostart.

For your startup.desktop, you need a linebreak after [Desktop Entry]. Also triple-check that java path. In Debian/Raspian /usr/bin/java (which points to whatever JRE is currently defined as the default) is your best bet. But since /usr/bin is in the $PATH, you don't need the full path anyway.

[Desktop Entry]
Exec=java -jar /home/Cubiod/swingui.jar

As for the one instance per host rule, I would solve that in the application using a locking mechanism, which is answered on StackOverflow.

  • Hi, I've tried as you mentioned above but still not working.
    – Cubiod
    Feb 22, 2016 at 20:02
  • I have added the FanController.desktop file to the directory /.config/autostart but still no joy. I also tried the directory /etc/xdg/autostart but still nothing. Is there a log file that would tell me if it attempted to start it and failed or is there a way for me to log a failure in a particular file. Semothing like "echo "Failed" >> /var/log/mylog". Also another note, My touchscreen has a different desktop environment to my VNC desktop.
    – Cubiod
    Feb 22, 2016 at 20:10

Probably not the solution you are looking for, but I noticed you have not launched the jar file as a background process as it should be done when editing the rc.local file. The system will not finish booting until the rc.local exits.

See the documentation on this

Try adding a & at the end like so

export DISPLAY=0.0
sudo java -jar /home/pi/NetBeansProjects/FanController/dist/FanCOntroller.jar &


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.