B4J Question Run cmd commands with B4j

stp

Member
Licensed User
Longtime User
Try to create an interface (windows ui) witth b4j but i need to execute also cmd commands.
I am new with b4j. How can i do that. I come up with jShell but there is no working example.

Thank you
 

Cableguy

Expert
Licensed User
Longtime User
Hi stp, use the keyword "jshell" in the forum's search engine and you will be presented with many related threads! I'm sure one of them will send you in the right path!
 
Upvote 0

stp

Member
Licensed User
Longtime User
I am able to run windows programs with that code:
code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    Dim shl As Shell
    shl.Initialize("shl", "calc.exe", Null)
    'shl.WorkingDirectory = "C:\Program Files\iA Writer"
    shl.WorkingDirectory = "\"
    shl.Run(-1)
    'StartMessageLoop 'need to call this as this is a console app.
End Sub

but i want ro run cmd commands, not windows executables.
Eg i want to execute an ffmpeg cmd command with parameters or a winzip command with parameters.
Do i have to open a cmd ?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I never really worked with jshell, but I guess your best (easiest?) option would be to create a .bat file on the fly and use jshell to execute it...
 
Upvote 0

stp

Member
Licensed User
Longtime User
Hi stp, use the keyword "jshell" in the forum's search engine and you will be presented with many related threads! I'm sure one of them will send you in the right path!

Hi stp, use the keyword "jshell" in the forum's search engine and you will be presented with many related threads! I'm sure one of them will send you in the right path!
Nice Pythia answer !!!
i did but there is not much.. I will check again
 
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
You can use this line...
B4X:
    shl.Initialize("shl", "cmd", Array As String("/c", "C:\WINDOWS\system32\calc.exe"))
And here's an example of using the java command with parameters...
B4X:
sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename))
 
Last edited:
Upvote 0

stp

Member
Licensed User
Longtime User
You can use this line...
B4X:
    shl.Initialize("shl", "cmd", Array As String("/c", "C:\WINDOWS\system32\calc.exe"))
And here's an example of using the java command with parameters...
B4X:
sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename))
Thank you i will try
 
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
You can add the correct arguments for the command you want to use to the array.
The java command is an example of that.
Can you show the command line (as you would type it at the command prompt)?
 
Upvote 0

stp

Member
Licensed User
Longtime User
You can add the correct arguments for the command you want to use to the array.
The java command is an example of that.
Can you show the command line (as you would type it at the command prompt)?
Yes of course:
my command:
python extract-data0046ex.py myname test.html

that is one of the commands
 
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
B4X:
dim pypath as string = "<path_to_file>" & "extract-data0046ex.py"
dim myname as string = "stp"
dim htmlfile as string = "<path_to_file>" & "test.html"
shl.InitializeDoNotHandleQuotes("shl", "cmd", Array As String("/c", "python", pypath, myname,htmlfile)
Replace <path_to_file> with the correct path including the last backslash (\).
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
You might find this useful:
NOTE: I was NEVER able to make this work with a long string with all of the parameters, I had to use params.Add and the batch file.
B4X:
Public Sub OpenJobInSk2010(JobNum As String)
Try
   
    If JobNum.Length = 0 Then Return
    Dim L As List = File.ReadList($"${File.DirApp}\.."$, "AccessCmdLine.txt")
    Dim SKPth As String =  $"${File.DirApp}\..\${L.Get(2)}"$
    Dim AccPth  As String = L.Get(1)
    Private shl As Shell
   
    Dim params As List: params.Initialize
    'params.Add("C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE")  'for testing
    params.Add($""${AccPth}""$)
    params.Add($""${SKPth}""$)
    params.Add("/Runtime")
    params.Add(" /nostartup")  
    params.Add("/CMD" )  
    params.Add($"${JobNum} *\\* ${L.Get(0)}"$)
    shl.Initialize("shl", $""${File.DirApp}\..\AccessCmdLine.bat""$, params)
    shl.WorkingDirectory = File.DirApp
    shl.Run(-1)
    SetForegroundWindow("ShopKeeper Job Control", False)

Catch
    Log(LastException)
    Main.Msgbox.Show(LastException.Message.SubString2(0, Min(LastException.Message.Length, 500)), "OpenJobInSk2010")
End Try
End Sub
A little explanation:
This opens an MS Access program, opens a particular form and navigates to a particular job.
AccessCmdLine.txt is written by the Access program when it opens my B4J program. It tells the B4J program where to find msaccess.exe and where to find the access program (AccPth, SKPth).
AccessCmdLine.bat is just the following:
B4X:
start /min %1 %2 %3 %4 %5 %6
And here is the code for SetForegroundWindow, note that you must know the EXACT title of the window for this to work:
B4X:
Sub SetForegroundWindow(Title As String, JustFind As Boolean) As Boolean
    Return Me.As(JavaObject).RunMethod("SetForegroundWindow", Array(Title, JustFind))
End Sub
Hope this helps

#if Java
import com.sun.jna.platform.win32.*;
public static boolean SetForegroundWindow(String title, boolean justFind) {
    User32 user32 = com.sun.jna.platform.win32.User32.INSTANCE;
    for (com.sun.jna.platform.DesktopWindow s : com.sun.jna.platform.WindowUtils.getAllWindows(false)) {
        String t = s.getTitle();
        if (t.toLowerCase().equals(title.toLowerCase())) {
            if (!justFind) {
                user32.SetForegroundWindow(s.getHWND());
                user32.SetFocus(s.getHWND());
                user32.ShowWindow(s.getHWND(), 9);
            }
            return true;
        }
    }
    return false;
}
#End If
 
Upvote 0
Top