1

I use a standard Java routine to execute a terminal command, however I haven't been able to get it to work to run a python file including arguments.

The terminal command (which works on the terminal) is:

python3 umlsConverter.py colon cancer

Where colon cancer is one of N possible string arguments

The Java routine I usually run (from Eclipse) to execute terminal commands is:

public static String execCmdV2(String cmd,String workingDirectoryPath) {

    Runtime rt = Runtime.getRuntime();
    //String[] commands = {"system.exe","-get t"};
    String[] env= {};
    Process proc;
    File runDir = new File(workingDirectoryPath);
    try {
        proc = rt.exec(cmd,env,runDir);
    } catch (IOException e1) {
        System.out.println("Error executing command:" + e1.getLocalizedMessage());
        return null;
    }

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

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

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    String fullOutputstring = null;
    String s = null;
    try {
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
            fullOutputstring = fullOutputstring + s;
        }
    } catch (IOException e) {
        System.out.println("Unable to output the results due to error:" + e.getLocalizedMessage());
        return null;
    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    try {
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        System.out.println("Unable to output the errors due to error:" + e.getLocalizedMessage());
        return null;
    }
    return fullOutputstring;
}

And the error I get, when I run the routine for:

cmd = "python3 umlsConverter.py Breast cancer"

and

`workingDirectoryPath="/Users/n9569065/QuickUMLS"`

is

Error executing command:Cannot run program "python3" (in directory "/Users/n9569065/QuickUMLS"): error=2, No such file or directory

I think the problem has something to do with accessing python3?

WaterBoy
  • 697
  • 3
  • 13
  • 35
  • Where do you tell him to use the python executable? – Turtle Jun 13 '17 at 08:41
  • That's the WorkingDirectoryPath variable listed above - it's just a normal user directory – WaterBoy Jun 13 '17 at 08:53
  • What I meant is: you're not telling him to use pthon, you're telling him to start a program named "python3" in your current directory. That's not python's location. – Turtle Jun 13 '17 at 08:55
  • Ok - so how do I change what I have to work for running a python3 file? – WaterBoy Jun 13 '17 at 09:02
  • Just to be sure: if you know you're using python, why don't you use [this](https://stackoverflow.com/questions/8898765/calling-python-in-java)? – Turtle Jun 13 '17 at 09:05
  • 2
    Otherwise, if you really want to start your python executable, you could execute `/bin/sh -c "python3 umlsConverter.py Breast cancer"`, which can use the path in an easier way. – Turtle Jun 13 '17 at 09:06
  • Hi Nathan - that seemed to do something. I didn't get an error. It printed, "Here is the standard output of the command:" and then blank and then seemed to hang. I got no return value. – WaterBoy Jun 13 '17 at 09:33

1 Answers1

1

use the full path of the python executable. for example: /usr/bin/python3

Jose Zevallos
  • 685
  • 4
  • 3
  • I have tried this - I set cmd = "/usr/bin/python3 umlsConverter.py Breast cancer" but the results were similar: Error executing command:Cannot run program "/usr/bin/python3" (in directory "/Users/n9569065/QuickUMLS"): error=2, No such file or directory – WaterBoy Jun 13 '17 at 09:01
  • @WaterBoy A standard macOS installation does not have a python3 command. The only python installed by default is python 2.7 – greg-449 Jun 13 '17 at 09:10
  • I think I used Brew to install - so could it be somewhere else? – WaterBoy Jun 13 '17 at 09:18
  • @WaterBoy I don't use brew but I believe it installs in /usr/local/bin or /usr/local/sbin. – greg-449 Jun 13 '17 at 09:38