B4J Library jShell library

The jShell library allows you to start other programs. It is based on Apache Commons Exec project: http://commons.apache.org/proper/commons-exec/index.html

The programs are always started asynchronously. ProcessComplete event is raised when the process completes.

The following code will run the Java program we previously created:

B4X:
Sub AppStart (Args() As String)
   Dim shl As Shell
   shl.Initialize("shl", "java", _
     Array As String("-cp", "curl.jar", "b4j.example.main", "http://www.basic4ppc.com"))
   shl.WorkingDirectory = "C:\Users\H\Documents\B4J\Curl\Objects"
   shl.Run(10000) 'set a timeout of 10 seconds
   StartMessageLoop 'need to call this as this is a console app.
End Sub


Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success AND ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
   Else
     Log("Error: " & StdErr)
   End If
   ExitApplication
End Sub
The Shell library can be used in a UI app in the same way.


V1.30 is released - Includes a method to start a process and wait for it to complete. You should normally not use this method in a UI application as it will block the UI thread.
 
Last edited:

Derek Johnson

Active Member
Licensed User
Longtime User
Tested, not working :)
I can´t get any command to work, not even:

B4X:
shl.Initialize("shl","ls", Null)
'or
shl.Initialize("shl","/bin/bash", Array as String("ls"))

I get not return in Process Completed or does the command work

Help, Please !! :(:(:(:(:(
I really need to issue "commands" to linux

Try using this: (this is in a UI based app using Tiny Core Linux)

B4X:
Sub Test
    Dim shl As Shell
    shl.Initialize("shl", "sudo",  Array As String("ls"))
    shl.WorkingDirectory = "/"
    shl.Run(10000) 'set a timeout of 10 seconds
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Success")
        Syslog(StdOut)
    Else
        Log("Error: " & StdErr)
    End If
End Sub

Just figured this out!
 

Pedro Caldeira

Active Member
Licensed User
Longtime User
doesn't work.
Get Exit code -1 in the result and does nothing
I really need to run some scripts called from B4J, so any help is welcome

B4X:
shl.initialize("shl","sudo", Array as String("./gtPro.sh")
shl.WorkinDirectory = file.DirApp
shl.run(10000)
 

OliverA

Expert
Licensed User
Get Exit code -1 in the result and does nothing
What is the output of StdErr and StdOut?
B4X:
Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut AsString, StdErr AsString)

Edit: What is the result of Success?
 
Top