B4J Tutorial Starting correct JRE (8 or 11) via batchfile (Windows)

Discussion in 'B4J Tutorials' started by OliverA, Jun 4, 2019.

  1. OliverA

    OliverA Well-Known Member Licensed User

    Foreword:
    This solves a very specific case that may just apply to me. For current development, I'm still on JDK version 8. But for future coding/testing, I also have JDK 11 installed. I often launch the applications from a command prompt and may forget that a .jar file was created with JDK 11, at which point I'll have to Control-C quite a bit to abort an application. Also, starting a JavaFX jar file under 11 is a tad tedious from the command line (not as simple as java -jar myb4japp.jar). Using this batch file, I don't have to worry about which version of JDK was used to create the .jar file. I also associated this batch file with the .jar files under Windows Explorer, allowing me just to run any .jar file on my system (without worrying about which JRE to use). Could I just run all .jar files with version 11? Maybe, but my requirements are that version 8 produces .jar files run with version 8 JRE and version 11 .jar files with version 11 JRE. Like I said, this may be a very specific requirement that may not apply to anyone else. If you are looking for a simpler means of just starting JDK 11 created jars, you may want to look at

    1) https://www.b4x.com/android/forum/threads/running-jars-under-openjdk-11.106201/
    2) https://www.b4x.com/android/forum/threads/runner-to-start-jar-with-open-jdk.106230/

    Prerequisites:
    1) Windows 7 and up
    2) Running version of JDK/JRE version 8. In my case I'm using Amazon's Corretto 8 (https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html).
    3) OpenJDK 11 provided by AnywhereSoftware (download from here: https://www.b4x.com/b4j.html)
    4) Batch file named selectjre.cmd with the following content:
    Code:
    @ECHO OFF

    REM Some of the resources used:
    REM https://docs.oracle.com/javase/tutorial/deployment/jar/
    view.html
    REM https://ss64.com/nt/
    REM https://www.dostips.com/DtTipsStringManipulation.php
    REM https://www.computerhope.com/forum/index.php/topic,
    63415.msg405034.html?PHPSESSID=kio2bn5g19aqbs16leh2r3a936#msg405034
    REM https://stackoverflow.com/a/
    1096159

    REM Change JAVA11PATH 
    as needed
    SET JAVA11PATH=
    "C:\OpenJDK\jdk-11.0.1"
    SET JAVAFXUSED=
    FALSE

    REM Use jar command 
    to
    REM 
    1) Find a class name to use for javap command
    REM 
    2To determine if JavaFX is used
    SETLOCAL EnableDelayedExpansion
    FOR /F "delims==" %%a IN ('jar -tf %1') DO (
       SET   lastline=%%a
       
    IF /I "!lastline:~0,11!"=="com/javafx/" (SET JAVAFXUSED=TRUE)
       )

    REM Remove the trailing .class extension from the class name found. Necessary
    REM 
    in order fo javap to work properly
    FOR /F "tokens=1, 2 delims=." %%a IN ("%lastline%"DO SET class=%%a

    REM Use javap 
    to determine version of Java used to compile class
    javap -verbose -p -classpath test.jar %class% | FINDSTR 
    "major version: 53" > Nul

    REM Run .jar 
    file with proper Java version
    IF %ERRORLEVEL% EQU 0 (
       
    "%JAVA11PATH%\bin\java.exe" --module-path "%JAVA11PATH%\javafx\lib" --add-modules ALL-MODULE-PATH -jar %1
    ELSE (
         java -jar %
    1
    )

    REM 
    If .jar file is a non-gui app, pause before exiting
    IF /I %JAVAFXUSED%==FALSE (PAUSE)
    Notes:
    1) In the batch file, you need to modify SET JAVA11PATH= to the path where you installed AnywhereSoftware's OpenJDK 11.
    2) Place the selectjre.cmd file in the C:\Windows directory. This allows you to call the batch file from anywhere within a command prompt
    3) If you want to associate this batch file with .jar files within Windows Explorer, then, using Windows Explorer
    1. Right click on any .jar file on your computer and select Properties
    2. In the General tab, click on the Change button next to "Open with:"
    3. Click on "More apps"
    4. Scroll down (if needed) until you see "Look for another app on this PC" and click on it
    5. Browse to where you saved the above batch file and select it and click on Open
    6. You should be back on the General tab. Click on Apply and then on Ok
    Now when you launch your Java applications, the batch file will be used to launch them.

    Known limitations:
    1) This is not a battle tested solution. It seems to work for me at the present
    2) This does not handle any extra parameters that you would like to pass on to your java application via the command line

    Suggestions and corrections are welcome.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice