WaterBoy
WaterBoy

Reputation: 709

How to run a Python3 file from Java on the mac

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?

Upvotes: 1

Views: 1082

Answers (1)

Jose Zevallos
Jose Zevallos

Reputation: 725

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

Upvotes: 1

Related Questions