B4J Question How to create b4j library?

Shay

Well-Known Member
Licensed User
How to create b4j library? I didn't see any tutorial for this
Also which one is the easiest to create b4i, b4a, b4j? (if I never did it before)
 

Shay

Well-Known Member
Licensed User
This is nice website
but I still don't understand how do I take existing Java code (for example I took the jsch library)
and make it b4J library
I assume I need to create wrapper, but I have no idea how to do it (I cannot see any tutorial for it)
 

rwblinn

Well-Known Member
Licensed User
Hi Shay,

either you wrap the jSch Library or you use JavaObject to access.
Example lexecuting command 'ls' tested on Raspberry Pi and TinkerForge RED Brick Host.
YES, its is bit of JavaObject juggle, but works fine - soon InLine Java will come, which makes these kind of code easier :)
B4X:
'Example executing ls command on remote host using jSch
'Ensure     #AdditionalJar: jsch-0.1.51
Sub ExecuteRemoteCommand
    Dim hostcmd As String = "ls"
    Dim user As String = "<USERNAME>"
    Dim host As String = "<HOST ADDRESS>"
    Dim port As Int = 22
    Dim pw As String = "<PASSWORD>"
    Dim joSch As JavaObject
    'Java: JSch js = new JSch();
    joSch.InitializeNewInstance("com.jcraft.jsch.JSch", Null)
    'Java: Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    Dim joS As JavaObject
    joS = joSch.RunMethod("getSession", Array(user, host, port))
    'Java: s.setPassword("mypassword");
    joS.RunMethod("setPassword", Array(pw))
    'Java: Properties config = new Properties();
    Dim joP As JavaObject
    joP = joP.InitializeNewInstance("java.util.Properties", Null)
    'Java: config.put("StrictHostKeyChecking", "no");
    joP.RunMethod("setProperty", Array("StrictHostKeyChecking", "no"))
    'Java: s.setConfig(config);
    joS.RunMethod("setConfig", Array(joP))
    'Java: s.connect();
    joS.RunMethod("connect", Null)
    Log("Connected to Host...")
    Log("Invoking Command..." & hostcmd )
    'Java: Channel c = s.openChannel("exec");
    Dim joC As JavaObject = joS.RunMethod("openChannel", Array("exec"))
    joC.RunMethod("setCommand", Array(hostcmd ))
    'Java: joC.RunMethod("setCommand", Array("ls -l"))
    Log("Invoking Command...Connect")
    joC.RunMethod("connect", Null)
    Log("InputstreamReader...Build")
    Dim joISR As JavaObject
    'Java: BufferedReader reader = new BufferedReader(new InputStreamReader(cE.getInputStream()));
    joISR.InitializeNewInstance("java.io.InputStreamReader", Array(joC.RunMethod("getInputStream", Null)))
    Log("BufferedReader...Reading")
    Dim joBR As JavaObject
    joBR.InitializeNewInstance("java.io.BufferedReader", Array(joISR))
    'Java: joBR.RunMethod("readLine", Null)
    Log("Result Command " & hostcmd )
    Dim s As String
    Do While s.Contains("null") = False
        s = joBR.RunMethod("readLine", Null)
        If s <> "null" Then Log(s)   
    Loop
End Sub
 

Shay

Well-Known Member
Licensed User
Thanks, but some newbie video tutorial is needed for taking any library in java and transfer it to b4j / b4a is needed.
Also I think the most limiting issue B4X has is the libraries, since if I knew how to write in java, I probably have used standard android/java coding and not b4x

so I think some "generic" tool is needed for converting any java/android library is needed here, I even willing to pay (which I did in the past) to someone for getting library conversion(but this need to be reasonable price)
 

Shay

Well-Known Member
Licensed User
I know, I am just saying, this magic feature (as you did in b4A, and b4I) will take you to the next level ;-)
but in the meanwhile, can you post some video tutorial on how to do it for someone with basic java skills
from A to Z (taking existing java library and make it b4a/b4j library)
 

B4JExplorer

Active Member
Licensed User
Hi Shay,

either you wrap the jSch Library or you use JavaObject to access.
Example lexecuting command 'ls' tested on Raspberry Pi and TinkerForge RED Brick Host.
YES, its is bit of JavaObject juggle, but works fine - soon InLine Java will come, which makes these kind of code easier :)
B4X:
'Example executing ls command on remote host using jSch
'Ensure     #AdditionalJar: jsch-0.1.51
Sub ExecuteRemoteCommand
    Dim hostcmd As String = "ls"
    Dim user As String = "<USERNAME>"
    Dim host As String = "<HOST ADDRESS>"
    Dim port As Int = 22
    Dim pw As String = "<PASSWORD>"
    Dim joSch As JavaObject
    'Java: JSch js = new JSch();
    joSch.InitializeNewInstance("com.jcraft.jsch.JSch", Null)
    'Java: Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    Dim joS As JavaObject
    joS = joSch.RunMethod("getSession", Array(user, host, port))
    'Java: s.setPassword("mypassword");
    joS.RunMethod("setPassword", Array(pw))
    'Java: Properties config = new Properties();
    Dim joP As JavaObject
    joP = joP.InitializeNewInstance("java.util.Properties", Null)
    'Java: config.put("StrictHostKeyChecking", "no");
    joP.RunMethod("setProperty", Array("StrictHostKeyChecking", "no"))
    'Java: s.setConfig(config);
    joS.RunMethod("setConfig", Array(joP))
    'Java: s.connect();
    joS.RunMethod("connect", Null)
    Log("Connected to Host...")
    Log("Invoking Command..." & hostcmd )
    'Java: Channel c = s.openChannel("exec");
    Dim joC As JavaObject = joS.RunMethod("openChannel", Array("exec"))
    joC.RunMethod("setCommand", Array(hostcmd ))
    'Java: joC.RunMethod("setCommand", Array("ls -l"))
    Log("Invoking Command...Connect")
    joC.RunMethod("connect", Null)
    Log("InputstreamReader...Build")
    Dim joISR As JavaObject
    'Java: BufferedReader reader = new BufferedReader(new InputStreamReader(cE.getInputStream()));
    joISR.InitializeNewInstance("java.io.InputStreamReader", Array(joC.RunMethod("getInputStream", Null)))
    Log("BufferedReader...Reading")
    Dim joBR As JavaObject
    joBR.InitializeNewInstance("java.io.BufferedReader", Array(joISR))
    'Java: joBR.RunMethod("readLine", Null)
    Log("Result Command " & hostcmd )
    Dim s As String
    Do While s.Contains("null") = False
        s = joBR.RunMethod("readLine", Null)
        If s <> "null" Then Log(s)   
    Loop
