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.b4x.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:

wl

Well-Known Member
Licensed User
Longtime User
The example seems to use a Windows folder as the WorkingDirectory.

I guess it can also be used within Linux to launch Linux commands ?

Can it also be used to launch a Linux command which pipes out its output (to be read in the event handler) ?
 

peacemaker

Expert
Licensed User
Longtime User
I cannot understand how to run .exe and exit my app. My app closes the starting app2 also when exiting.
 
Last edited:

derez

Expert
Licensed User
Longtime User
The shell works for me only if I change the jar to exe (with launch4j)
B4X:
shl.Initialize("shl", "server_report.exe", Array As String(name))
otherwise there is an error saying that this is not a windows executable. The application is a UI one.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Thank you. The first option does not work in non Ui .
The shell works with jar like this:
B4X:
 shl.Initialize("shl", "java.exe" , Array As String("-jar", "Server_report.jar",name))
 

alienhunter

Active Member
Licensed User
Longtime User
StdOut and StdErr strings hold the process output.

Hi , is there a way to find out if "example.exe"(dos cmd) has finished downloading a file with Jshell if the "example.exe" does not close if download is complete ?
or i just have to track the bytes written and compare if the file size has changed in the last second
AH
 

alienhunter

Active Member
Licensed User
Longtime User
You can track the process output by running it with RunWithOutputEvents.

Finaly i got to use this but there is a Message "XML was improperly formatted ..."
it will crash the app
Any clues
thanks AH

7-29-2014 10-32-07 AM.png
 
Top