B4J Question jShell under Linux (Cannot run program, error=2, No such file or directory)

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Who uses jShell under Ubunta ?
What is correct way to start commands ?

If to prepare commands - they work in the terminal, but when try to run by jShell - always error

org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "echo PASSWORD | sudo -S reboot" (in directory "/home/vlad/tempjars"): error=2, No such file or directory)

org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/usr/bin/ls /" (in directory "/usr/bin"): error=2, No such file or directory)


What file, what directory ? Work directory is existing for sure (it's File.DirApp).
And it's about any other command in a single string.

Trying to start so:
B4X:
Sub Run_Command(cli As String)
    sh.InitializeDoNotHandleQuotes("cli", cli, Null)
    sh.WorkingDirectory = File.DirApp
    sh.RunWithOutputEvents(-1)
    Wait for cli_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
       
    #if Debug
    Log(Success & "; ExitCode = " & ExitCode)
    Log("StdOut = " &  StdOut)
    Log("StdErr = " &  StdErr)
    #end if
    If Success And ExitCode = 0 Then    'all is OK
        RunLastError = ""
    Else
        RunLastError = StdErr
    End If
End Sub


How ?
 
Last edited:

teddybear

Well-Known Member
Licensed User
if echo PASSWORD | sudo -S reboot it works in cmdline. then

Create a shell script reboot.sh
like this:
B4X:
#!/bin/sh
echo PASSWORD | sudo -S reboot
put it to a path you specify such as /home/your/bin/reboot.sh
never forget to do chmod +x reboot.sh
pass it as paramenter to run_command("/home/your/bin/reboot.sh")

end if
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
Are you testing in remote debug mode?
Because I see that the folder 'tempjars' appears. This folder is created and the program is compiled in debug mode. So there you have to put all the files needed for your program.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
Maybe you need to break it into 2 separate commands "echo PASSWORD | sudo -S reboot". Maybe jShell is unable to do this.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Maybe jShell is unable to do this
Topic was created indeed to find how to do.
But "ls /" command is also "build-in" like "ffmpeg" (that works) is installed, but does not run:

Cannot run program "/usr/bin/ls /" (in directory "/usr/bin"): error=2, No such file or directory)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Before creating this topic. Did you personally try jShell under Linux ?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Great ! Any practically working code example ? Trying in Ubuntu 20.04 (or other Ubuntu) ? Or only some other Linux ?
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
I am using CentOS and other RH based systems but this has no effect on booting.
Maybe I will post later, now I do not have access to sources.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
It works like this for me.
(Ubuntu is like Debian)
B4X:
'Non-UI application (console / server application)
Sub Process_Globals
    Private sh As Shell
    Private RunLastError As String
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Run_Command( "cat", Array As String( "/etc/os-release"))
    StartMessageLoop
End Sub

Sub Run_Command( cli As String, args As List)
    Log( "Run_Command")
    
    sh.InitializeDoNotHandleQuotes( "cli", cli, args)
    sh.WorkingDirectory = File.DirApp
    sh.Run(-1)    '<<< changed
    
    Wait for cli_ProcessCompleted( Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
      
'    #if Debug
    Log(Success & "; ExitCode = " & ExitCode)
    Log("StdOut = " &  StdOut)
    Log("StdErr = " &  StdErr)
'    #end if
    If Success And ExitCode = 0 Then    'all is OK
        RunLastError = ""
    Else
        RunLastError = StdErr
    End If
End Sub

'OUTPUT:
'
'Hello world!!!
'Run_Command
'true; ExitCode = 0
'StdOut = PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
'NAME="Debian GNU/Linux"
'VERSION_ID="11"
'VERSION="11 (bullseye)"
'VERSION_CODENAME=bullseye
'ID=debian
'HOME_URL="https://www.debian.org/"
'SUPPORT_URL="https://www.debian.org/support"
'BUG_REPORT_URL="https://bugs.debian.org/"
'
'StdErr =
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
So, command separately from the argument - it's working. But if to try complex command with arguments and several piped commands (with "|") by a single string - impossible for jShell ?
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
you can do this
create a script
getdebian:
#!/bin/bash
cat /etc/os-release | grep debian
or 
cat /etc/os-release | grep $1
don't forget to make it executable
Bash:
chmod +x getdebian

B4X:
Run_Command( "./getdebian", Null)
or
Run_Command( "./getdebian", Array As String( "debian"))
Result:
Hello world!!!
Run_Command
true; ExitCode = 0
StdOut = ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

StdErr =
 
Last edited:
Upvote 0
Top