vatbub/javaAutoStart

Launching JVM on the same folder

Closed this issue · 7 comments

Hello Fred,

We have a folder named "java" with a JDK on the same location where the jar is placed. This is done to avoid having to install JDK separately and simply "click to run" our app.

How can we instruct javaAutoStart to launch the jar file using this interpreter found in .\java\bin\java.exe ?

Code from our side is java instead of kotlin. The documentation isn't precise about the configuration and I was not able to understand on my own. Thank you in advance.

vatbub commented

Hi Max,
AutoStartManager uses System.getProperty("java.home") to get the location of the JRE. Hence, you can use System.setProperty("java.home", "/path/to/your/jre") before calling AutoStartManager to customize the JRE location. Note that AutoStartManager automatically resolves the bin subdirectory of the JRE, so "/path/to/your/jre" should not include that. Also, I would strongly recommend that you resolve your relative JRE location to an absolute path to avoid any ambiguities like so:

val absoluteJrePath = File("java").absolutePath
System.setProperty("java.home", absoluteJrePath)
val manager = AutoStartManager(appId)
// ...

I will close the issue now, but feel free to reopen it if you have any further questions.
Cheers :)
Freddie

Freddie, thank you for the quick and insightful reply.

The solution looked simple and elegant. Unfortunately still failing.

This was the code written. It uses the absolute path but it wasn't working, so I've added canonical path to remove doubt. I've also tried to change the app id to assure it would start something fresh. btw, where can I erase the older registry entries?

    // setup our custom JVM location
    File folderJVM = new File(".", "java"); 
    String folderText = folderJVM.getAbsolutePath();
    try{
        folderText = folderJVM.getCanonicalPath();
    }catch(Exception e){
        e.printStackTrace();
    }
    System.setProperty("java.home", folderText);

At this point I'm suspecting that it is either because the username has spaces such as "John Doe" or that it isn't being launched on the same working directory, or something else different.

Any ideas?

Thanks!

Ahh.. I was finding the appropriate registry key at HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

It is writing:

"C:\Users\John Doe\Desktop\distro\java\bin\javaw.exe" -jar "C:\Users\John Doe\Desktop\distro\jar-with-dependencies.jar"

The problem here is "javaw" that is called instead of "java".

How can I set the Interpreter as option on the config?

vatbub commented

Hmm, if your JDK/JRE includes javaw.exe, this should work nevertheless. In any case, you can set the interpreter to use like so:

File jarFileLocation = null;
try {
    jarFileLocation = new File(AutoStartDemo.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

AutoStartManager manager = new AutoStartManager(appId);

manager.addToAutoStart(
        new AutoStartLaunchConfig(jarFileLocation, Interpreter.java)
);

Keep in mind that you need to replace AutoStartDemo.class with the name of your class. Unfortunately, this is necessary because Java does not support named arguments like Kotlin does.

vatbub commented

In general, I would recommend you though to use javaw if possible as java.exe usually opens a conhost.exe-window as well, which you usually don't want for an autostart-app.

Thank you, that was a fantastic explanation! Everything is solved and working.

Off-topic, but since you like text games then I'd invite you to join to play Lord 1 (Legend Of the Red Dragon) at an active BBS that you can telnet. Details are here: https://cavebbs.com/ or just telnet cavebbs.homeip.net I know this is online but it is fun to play with more people and there a nice group around the place.

Again, thanks. Please close the ticket.