How to set java system properties globally on OS X?

Blanka picture Blanka · May 17, 2012 · Viewed 9.3k times · Source

Short story

I need a system level way to set the java.awt.headless property to true for all java invocations. That is, using -Djava.awt.headless=true is not an option, since java is being invoked from places I don't have access to (e.g. from within another tool written in Java/C/etc.)

Long story

I'm using a bunch of tools written in Java (specifically Adobe's Air ADT) that rely on AWT classes. When I run these tools on the console they work fine. But when I run them from an SSH session they fail with java.lang.InternalError: Can't connect to window server - not enough permissions. Googling around I found that setting java.awt.headless to true will fix the problem. It doesn't, and that's because ADT itself spawns children Java processes without -Djava.awt.headless=true.

Is there any system-level way to ensure this property is set whenever Java is invoked? Maybe some system awt property file or equivalent?

Worst case scenario I could try replacing /usr/bin/java with a shell script that adds this argument to "$@" but I'm hoping to avoid that. (Update: Just to ensure my theory is right, tried this shell script hack and it does solve the problem. Just hoping for a cleaner solution)

Answer

BitwiseMan picture BitwiseMan · Dec 12, 2012

Use _JAVA_OPTIONS instead of JAVA_OPTS. _JAVA_OPTIONS will get picked up automatically when you run java.

export _JAVA_OPTIONS=-Djava.awt.headless=true
adt ... 

I know this is true on OS X. This indicates that this may work on Windows and Linux, as well.