B4J Library jShell library

Erel

Administrator
Staff member
Licensed User
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.

Installation instructions:
- Download the attached zip file.
- Unzip it and copy jShell.xml and jShell.jar to the additional libraries folder or to the internal libraries folder.

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.
 

Attachments

Last edited:

wl

Well-Known Member
Licensed 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

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

Erel

Administrator
Staff member
Licensed User
What do you mean with run an external url? Do you want to download it? Do you want to show it in a browser?
 

derez

Expert
Licensed 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:

Erel

Administrator
Staff member
Licensed User
If you just want to start the jar file then you can use fx.OpenExternalDocument. If you want to do it with jShell then you need to start "java.exe -jar <your jar>"
 

derez

Expert
Licensed 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
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
 

Erel

Administrator
Staff member
Licensed User
This message is an IDE error. Ignore it. It has no effect on your app.
 
Top