B4J Library jShell library

Discussion in 'B4J Libraries & Classes' started by Erel, Nov 19, 2013.

  1. Erel

    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:

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

    Attached Files:

    Last edited: Aug 31, 2014
    koaunglay, wl and Theera like this.
  2. wl

    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) ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. peacemaker

    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: Dec 20, 2013
  5. Erel

    Erel Administrator Staff Member Licensed User

    Please do not post duplicate questions.
     
  6. imbault

    imbault Well-Known Member Licensed User

  7. Erel

    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?
     
  8. imbault

    imbault Well-Known Member Licensed User

    Last edited: Jan 10, 2014
  9. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    fx.ShowExternalDocument("your link")
     
    Mashiane likes this.
  10. imbault

    imbault Well-Known Member Licensed User

    Thank you Erel
     
  11. derez

    derez Expert Licensed User

    The shell works for me only if I change the jar to exe (with launch4j)
    Code:
    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: Feb 1, 2014
  12. Erel

    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>"
     
  13. derez

    derez Expert Licensed User

    Thank you. The first option does not work in non Ui .
    The shell works with jar like this:
    Code:
    shl.Initialize("shl""java.exe" , Array As String("-jar""Server_report.jar",name))
     
  14. Gary Miyakawa

    Gary Miyakawa Active Member Licensed User

    How do you "debug" jshell ? Is there an option to turn on logging ?

    Thanks,

    Gary M
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    StdOut and StdErr strings hold the process output.
     
  16. alienhunter

    alienhunter Active Member Licensed User

    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
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    You can track the process output by running it with RunWithOutputEvents.
     
    alienhunter likes this.
  18. alienhunter

    alienhunter Active Member Licensed User

    thanks i will try AH
     
  19. alienhunter

    alienhunter Active Member Licensed User

    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
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    This message is an IDE error. Ignore it. It has no effect on your app.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice