jkutner / mjruby

A new jruby launcher built with mruby-cli

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Detect native JAVA_HOME

jkutner opened this issue · comments

When JAVA_HOME is not set, and the java that is on the PATH is in a location such as /usr/bin, it is not possible to easily detect the location of the JAVA_HOME directory.

Different platforms use different locations, such as:

  • /Library/Java/JavaVirtualMachines/$VERSION/Contents/Home on Mac
  • /usr/lib/jvm on Linux
  • C:\Program Files\Java on Windows

It is unclear if searching these directories is the correct approach. Doing so adds significant time to the start-up of the command.

As an alternative, if java is on the PATH, we could launch an out-of-process JVM.

Another example on linux mint: /usr/lib/jvm/default-java/jre/lib/amd64/jamvm/libjvm.so

It looks like the key on Linux is:

$ readlink -f /usr/bin/java | sed "s:bin/java::"

If I can translate that into Ruby code that would be great.

On Mac, it's:

$ readlink /usr/bin/java 

But that only returns the path to some directory with commands. That directory contains a java_home script, which maybe what we need.

On windows, the registry is the place to look:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

Example: https://github.com/jruby/jruby-launcher/blob/master/jvmlauncher.cpp#L439-L454

I think I have Mac covered with 96b583b

Covered for Windows: 96f4587

I think Linux is fixed too. need to test.

Tested