stp Active Member Licensed User Longtime User Jul 15, 2025 #1 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
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 Jul 15, 2025 #2 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
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!
stp Active Member Licensed User Longtime User Jul 15, 2025 #3 I am able to run windows programs with that code: Code: Copy 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
I am able to run windows programs with that code: Code: Copy 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 ?
Cableguy Expert Licensed User Longtime User Jul 15, 2025 #4 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
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...
stp Active Member Licensed User Longtime User Jul 15, 2025 #5 Cableguy said: 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! Click to expand... Cableguy said: 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! Click to expand... Nice Pythia answer !!! i did but there is not much.. I will check again Upvote 0
Cableguy said: 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! Click to expand... Cableguy said: 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! Click to expand... Nice Pythia answer !!! i did but there is not much.. I will check again
Cableguy Expert Licensed User Longtime User Jul 15, 2025 #6 stp said: Nice Pythia answer !!! Click to expand... you just lost me there! Upvote 0
P PaulMeuris Well-Known Member Licensed User Jul 15, 2025 #7 You can use this line... B4X: Copy 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: Copy sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename)) Last edited: Jul 15, 2025 Upvote 0
You can use this line... B4X: Copy 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: Copy sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename))
stp Active Member Licensed User Longtime User Jul 15, 2025 #8 PaulMeuris said: You can use this line... B4X: Copy 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: Copy sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename)) Click to expand... Thank you i will try Upvote 0
PaulMeuris said: You can use this line... B4X: Copy 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: Copy sh.InitializeDoNotHandleQuotes("sh", "cmd", Array As String("/c", "java", "-jar", "--module-path",modulepath,"--add-modules",addmodules,jarfilename)) Click to expand... Thank you i will try
stp Active Member Licensed User Longtime User Jul 15, 2025 #9 stp said: Thank you i will try Click to expand... i tried that code but does not work. I want to run cmd. I succeded with calc. My problem is with cmd commandw with parameter. Upvote 0
stp said: Thank you i will try Click to expand... i tried that code but does not work. I want to run cmd. I succeded with calc. My problem is with cmd commandw with parameter.
P PaulMeuris Well-Known Member Licensed User Jul 15, 2025 #10 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
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)?
stp Active Member Licensed User Longtime User Jul 15, 2025 #11 PaulMeuris said: 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)? Click to expand... Yes of course: Code: Copy python extract-data0046ex.py myname test.html that is one of the commands Upvote 0
PaulMeuris said: 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)? Click to expand... Yes of course: Code: Copy python extract-data0046ex.py myname test.html that is one of the commands
P PaulMeuris Well-Known Member Licensed User Jul 15, 2025 #12 B4X: Copy 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
B4X: Copy 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 (\).
MrKim Well-Known Member Licensed User Longtime User Jul 22, 2025 #13 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: Copy 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: Copy 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: Copy 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
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: Copy 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: Copy 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: Copy 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