I have a Java GUI app that runs fine on my windows PC and on a Raspi running Raspian when the Raspi has a monitor connected. I want to run the java app on the Raspi without a monitor attached. I'm currently interacting with the Raspi using TightVNC. When I run the java app on the pi without a monitor, I get the following error:

Can't connect to X11 window server using ':2.0' as the value of the DISPLAY variable.'

(I start the VNCServer on the Raspi with a file which includes the line: su - $USER -c "/user/bin/vncserver :2 )

and what I see at the top left of my VNC client window on my PC is:

pi's X desktop(raspberrypi:2)

Can anyone help me get the java GUI working on the pi via VNC?

  • Have you tried setting the DISPLAY environment variable to :2? – airfishey May 9 '16 at 16:26
  • I'm new to Unix - I have tried setting the display environment to 2, if putting: set DISPLAY = 2 into a terminal window would do the trick. One thing that puzzles me is the lack of feedback - I put a command in the command line, and often, no obvious result! – pir8ped May 9 '16 at 19:35
  • The easiest way to specify an environment variable for a single application is VAR=value command arg1 arg2 e.g. DISPLAY=':2' java -jar my.jar. Note that DISPLAY is a string, not an integer, and that Bash requires there be no spaces between the variable and the value (so DISPLAY = 2 isn't going to work). – dimo414 May 10 '16 at 1:39
  • Where did you get the :2 value in your VNCServer script? What happens if you don't specify that argument? If you need to leave it in in order for your script to work, it's likely you need to use the same value (:2) as the value passed to your Java application. – dimo414 May 10 '16 at 1:41
  • Also (last comment, I swear) the easiest way to get feedback is to add logging lines. For example adding System.out.println("DISPLAY is set to " + System.getenv("DISPLAY")); at the start of your main() method will let you verify the value of DISPLAY your Java program is seeing. – dimo414 May 10 '16 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.