« One month until the X… | Home | Xojo 2023r1 release »

Trying Java 19 in Xojo

Let's try the MBS Xojo Java Plugin for Xojo with Java 19.0.2. We go to the download page and pick the jdk-19_macos-aarch64_bin.dmg file from Oracle's download page.

We get the installer, run it and after it finishes, Java is installed. Let's try and open Terminal and run the

java --version

command to see what version we got:

java 19.0.2 2023-01-17
Java(TM) SE Runtime Environment (build 19.0.2+7-44)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)

The files are all installed into a folder in your /Library folder, e.g.

/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home

so the libjvm.dylib is in that folder and we load it in our project here:

JavaVMMBS.SetLibraryPath("/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/lib/server/libjvm.dylib")

Put this before calling the JavaVMMBS constructor. You still pass version 1.4 there as this is the version of the jni API and this part of Java hasn't changed or 10 years. The later Java versions changed compiler and added framework classes, but didn't change the jni interface.

We can query java.version or os.version here:

#If TargetMacOS Then // change the path for your installation! JavaVMMBS.SetLibraryPath("/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/lib/server/libjvm.dylib") #ElseIf TargetLinux Then // change path for your linux PC! JavaVMMBS.SetLibraryPath("/home/cs/jre1.6.0_05/lib/i386/client/libjvm.so") #EndIf // init Java Dim options() As String Dim javaVm As New JavaVMMBS(JavaVMMBS.JNI_VERSION_1_4, options, True) // Get system class Dim System As JavaClassMBS = javaVm.FindClass("java/lang/System") If System <> Nil Then // query method Dim transfomerGetPropertyId As JavaMethodMBS = System.GetStaticMethod("getProperty", "(Ljava/lang/String;)Ljava/lang/String;") If transfomerGetPropertyId <> Nil Then // make parameters Dim keyString As JavaStringMBS = javaVm.NewStringUTF8("java.version") Dim m As New MemoryBlock(8) m.Int64Value(0)=keyString.Handle // run it Dim r As JavaObjectMBS = System.CallStaticObjectMethod(transfomerGetPropertyId, m) If r<>Nil Then // show result Dim s As JavaStringMBS = JavaStringMBS(r) MsgBox s.CopyStringUTF End If End If End If

When we run this, it shows the version 19.0.2 here for the java.version property. Please note that we had recently to update the .long() calls to .Int64Value() since Xojo deprecated the old calls. If the library isn't found and loaded, the application may fail and get killed by macOS for accessing Java.framework, which is the default way to load.

Please note that we have to check the platform used for macOS since running an example compiler for Intel with a Java library built for Apple Silicon. The application may just quit in that case or better repport an error.

We'll fix the Java.framework thing. Let us know if you have questions. The bug fixes for Java 19 on macOS comes into version 23.2.

27 03 23 - 10:17