3

I am trying to execute a command line with Delphi 10 on Android and get the output of the command.

I'm using files from here:

https://github.com/FMXExpress/android-object-pascal-wrapper/tree/master/android-23

Here is my code:

uses Classes,
     Androidapi.Helpers,
     java.lang.Process, java.lang.Runtime,
     java.io.OutputStream, java.io.InputStream,
     java.io.InputStreamReader, java.io.BufferedReader;

procedure Execute(sCmd: string; sOut : TStringList);
var Process : JProcess;
    Runtime : JRuntime;
    Input   : JInputStream;
    BuffInput : JBufferedReader;  
begin

    Process := TJRuntime.JavaClass.getRuntime.exec(StringToJString(sCmd));
    //Input := JInputStream(Process.getInputStream);
    BuffInput := JBufferedReader(jInputStreamReader(Process.getInputStream) );
while (s  = '') do
begin
    s := JStringToString(BuffInput.readLine);
    sOut.Add(s);
 end;

 //--- Compile but get Segmentation fault(11) at 
 // s := JStringToString(BuffInput.readLine);

end;
5
  • Did you read the Android documentation for the InputStream and OutputStream classes yet? Commented Jul 16, 2016 at 2:23
  • 1
    In what way are you stuck? Commented Jul 16, 2016 at 2:38
  • -Remy , yes i read it but i dont get any result so far, sorry im a noob for all android stuff. Commented Jul 16, 2016 at 2:57
  • You dont get any result so far because you've made no effort to get any result. What have you tried to get any input or output from the streams? Commented Jul 16, 2016 at 3:03
  • If you call 1 day of trying a no effort ... anyways i found a lot of sample to do it in java, but like i said im noob for convert java to delphi BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); String s = null; while ((s = stdInput.readLine()) != null) { System.out.println(s); } Commented Jul 16, 2016 at 3:13

1 Answer 1

1

Thanks to the nice comment of Ken White, i digg more on the problem.

1: i need to use Androidapi.JNI.JavaTypes and not java.io.InputStream -> Input.available is always 0 if i use from java.io.InputStream

2: After executing my command, i need to do Process.waitFor. i guest i can't have a live output like good old dos

3: if someone know a better way to do this, your welcome :P

procedure Execute(sCmd: string; sOut : TStringList);
var Process : JProcess;
    Runtime : JRuntime;

    Input   : JInputStream;

    x, bufflen: Integer;

    s: string;
    buff : TJavaArray<Byte>;

begin
  Process := TJRuntime.JavaClass.getRuntime.exec(StringToJString(sCmd));
  Process.waitFor;

  Input := Process.getInputStream;
  bufflen := Input.available;

  buff := TJavaArray<Byte>.Create(bufflen);
  Input.read(buff);

  s := '';

  for x := 0 to bufflen - 1 do
    s := s + chr(buff[x]);

  sOut.Add(s); 

end;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.