How to dynamically locate jjs.exe on Windows machines?

I'm trying to run Nashorn on a Windows machine and would like to provide a simple start.bat file to start it.

On Unix-derivatives that is very easy; but on Windows, I struggle to reliably find the location of jjs.exe.

I don't want to tell my users: Oh, go find the location of the Java 8 SDK and edit start.bat to point it to that directory.

I tried:

$ where java

C:\ProgramData\Oracle\Java\javapath\java.exe

Which doesn't contain jjs.exe either.
JAVA_HOME or JDK_HOME isn't set by default, and the bin directory isn't in PATH either; so now it seems I have to rely on guessing default locations.

Are there any other options?

5

2 Answers

I don't think anything is going to be absolutely foolproof here, as there are a huge number of ways to potentially install Java (ZIP an installation on one machine and put it on another, for example). You don't tell us how your users are installing, or how you can even be sure they have Java 8 (which is required for jjs).

But probably good enough is to assume the user actually ran the installer. In that case, you can find Java information in the registry. See HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.8\JavaHome, which should give you the Java home directory of the most recent 1.8 installation (you can also look for specific releases under HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.8.0_*).

So, for example, on my test machine:

C:\Users\IEUser>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.8" /v JavaHome
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.8 JavaHome REG_SZ C:\Program Files\Java\jdk1.8.0_25
3

Here's a solution but I'm not sure how good it is. It assumes a JDK has been installed.

import java.io.File;
public class FindJJS { public static void main(String[] args) { System.out.println(System.getProperty("java.home") + File.separator + "bin" + File.separator + "jjs" + (System.getProperty("os.name").startsWith("Windows") ? ".exe" : "")); }
}

Compile it, put it in lib/

In start.bat:

for /f "usebackq tokens=*" %%a in (`java -cp lib FindJJS`) do "%%a"-scripting bin\yourscript.jjs -- %1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like