B4J Question Running jars under OpenJDK 11

agraham

Expert
Licensed User
Longtime 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.
B4X:
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.
 

Attachments

  • RunSelfNamedJarJdk11_v4.zip
    89 KB · Views: 541
Last edited:

aeric

Expert
Licensed User
Longtime User
Shell(ojp & "\bin\javaw", " --module-path " & ojp & "\javafx\lib --add-modules ALL-MODULE-PATH -jar " & Chr(34) & AppPath & "\" & args(0) & ".jar" & Chr(34))

Based on this command, I can create a batch file as below:
B4X:
@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
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Based on this command, I can create a batch file as below:
B4X:
@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
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
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Based on this command, I can create a batch file as below:
B4X:
@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
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.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime 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!?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime 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
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Anoter question, how would this file be launched by your solution?

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

agraham

Expert
Licensed User
Longtime 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:
Upvote 0

Cableguy

Expert
Licensed User
Longtime 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?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Hummmmm, I'm not even sure what THAT means!!!
Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables -> (System variables) New...
Variable name: OpenJdkPath
Variable value: c\:java\jdk11.01
-> OK
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
It is a valid command, in a bat file…
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
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime 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
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime 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...
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Apparently Dprism is about graphics acceleration…
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.
 
Upvote 0
Top