End Sub
One of the confusions that I originally had about generating a B4J-compatible library, is that the reference to 'coding', meant that we needed the source code, and had to modify that with annotations, in order to make it work. But it's clear now, that we just have to write wrappers for the classes in the jar.

Anyway,

"soon InLine Java will come, which makes these kind of code easier"

You mean, in the context of B4A/J? We will be able to insert java snippets inside a B4J module? Sorry, I haven't been reading through all the announcements.


Thanks for the jsch examples, rwblinn. Having a problem with one of the wrappers for this, and am starting to enjoy using the JavaObject library, more & more.
 

rwblinn

Well-Known Member
Licensed User
Hi,

as a followup using InLine Java, find the previous example converted using B4J 2.80. Tested again under Raspian and Debian.
Pretty simple to use and a really cool feature of B4A / B4J.
Note that the returned Java List is straight used as a B4J List.

B4X:
Sub Process_Globals
   Public NativeMe As JavaObject
...

Sub AppStart (Form1 As Form, Args() As String)
  MainForm = Form1
  NativeMe = Me
  Dim l As List = NativeMe.RunMethod("jschexeccmd", Array("username", "hostaddress", "password", "hostcommand"))
  Log("Result (Lines:" & l.Size & ")")
  For i = 0 To l.Size - 1
    Log(l.Get(i))
  Next
End Sub

#If JAVA
import java.util.*;
import java.io.*;
import com.jcraft.jsch.*;

public static List<Object> jschexeccmd(String user, String host, String pw, String cmd) {
    List<Object> listJava = new ArrayList<Object>();
    try{
        JSch js = new JSch();
        Session s = js.getSession(user, host, 22);
        s.setPassword(pw);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        s.setConfig(config);
        s.connect();
        Channel c = s.openChannel("exec");
        ChannelExec ce = (ChannelExec) c;
        ce.setCommand(cmd);
        ce.setErrStream(System.err);
        ce.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
              listJava.add(line);
        }
        ce.disconnect();
        s.disconnect();
    }
    catch(Exception e){
        System.out.println(e);
    }
    return listJava;
}
#End If
 

B4JExplorer

Active Member
Licensed User
Hi,

as a followup using InLine Java, find the previous example converted using B4J 2.80. Tested again under Raspian and Debian.
Pretty simple to use and a really cool feature of B4A / B4J.
Note that the returned Java List is straight used as a B4J List.

B4X:
Sub Process_Globals
   Public NativeMe As JavaObject
...

Sub AppStart (Form1 As Form, Args() As String)
  MainForm = Form1
  NativeMe = Me
  Dim l As List = NativeMe.RunMethod("jschexeccmd", Array("username", "hostaddress", "password", "hostcommand"))
  Log("Result (Lines:" & l.Size & ")")
  For i = 0 To l.Size - 1
    Log(l.Get(i))
  Next
End Sub

#If JAVA
import java.util.*;
import java.io.*;
import com.jcraft.jsch.*;

public static List<Object> jschexeccmd(String user, String host, String pw, String cmd) {
    List<Object> listJava = new ArrayList<Object>();
    try{
        JSch js = new JSch();
        Session s = js.getSession(user, host, 22);
        s.setPassword(pw);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        s.setConfig(config);
        s.connect();
        Channel c = s.openChannel("exec");
        ChannelExec ce = (ChannelExec) c;
        ce.setCommand(cmd);
        ce.setErrStream(System.err);
        ce.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
              listJava.add(line);
        }
        ce.disconnect();
        s.disconnect();
    }
    catch(Exception e){
        System.out.println(e);
    }
    return listJava;
}
#End If
Whooooa! This is completely unexpected. I badly need inline code, it'll save tons of Java - B4x translation time. (Also, thanks for the sample above rwblinn, that will also save time trying to figure out how to use it, inside B4J).

Also, just noticed in the announcements, that they have a Groovy-style String feature, in the beta.

I feel like a real nerd, but this is actually exciting.

So as of last year, they made Web Development much easier, than any other language out there. Now they are shutting down any remaining inconveniences, when working with external Jars.

Thank You to the developers. What a great gift, for a new year.
 

Kevin Golding

Member
Licensed User
Hello,

I'm trying to find the jsch library (file:jsch-0.1.51.jar) with no success. Any chance of a link to it please?

Thanks,

Kevin
 
Top