Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Java Plugin
Last modified Tue, 2nd Nov 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Java/JavaTest/Java
Download this example: Java.zip
Project "Java.xojo_binary_project"
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
test1
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
test2
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
test3
End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
Test4
End EventHandler
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action()
Test5
End EventHandler
End Control
Control PushButton6 Inherits PushButton
ControlInstance PushButton6 Inherits PushButton
EventHandler Sub Action()
Test6
End EventHandler
End Control
Control StaticText5 Inherits Label
ControlInstance StaticText5 Inherits Label
End Control
Control StaticText6 Inherits Label
ControlInstance StaticText6 Inherits Label
End Control
Control PushButton7 Inherits PushButton
ControlInstance PushButton7 Inherits PushButton
EventHandler Sub Action()
init
End EventHandler
End Control
Control PushButton8 Inherits PushButton
ControlInstance PushButton8 Inherits PushButton
EventHandler Sub Action()
vm=nil
End EventHandler
End Control
Control PushButton9 Inherits PushButton
ControlInstance PushButton9 Inherits PushButton
EventHandler Sub Action()
Thread1.run
End EventHandler
End Control
Control Thread1 Inherits Thread
ControlInstance Thread1 Inherits Thread
EventHandler Sub Run()
var jclass As JavaClassMBS
var jmethod As JavaMethodMBS
var m As MemoryBlock
var jfield As JavaFieldMBS
var jobject As JavaObjectMBS
var value As Integer
System.DebugLog "Thread started "+Me.ThreadID.ToString
If vm = Nil Then
System.DebugLog "no VM initialized."
Return
End If
// 1. Find class
jclass=vm.FindClass("test")
If jclass=Nil Then
System.debuglog "Can't find test class"
Return
End If
// 2. Call constructor
jmethod = jclass.GetMethod("<init>", "()V")
If jmethod=Nil Then
system.debuglog "Can't find constructor"
Return
End If
jobject=jclass.NewObject(jmethod, m)
If jobject=Nil Then
system.debuglog "Constructor failed!?"
Return
End If
// 3. get current value
jmethod=jclass.GetMethod("getValue","()I")
If jmethod=Nil Then
system.debuglog "Failed to find getValue method."
Return
End If
value=jobject.CallIntMethod(jmethod,Nil)
If value<>1000 Then
system.debuglog "test constructor should set value to 1000, but now it is "+Str(value)
Return
End If
// 4. Set a new value
jmethod=jclass.GetMethod("setValue","(I)V")
If jmethod=Nil Then
system.debuglog "Failed to find setValue method."
Return
End If
m=NewMemoryBlock(8) // 8 bytes for each argument!
m.Int32Value(0)=2000
jobject.CallVoidMethod(jmethod,m)
// 5. get current value again
jmethod=jclass.GetMethod("getValue","()I")
If jmethod=Nil Then
system.debuglog "Failed to find getValue method."
Return
End If
value=jobject.CallIntMethod(jmethod,Nil)
If value<>2000 Then
system.debuglog "value should be 2000, but now it is "+Str(value)
Return
End If
// 6. Set variable direct
jfield=jclass.GetField("value","I")
If jfield=Nil Then
system.debuglog "Failed to get the value property."
Return
End If
jobject.IntField(jfield)=3000
// 7. Read variable direct
jfield=jclass.GetField("value","I")
If jfield=Nil Then
system.debuglog "Failed to get the value property."
Return
End If
value=jobject.IntField(jfield)
If value<>3000 Then
system.debuglog "value should be 3000, but now it is "+Str(value)
Return
End If
// 8. increment
jmethod=jclass.GetMethod("increment","()V")
If jmethod=Nil Then
system.debuglog "Failed to find increment method."
Return
End If
jobject.CallVoidMethod(jmethod,Nil)
// 9. Read variable direct
jfield=jclass.GetField("value","I")
If jfield=Nil Then
system.debuglog "Failed to get the value property."
Return
End If
value=jobject.IntField(jfield)
If value<>3001 Then
system.debuglog "value should be 3001, but now it is "+Str(value)
Return
End If
System.debuglog "Everything ok."
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
EventHandler Sub Open()
Init
End EventHandler
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
var parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
var file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Protected Sub Init()
'JavaVMMBS.debuggging = True
#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
// defines what inteface you get for compatibility
Const JNI_VERSION_1_1 = &h00010001
Const JNI_VERSION_1_2 = &h00010002
Const JNI_VERSION_1_4 = &h00010004
Const JNI_VERSION_1_6 = &h00010006
Const JNI_VERSION_1_8 = &h00010008
Const JNI_VERSION_9 = &h00090000
Const JNI_VERSION_10 = &h000a0000
Const JNI_VERSION_19 = &h00130000
var options(-1) as string
var f as FolderItem=FindFIle("test.jar")
vm=new JavaVMMBS(JNI_VERSION_1_4, options, f, false)
if vm.Handle = 0 then
MsgBox "Can't create Java VM"
else
MsgBox "Java Initialized."
end if
End Sub
Protected Sub Test4()
var jclass As JavaClassMBS
var jmethod as JavaMethodMBS
var m as MemoryBlock
var jfield as JavaFieldMBS
var jobject as JavaObjectMBS
var value as integer
if vm=nil then init
// 1. Find class
jclass=vm.FindClass("test")
if jclass=nil then
msgbox "Can't find test class"
Return
end if
// 2. Call constructor
jmethod = jclass.GetMethod("<init>", "()V")
if jmethod=nil then
msgbox "Can't find constructor"
Return
end if
jobject=jclass.NewObject(jmethod, m)
if jobject=nil then
MsgBox "Constructor failed!?"
Return
end if
// 3. get current value
jmethod=jclass.GetMethod("getValue","()I")
if jmethod=nil then
MsgBox "Failed to find getValue method."
Return
end if
value=jobject.CallIntMethod(jmethod,nil)
if value<>1000 then
MsgBox "test constructor should set value to 1000, but now it is "+str(value)
Return
end if
// 4. Set a new value
jmethod=jclass.GetMethod("setValue","(I)V")
if jmethod=nil then
MsgBox "Failed to find setValue method."
Return
end if
m=NewMemoryBlock(8) // 8 bytes for each argument!
m.Int32Value(0)=2000
jobject.CallVoidMethod(jmethod,m)
// 5. get current value again
jmethod=jclass.GetMethod("getValue","()I")
if jmethod=nil then
MsgBox "Failed to find getValue method."
Return
end if
value=jobject.CallIntMethod(jmethod,nil)
if value<>2000 then
MsgBox "value should be 2000, but now it is "+str(value)
Return
end if
// 6. Set variable direct
jfield=jclass.GetField("value","I")
if jfield=nil then
MsgBox "Failed to get the value property."
Return
end if
jobject.IntField(jfield)=3000
// 7. Read variable direct
jfield=jclass.GetField("value","I")
if jfield=nil then
MsgBox "Failed to get the value property."
Return
end if
value=jobject.IntField(jfield)
if value<>3000 then
MsgBox "value should be 3000, but now it is "+str(value)
Return
end if
// 8. increment
jmethod=jclass.GetMethod("increment","()V")
if jmethod=nil then
MsgBox "Failed to find increment method."
Return
end if
jobject.CallVoidMethod(jmethod,nil)
// 9. Read variable direct
jfield=jclass.GetField("value","I")
if jfield=nil then
MsgBox "Failed to get the value property."
Return
end if
value=jobject.IntField(jfield)
if value<>3001 then
MsgBox "value should be 3001, but now it is "+str(value)
Return
end if
MsgBox "Everything ok."
End Sub
Protected Sub Test5()
var jclass as JavaClassMBS
var jmethod as JavaMethodMBS
var jobject as JavaObjectMBS
var jstring as JavaStringMBS
var s as string
if vm=nil then init
// 1. Find class
jclass=vm.FindClass("test")
if jclass=nil then
msgbox "Can't find test class"
Return
end if
// 2. Call method to return string
jmethod=jclass.GetStaticMethod("GetString","()Ljava/lang/String;")
if jmethod=nil then
MsgBox "Failed to find GetString method."
Return
end if
jobject=jclass.CallStaticObjectMethod(jmethod,nil)
if jobject=nil then
MsgBox "GetString returned nil!."
Return
end if
jstring=JavaStringMBS(jobject)
s= jstring.StringValue
MsgBox s
End Sub
Protected Sub Test6()
var jclass as JavaClassMBS
var jmethod as JavaMethodMBS
var jobject as JavaObjectMBS
var jstring as JavaStringMBS
var jarray as JavaObjectArrayMBS
var i,c as integer
if vm=nil then init
// 1. Find class
jclass=vm.FindClass("test")
if jclass=nil then
msgbox "Can't find test class"
Return
end if
// 2. Call method to return string array
jmethod=jclass.GetStaticMethod("GetStringArray","()[Ljava/lang/String;")
if jmethod=nil then
MsgBox "Failed to find GetStringArray method."
Return
end if
jobject=jclass.CallStaticObjectMethod(jmethod,nil)
if jobject=nil then
MsgBox "GetString returned nil!."
Return
end if
// make an array object. Be sure that this object is an object array!
jarray=vm.NewObjectArray(jobject)
// now use the array
c=jarray.Length
if c<>3 then
MsgBox "Array length should be 3!"
Return
end if
for i=0 to c-1
jobject=jarray.ArrayElement(i)
jstring=JavaStringMBS(jobject)
MsgBox jstring.StringValue
next
End Sub
Protected Sub test1()
var jclass As JavaClassMBS
var jmethod as JavaMethodMBS
var jfield as JavaFieldMBS
var sa(0) as string
if vm=nil then init
jclass=vm.FindClass("test")
if jclass=nil then
msgbox "Can't find test class"
else
jmethod = jclass.GetStaticMethod("main", "([Ljava/lang/String;)V")
if jmethod=nil then
msgbox "Can't find HelloWorld.main"
else
sa(0)=" from Realbasic! (test1) äöü"
jclass.CallStaticMain(sa)
// now read the variable n
jfield=jclass.GetStaticField("n","I")
if jfield<>nil then
MsgBox str(jclass.StaticIntField(jfield))
end if
end if
end if
End Sub
Protected Sub test2()
var jclass, sclass As JavaClassMBS
var jmethod as JavaMethodMBS
var jstring as JavaStringMBS
var args as JavaObjectArrayMBS
var m as MemoryBlock
var jfield as JavaFieldMBS
if vm=nil then init
jclass=vm.FindClass("test")
if jclass=nil then
msgbox "Can't find test class"
else
jmethod = jclass.GetStaticMethod("main", "([Ljava/lang/String;)V")
if jmethod=nil then
msgbox "Can't find HelloWorld.main"
else
jstring = vm.NewStringUnicode(" from Realbasic! (test2)")
if jstring=nil then
msgbox "Out of memory"
else
sclass=vm.FindClass("java/lang/String")
args = vm.NewObjectArray(1, sclass, jstring)
if args=nil then
msgbox "Out of memory"
else
args.ArrayElement(0)=jstring
m=NewMemoryBlock(8) // 8 bytes per parameter
m.Int64Value(0)=args.Handle
jclass.CallStaticVoidMethod(jmethod, m)
// now read the variable n
jfield=jclass.GetStaticField("n","I")
if jfield<>nil then
MsgBox str(jclass.StaticIntField(jfield))
end if
end if
end if
end if
end if
End Sub
Protected Sub test3()
var jclass as JavaClassMBS
var jarray as JavaObjectArrayMBS
var jstring,s as JavaStringMBS
var jobject as JavaObjectMBS
if vm=nil then init
jstring=vm.NewStringUnicode("Hello")
jclass=vm.FindClass("java/lang/String")
jarray = vm.NewObjectArray(1, jclass, jstring)
jobject=jarray.ArrayElement(0)
// The plugin gives you a JavaStringMBS object if it knows that it is a string
s=JavaStringMBS(jobject)
if s=nil then
MsgBox "s=nil!?" // does not work in RB 4.5 correctly (encoding maybe)
else
MsgBox s.StringValue
End If
End Sub
Property Protected vm As JavaVMMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu7 = ""
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu6 = ""
MenuItem UntitledMenu5 = ""
MenuItem UntitledMenu4 = ""
End MenuBar
Class App Inherits Application
End Class
Sign
End Sign
End Project
Download this example: Java.zip
The items on this page are in the following plugins: MBS Java Plugin.