0

NOTE: Path of python.exe has already been set

I am trying to create a Java program that passes the variable args (or any other variable) to a Python script.

import java.io.*;

public class PythonCallTest{

    public static void main (String[] args){
        String s = null;

        Runtime r = Runtime.getRuntime();
        try{
            Process p = r.exec("cmd /c python ps.py+",args);

            BufferedReader stdInput = new BufferedReader(new
                InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                InputStreamReader(p.getErrorStream()));

            while ((s = stdInput.readLine()) != null){
                System.out.println(s);
            }

            while ((s = stdError.readLine()) != null){
                System.out.println(s);
            }

            System.exit(0);
        }
        catch(IOException ioe){
            ioe.printStackTrace();
            System.exit(-1);
        }
    }
}

The program compiles but when I run it with

java PythonCallTest sender-ip=10.10.10.10

I get the error

'python' is not recognized as an internal or external command, operable program or batch file.

How do I properly concatenate the string in r.exec("cmd /c python ps.py+",args)

EDIT

If I execute the following

Process p = r.exec("cmd /c python ps.py sender-ip=10.251.22.105");

Then the program works. The path for python.exe has already been set. I just need to know how to add args to r.exec, i.e how to concatenate cmd /c python ps.py with args

12
  • 2
    Possible duplicate of stackoverflow.com/questions/17953124/… Commented Jul 1, 2014 at 14:13
  • Also, possible duplicate of stackoverflow.com/questions/14433499/… Commented Jul 1, 2014 at 14:13
  • 1
    @Andrew_CS Instead of saying it's a dupe say hey your path variable is the problem: look here Commented Jul 1, 2014 at 14:25
  • 1
    @KevinPanko My reasoning was that it was a possible duplicate. Commented Jul 1, 2014 at 14:49
  • 1
    @Glowie It's not a duplicate of my links since your updated post with more info. - that's why originally I put "possible" duplicate. Sorry people are more interested in comment policing than helping you. Commented Jul 1, 2014 at 14:53

1 Answer 1

2

You are passing args as the second argument of Runtime.exec(...).

This overrides the default (inherited) environment of the new process to be useless, and hence the Path variable no longer contains the path to python.exe.

You need to use this version of Runtime.exec(...):

public Process exec(String[] cmdarray);

Which you would do so like this:

public static void main(String[] args) {
    ...

    List<String> process_args = new ArrayList<String>(Arrays.asList("cmd", "/c", "python", "ps.py"));
    process_args.addAll(Arrays.asList(args));

    Runtime r = Runtime.getRuntime();
    try {

        Process p = r.exec(process_args.toArray(new String[] {}));
        ...
    } catch (IOException e) {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@ It is already added to PATH. I should have mentioned in the original question that if I execute r.exec("cmd /c python ps.py sender-ip=10.10.10.10") then it works
I will try your solution and let you know
My bad, I've changed the answer to fix your problem. The reason one works, and the other not, is that you're passing a second argument to exec.

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.