3

I Asked some days ago about executing python script from java. It solved my problem partially. But now, I am not able to pass any parameter and make that script to do anything.

My script has to receive a XBee frame and send it by serial port. I tested the script in shell and it works fine. So, doing: sudo python script.py frame, the frame is sent.

Now, I tried to do the same with java, and it fails. My code is:

Process p;
//System.out.println(packet.toString());
try{
    StringBuffer p1 = new StringBuffer();
    String[] cmd = {"/bin/bash", "-c", "echo pass | python script.py b'", packet.toString(), "'"};


    p = Runtime.getRuntime().exec(cmd); 
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s = br.readLine();
    p.waitFor();
    p.destroy();
} catch (Exception e) {}

I have to add b' before argument and another ' after it. packet is StringBuffer so I get the String with toString method.

I need to run it as root because it uses serial port and if not, it says to me I have not permission.

Do you know how to do it? I tried to write a file with some word when I run the script but nothing happends, which makes me think about it doesn't run properly.

Biribu
  • 3,615
  • 13
  • 43
  • 79
  • Why do you need pipe here? This is not what you tried using shell. why not just to run the python script? Command `echo pass` just prints `passs` that then is processed by script. You can just execute script, ans write word `pass` into its STDIN. – AlexR May 09 '13 at 19:14
  • Thanks for comment. pass is the password for being root. How do you execute that line? – Biribu May 09 '13 at 19:38
  • Do you think, would be easier make a socket listening in python script and leave it running all the time. This way, I can send data from java as if I was sending it over internet... – Biribu May 10 '13 at 11:03
  • possible duplicate of [Passing arguments to Python script in Java](http://stackoverflow.com/questions/17466628/passing-arguments-to-python-script-in-java) – Lennart Regebro Jul 04 '13 at 09:34
  • http://stackoverflow.com/questions/38657109/how-to-call-a-python-script-with-arguments-from-java-class – yazabara Jan 16 '17 at 12:39

0 Answers0