0

On my Mac (El-Capitan), I have added the following command into my bash_profile file:

export JAVA_HOME="$(/usr/libexec/java_home)"

This perfectly sets the correct java_home environment var. However, on one of my machines I don't have java installed and I don't want to install java :).

I have a dotfiles repository, so on all of my machines I have the same .bash_profile file. Now the problem is that on the machine without Java, the command above outputs this:

Unable to find any JVMs matching version "(null)". No Java runtime present, try --request to install.

Now my question is: how can I get rid of the error / warning, without changing the meaning of the command?

Rogier Lommers
  • 2,263
  • 3
  • 24
  • 38

1 Answers1

0

Probably packing it into an if-statement, checking if java is actually installed? something like this:

if ls /Library/Java/JavaVirtualMachines/j* 1> /dev/null 2>&1; then
  export JAVA_HOME="$(/usr/libexec/java_home)"
fi

This uses the return code of ls (thanks to this question/answer: Check if a file exists with wildcard in shell script) to figure out if there are any files or directories starting with a j (like jdk1.8.0_92.jdk or jre1.8.0_92.jre). And only if such a JDK/JRE exists, it tries to export JAVA_HOME.

Community
  • 1
  • 1
cello
  • 5,356
  • 3
  • 23
  • 28