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

Chuck
  • 998
  • 8
  • 17
  • 30
Glowie
  • 2,271
  • 21
  • 60
  • 104
  • 2
    Possible duplicate of http://stackoverflow.com/questions/17953124/python-is-not-recognized-as-an-internal-or-external-command-why – Andrew_CS Jul 01 '14 at 14:13
  • Also, possible duplicate of http://stackoverflow.com/questions/14433499/python-is-not-recognized-as-an-internal-or-external-command – Andrew_CS Jul 01 '14 at 14:13
  • @Andrew_CS It would probably be easier for the OP if you said what you think the problem is instead of just saying the question is a duplicate. Both of those other questions may help him solve his problem but they are not necessarily any help without you saying that you think PATH issues are the problem – ford prefect Jul 01 '14 at 14:15
  • @inquisitiveIdiot There's no need to say it again if it's already been said - that's how SO attempts to keep the site uncluttered. OP can look at those links and see if they solve the problem. If they don't and the moderators don't believe it's a duplicate - then someone will eventually answer the "new" issue and my comments will either be ignored or deleted. However, they also might help other people who are directed here to find the right answer at those links. – Andrew_CS Jul 01 '14 at 14:17
  • 1
    @Andrew_CS Instead of saying it's a dupe say hey your path variable is the problem: look here – ford prefect Jul 01 '14 at 14:25
  • @inquisitiveIdiot The links and stating duplicate were provided for moderators, not OP. OP can use them if OP wants and if they help then great. – Andrew_CS Jul 01 '14 at 14:30
  • @Andrew_CS I added more information to my original question. Path has already been set – Glowie Jul 01 '14 at 14:35
  • @Andrew_CS moderators are not mind readers, so please do explain your reasoning. It helps everybody. – Kevin Panko Jul 01 '14 at 14:45
  • 1
    @KevinPanko My reasoning was that it was a possible duplicate. – Andrew_CS Jul 01 '14 at 14:49
  • I updated the question so I don't believe it's a duplicate – Glowie Jul 01 '14 at 14:50
  • 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. – Andrew_CS Jul 01 '14 at 14:53
  • @Andrew_CS --- ah no problem, I'll try out the solution posted by Jamie ..... – Glowie Jul 01 '14 at 14:57

1 Answers1

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) {
        ...
    }
}
Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37
  • @ 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 – Glowie Jul 01 '14 at 14:32
  • I will try your solution and let you know – Glowie Jul 01 '14 at 14:52
  • 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`. – Jamie Cockburn Jul 01 '14 at 14:53