Run java bundled in .app from command-line

command linejavaterminal

The question is how do I run some application written in java and bundled into .app package from command-line?

The main purpose of this is to run an application under JDK7. So I will start it with the java bundled in JDK7 instead of default one.

I need something like https://apple.stackexchange.com/a/10253/15553 but with java, so I can run it with a different JDK.

Best Answer

To switch to another JVM, try to modify the JAVA_HOME and PATH shell environment variable in ~/.profile.

# in ~/.profile
# switch to another JVM (here, 1.7)
unset JAVA_HOME PATH
export JAVA_HOME="$(/usr/libexec/java_home -F --version 1.7 2>/dev/null)"
export PATH="${JAVA_HOME}/bin:${PATH}"

# general info on Java preferences
open -a 'Java Preferences'