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:

micro

Well-Known Member
Licensed User
Longtime User
.....my own private version of Basic4ppc
Ingact I can't open your sbp file, it says it was created with a new version.
My version is 6.9
My code get an error in args(0), in args(0) there should be the name of the application
but if I start it gives me error, ......out of index.

1659449605319.png
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ingact I can't open your sbp file, it says it was created with a new version.
To open it edit the first line of sbp file with Notepad++ or similar to read 'Version=6.9'

However
My code get an error in args(0), in args(0) there should be the name of the application

I have already stated in post #1 and referred to it in my reply to you in post #37
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.
 
Upvote 0
Top