B4J Question Output Events: Set Class Global Variable As StdOut/StdErr?

LWGShane

Well-Known Member
Licensed User
Longtime User
I'm working on OpenLibX 7.0 and I'm wondering if it's possible to reroute a Shell's StdOut and StdErr events to a public variable in Class_Globals. My "Computer" module is attached.


B4X:
Dim Com as Computer
Com.Initialize
Com.Execute("java -version", True, False)
Log (Com.ReadAll)

So basically, I'm wanting to have the "ReadAll" variable in "Com" actually contain StdOut and StdErr so I can use it without having to write the output to a text file.
 

Attachments

  • Computer.bas
    2.3 KB · Views: 186
Last edited:

LWGShane

Well-Known Member
Licensed User
Longtime User
Another option is to run it with RunSynchronous and then get the outputs from the ShellSyncResult object.
I have some code but I still can't read the Shell's output. My new module is attached.

Example Usage:
B4X:
Dim P As Process
P.Initialize
P.Execute("java -version", True, False)
Log (P.ReadAll)

The "ReadAll" command should work. I'm unsure as to why it isn't.
 

Attachments

  • Process.bas
    1.4 KB · Views: 241
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that your code only makes it more complicated to correctly use the Shell object. Maybe create a simple method that splits the arguments.

You should run the process and return the ShellSyncResult object. Otherwise you will need to run the process multiple times if you want to check the results.
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
@Erel
I have simplified the code to where my Execute method returns a map with the Shell's output.

Method 1:
B4X:
Dim Result As ShellSyncResult = ProcessShell.RunSynchronous(-1)
Dim M As
M.
M.Put("ExitCode", Result.ExitCode)
M.Put("Success", Result.Success)
M.Put("Output", Result.StdOut & Result.StdErr)
Return M

Method 2:
B4X:
Dim Result As ShellSyncResult = ProcessShell.RunSynchronous(-1)
Dim M As Map
M.Initialize
M.Put("ExitCode", Result.ExitCode)
M.Put("Success", Result.Success)
M.Put("Error", Result.StdErr)
M.Put("Output", Result.StdOut)
Return M

Method 1 works, but with Method 2, StdErr actually contains the output from "java -version". I don't mind using Method 1, but I would like to know what causes Method 2 to do what it does.

Edit 1: Forgot to upload the updated module.
 

Attachments

  • Process.bas
    1.3 KB · Views: 241
Last edited:
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
I'm not sure why you prefer to return a Map over returning ShellSyncResult. It will be easier to work with ShellSyncResult.
Though it would be easier, I want to use more than just the output. I also want to use the Success variable. Ex:

B4X:
Dim M As Map = P.Execute("command", True, False)
Dim Answer As Boolean = M.Get("Success")
If Answer Then
    'Do stuff
Else
    'Command wasn't successful.
End If

This means that java -version writes the relevant text to StdErr.
Oh, OK. I didn't realize that programs could choose where to output their output.
 
Upvote 0
Top