Platforms to show: All Mac Windows Linux Cross-Platform

/Java/Java Classes in RB/Java
Function:
Required plugins for this example: MBS Java Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Java/Java Classes in RB/Java
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
EventHandler Sub Open() Init End EventHandler
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim 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() const JNI_VERSION_1_1 = &h00010001 const JNI_VERSION_1_2 = &h00010002 const JNI_VERSION_1_4 = &h00010004 if TargetLinux then // change path for your linux PC! JavaVMMBS.SetLibraryPath("/home/cs/jre1.6.0_05/lib/i386/client/libjvm.so") end if dim options(-1) as string dim 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() dim jclass as JavaClassMBS dim jmethod as JavaMethodMBS dim m as MemoryBlock dim s as String dim jfield as JavaFieldMBS dim jobject as JavaObjectMBS dim 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() dim jclass as JavaClassMBS dim jmethod as JavaMethodMBS dim jobject as JavaObjectMBS dim jstring as JavaStringMBS dim 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.CopyStringUTF MsgBox s End Sub
Protected Sub Test6() dim jclass as JavaClassMBS dim jmethod as JavaMethodMBS dim jobject as JavaObjectMBS dim jstring as JavaStringMBS dim jarray as JavaObjectArrayMBS dim 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.CopyStringUTF next End Sub
Protected Sub test1() dim jclass as JavaClassMBS dim jmethod as JavaMethodMBS dim jstring as JavaStringMBS dim jfield as JavaFieldMBS dim 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() Dim jclass, sclass As JavaClassMBS dim jmethod as JavaMethodMBS dim jstring as JavaStringMBS dim args as JavaObjectArrayMBS dim m as MemoryBlock dim s as String dim 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.NewStringUTF8(" 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() dim jclass as JavaClassMBS dim jarray as JavaObjectArrayMBS dim jstring,s as JavaStringMBS dim jobject as JavaObjectMBS if vm=nil then init jstring=vm.NewStringUTF8("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.CopyStringUTF 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
End Project

See also:

The items on this page are in the following plugins: MBS Java Plugin.


💬 Ask a question or report a problem
The biggest plugin in space...