B4J Question Running jars under OpenJDK 11

Discussion in 'B4J Questions' started by agraham, May 28, 2019.

  1. agraham

    agraham Expert Licensed User

    With Java 8 you could run a jar on a Windows PC by double clicking on it. This does not work with OpenJDK so as I want to be able to quickly run some utility B4J programs easily without packaging them this is what I did.

    1) I installed OpenJDK in a convenient place, mine is in 'C:\jdk-11', and set up B4J to use it.

    2) I made an environment variable named 'OpenJdkPath' with the value of the OpenJDK path, in my case 'C:\jdk-11'.

    3) I wrote and compiled a trivial program in Basic4ppc called 'RunSelfNamedJarJdk11.sbp' that looks at what its .exe is named and invokes javaw.exe to run a co-located jar of the same name. Your Basic4ppc won't do this as one of the mods to my own version is to pass its own exe name as args(0). However you can probably do this in any other language that can create an exe that can identify itself.
    Code:
    Sub Globals
       
    'Declare any global variables here
       Dim appargsfile
       appargsfile = args(
    0) & ".args"
       
    Dim ThisVersion : ThisVersion = "4.0"
    End Sub

    Sub App_Start
       Obj1.New1
       Obj1.CreateNew(
    "System.Environment")
       envar = 
    "OpenJdkPath"
       ojp = Obj1.RunMethod2(
    "GetEnvironmentVariable", envar, "System.String")
       
    If ojp = cNull Then
           
    Msgbox("Environment variable '" & envar & "' not present!""OpenJDK Path Error")
       
    Else
           appargs = 
    ""
           javaops = 
    ""
           debug = 
    ""
           
    If FileExist(appargsfile) Then
               FileOpen(
    in, appargsfile, cRead)
               appargs = FileRead(
    in)
               javaops = FileRead(
    in)
               debug = FileRead(
    in)
               
    If javaops = EOF Then
                   javaops = 
    ""
               
    End If
               FileClose(
    in)
           
    End If
           
    If ArrayLen(args()) > 1 Then
               
    For j = 1 To  ArrayLen(args()) - 1
                   
    If StrIndexOf(args(j), " "0) > 0 Then
                       appargs = appargs & 
    " " & Chr(34) & args(j) & Chr(34)
                   
    Else
                       appargs = appargs & 
    " " & args(j)
                   
    End If
               
    Next
           
    End If       
           
    ' java options
           shellargs =  " --module-path " & ojp & "\javafx\lib --add-modules ALL-MODULE-PATH " & javaops & " "
           
    ' jar file
           shellargs = shellargs & "-jar " & Chr(34) & AppPath & "\" & args(0) & ".jar" & Chr(34)
           
    ' app arguments
           shellargs = shellargs & " " & appargs
           
    Shell(ojp & "\bin\javaw", shellargs)
           
    If debug <> EOF Then
               
    Msgbox("Runner Version "  & ThisVersion & CRLF & CRLF &  shellargs, "Shell Arguments")
           
    End If
       
    End If
    End Sub
    4) I place a copy of 'RunSelfNamedJarJdk11.exe' in the B4J project Objects folder next to the B4J jar and rename it to match the jar. Voila! you can now double click the exe and the jar will run without the hassle of packaging it.

    5) To pass arguments to the invoked jar place a file named <nameofjar>.args in the B4J project Objects folder. The first line of this file will be passed to the Args() array of App_Start. Remember that an argument item containing space characters will need to be delimited by quotes. There is no error raised if the file is not present. 'RunSelfNamedJarJdk11.exe' can also accept command line arguments that will be passed to the B4J program. These arguments wil lbe added to the end of any argument list present in the args file.

    6) To pass options to javaw.exe add a second line containing the required options to the <nameofjar>.args file.

    7) To examine the generated argument string passed to java.exe add a third line to the <nameofjar>.args file. This can be blank or contain anything, it doesn't matter. The presence of a third line triggers the display of a Msgbox.

    If you trust me you can use the zipped exe attached

    EDIT: Version 2 now posted that passes arguments to the jar when run.

    EDIT: Version 3 now posted passes java options as well.

    EDIT: Version 4 now posted accepts command line arguments.
     

    Attached Files:

    Last edited: May 31, 2019
    lemonisdead, jimmyF, Cableguy and 3 others like this.
  2. aeric

    aeric Active Member Licensed User

    Based on this command, I can create a batch file as below:
    Code:
    @ECHO OFF
    TITLE RUN A JAR 
    FILE
    CD C:\Java\jdk-
    11.0.1\bin
    javaw --module-
    path C:\Java\jdk-11.0.1\javafx\lib --add-modules ALL-MODULE-PATH -jar "C:\Development\App\Objects\App.jar"
    ::PAUSE
     
  3. Cableguy

    Cableguy Expert Licensed User

    Yes, but that approach implies having to edit the batch file to update the path each time you copy it to a new location.
    The solution proposed by Andrew has as single condition that it is placed on the same folder as the jar, and renamed it to the jars name
     
    DonManfred and aeric like this.
  4. agraham

    agraham Expert Licensed User

    Another problem with this is that the current folder is no longer the one where the jar is located which will give problems in accessing associated co-located files (if there are any).

    Also the command window will stay open until the jar exits.
     
    aeric likes this.
  5. Cableguy

    Cableguy Expert Licensed User

    [Feature Request:]
    Could you add the possibility to search for a "app_params.txt" file so that the jar could be run with custom additional parameters!?
     
  6. agraham

    agraham Expert Licensed User

    To keep things tidy how about a file named <nameofjar>.args whose first (and only) line is passed as arguments to the jar?
     
    Cableguy and jimmyF like this.
  7. Cableguy

    Cableguy Expert Licensed User

    That would do perfect!
     
  8. agraham

    agraham Expert Licensed User

    Cableguy's suggestion is now implemented in version 2 posted above.
     
    Cableguy likes this.
  9. Cableguy

    Cableguy Expert Licensed User

    Thanks Andrew, You ROCK!
     
  10. Cableguy

    Cableguy Expert Licensed User

    I keep getting the error message "Enviroment Variable….. Not found"

    I moved my jdk11.01 folder to c:/ as it was previously Under c:/java/jdk11.01 but same result
     
  11. Cableguy

    Cableguy Expert Licensed User

    Anoter question, how would this file be launched by your solution?

    java -jar -Dprism.order=sw myjar.jar -dark
     
  12. agraham

    agraham Expert Licensed User

    I've tested version 2 and it works for me. If you look at the code above the only reason for the message is that the environment variable named 'OpenJdkPath' wasn't found.
    Are you sure that you created the environment variable and it has the correct name. As an aside the value should be 'c\:java\jdk11.01' with a backslash, although that may not matter and the present problem is that it hasn't found 'OpenJdkPath'.
     
    Last edited: May 30, 2019
  13. Cableguy

    Cableguy Expert Licensed User

    Hummmmm, I'm not even sure what THAT means!!!
     
  14. agraham

    agraham Expert Licensed User

    Apart from the fact I use javaw to avoid leaving a comand console open how is that a valid command? The jar file name follows '-jar' and that doesn't look right.
     
  15. Cableguy

    Cableguy Expert Licensed User

    It is a valid command, in a bat file… Its meant to launch the ABMGridBuilder, and without the -Dprism.order=sw it just opens a blanck form!
    BTW, does this exe trick also apply to other JDK versions?
     
  16. agraham

    agraham Expert Licensed User

    Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables -> (System variables) New...
    Variable name: OpenJdkPath
    Variable value: c\:java\jdk11.01
    -> OK
     
    Last edited: May 30, 2019
    Cableguy likes this.
  17. agraham

    agraham Expert Licensed User

    Hmm, still looks odd to me. If -D is an argument to myjar I would have expected to see

    java -jar myjar.jar -Dprism.order=sw -dark


    In this case 'myjar.args' would contain the line

    -Dprism.order=sw -dark
     
  18. Cableguy

    Cableguy Expert Licensed User

    I will try to see if that works… anyway, it just a "case study" … its the only ready to run jar I have to test with
     
  19. Cableguy

    Cableguy Expert Licensed User

    Apparently Dprism is about graphics acceleration… so basically, some Java forms do not render propely when default acceleration is Hardware, so this forces it to be rendered by software...
     
  20. agraham

    agraham Expert Licensed User

    Right, I see it is setting a Java system property. I still think it is in the wrong place but maybe the java parsing of the command line is less strict regarding positioning than the specification which is

    java [options] -jar <jarfile> [args...]


    to execute a jar file. I would have expected

    java -Dprism.order=sw -jar myjar.jar -dark

    assuming that -dark is a parameter for myjar.
     
    Cableguy likes this.
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