Java Question Call Java Class from library by name without variable definition

fghdfg

New Member
Hello every one,
Is it possible to use java class from library directly?
In file library we can call methods directly for instance
B4X:
File.delete()
But I can't call my class like that for example I have class names Utils
Java:
public class Utils {

 public void test(){
  // DO SOME OPERATION
 }

}
I intend to call Utils in b4a like that,
B4X:
Utils.test()
but Utils is not recognized, I have to add ShortName annotation and declare variable then call test method from variable like below.
Java:
@BA.ShortName("Utils")
public class Utils {
    public void test(){
        
    }
}
in b4a,
B4X:
dim utils as Utils
utils.test()
Any help would be appreciated
 

drgottjr

Expert
Licensed User
Longtime User
assuming you must write your library in java, your choices are:
1) inline java with a javaobject
2) build the library with the simple library compiler

using the simple library compiler is beyond the scope of this reply (search "simple library
compiler" in the search box above right).

but for inline java, here is 1 way to do it. (attached image shows result).

B4X:
    Dim jo As JavaObject
    jo.InitializeNewInstance(Application.PackageName & ".main$Utils",Null)
    jo.RunMethod("test",Null)


#If Java
public static class Utils {

 public void test(){
  BA.Log("I'm doing something, just like you wanted.");
 }

}
#End If


note:this solution works for this particular case. if you change your code, you may need a
different solution. in other words, if you change your code, please don't say, "it doesn't work".
the solution works for this case. start a new thread if you make changes.

in order to use "File.delete", etc, you'll need to use the simple library compiler. use of the javaobject will achieve the same result. you could also write your library in b4x.
 

Attachments

  • capture.png
    capture.png
    23.9 KB · Views: 211
Last edited:
Top