Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make this works since 2 days ago but I don't know why it doesn't.

I can execute normal linux commands (ls, pwd...) without problems but I try to execute a python script and nothing happends.

This is my code:

Process p;
try{
    System.out.println("SEND");
    String cmd = "/bash/bin -c echo password| python script.py '" + packet.toString() + "'";
    //System.out.println(cmd);
    p = Runtime.getRuntime().exec(cmd); 
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s = br.readLine(); 
    System.out.println(s);
    System.out.println("Sent");
    p.waitFor();
    p.destroy();
} catch (Exception e) {}

Nothing happends. It reach SEND print but it just stopped after it...

I am trying to execute a script which needs root permissions because it uses serial port. Also, I have to pass a string with some parameters (packet)

share|improve this question
 
is your python script writing somethong to it's standard out? –  VishalD May 8 at 18:15
 
What about use Apache commons exec ? –  rkosegi May 8 at 18:20

3 Answers

up vote 3 down vote accepted

You cannot use the PIPE inside the Runtime.getRuntime().exec() as you do in your example. PIPE is part of the shell.

You could do either

  • Put your command to a shell script and execute that shell script with .exec() or
  • You can do something similar to the following

    String[] cmd = {
            "/bin/bash",
            "-c",
            "echo password | python script.py '" + packet.toString() + "'"
        };
    Runtime.getRuntime().exec(cmd);
    
share|improve this answer
 
How do I do a shell script for that? I have never created a shell script –  Biribu May 8 at 18:37
 
This answer works even without creating a shell script file. Just copy-paste it to your code. –  pts May 8 at 18:43
 
@pts Yes, it works but I couldn't explain it clearly before. I updated the post, I hope it better now. –  Alper May 8 at 19:19
 
It works! thank you so much! –  Biribu May 8 at 19:34

You would do worse than to try embedding jython and executing your script. A simple example should help:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

// Using the eval() method on the engine causes a direct
// interpretataion and execution of the code string passed into it
engine.eval("import sys");
engine.eval("print sys");

If you need further help, leave a comment. This does not create an additional process.

share|improve this answer

@Alper's answer should work. Better yet, though, don't use a shell script and redirection at all. You can write the password directly to the process' stdin using the (confusingly named) Process.getOutputStream().

Process p = Runtime.exec(new String[]{"python", "script.py", packet.toString()});
BufferWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("password");
writer.newLine();
writer.close();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.