简体   繁体   中英

Basic Java Runtime program cannot find python

Hello working on a small program that just needs to run a python script I have. This python script will play a given .wav file, and draw a shape on the turtle screen. As such, I'm not looking for an output to be returned to java. Here is my java code:

public class Driver {

public static void main(String[] args){
    try {
        Process p = Runtime.getRuntime().exec("python " + 
" D:/Coding Files/Python/MusicColors.py" +"  teenagers.wav");
    }
    catch (Exception e){
        System.out.println(e);
    }
}
}

The exception I get is:

java.io.IOException: Cannot run program "python": 
CreateProcess error=2, The system cannot find the file specified

I probably am making a very stupid mistake as I have limited knowledge in the subject of processes and such. I added python to my system path, so whenever I put "python" into command line, it returns with

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information.

And makes it the python shell.

Here is the exact line I added to my environment path: C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python35-32

If anyone can figure out where I went wrong I'd really appreciate it!

The $PATH variable you've set is not inherited in Java's execution context. Try passing the Python's bin path to exec() 's execution environment.

To do this, the code below first retrieve all the environment variables and create an array of ENV_KEY=ENV_VALUE pairs.

Then, the path to your Python's bin is appended to the PATH value.

Finally, we pass the array of all environment variables to exec() (via the second parameter).

import java.util.HashMap;
import java.util.Map;

public class Driver {
    public static void main(String[] args){
        try {
            String[] commands = {"python D:/Coding Files/Python/MusicColors.py teenagers.wav"}; 

            // Get a list of all environment variables
            final Map<String, String> envMap = new HashMap<String, String>(System.getenv());

            // Append Python bin path to Path
            envMap.put("Path", envMap.get("Path") + ";C:/Users/Joe/AppData/Local/Programs/Python/Python35-32");

            // Convert to an array of ENV_KEY=ENV_VALUE format strings
            final String[] envs = new String[envMap.size()];
            int i = 0;
            for (Map.Entry<String, String> e : envMap.entrySet()) {
                envs[i] = e.getKey() + '=' + e.getValue();
                i++;
            }

            // Exec with the environment variables
            Process p = Runtime.getRuntime().exec(commands, envs); 
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM