1

I'm trying to help out a friend who is trying to run a Python script from Java. Right now, here's my code:

Java:

public class test {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        try {
            Runtime.getRuntime().exec("cmd /c start C:/Users/User/Documents/python/runFile.bat");
        } catch (IOException e) {
            e.printStackTrace();
        }     
    }
}

Batch:

@echo off
python test.py 
pause

Python:

print("Hello")
input()

If I run the batch file, it prints Hello like it should. But when you run the Java file, it brings up this command prompt error:
"'python' is not recognized as an internal or external command, operable program or batch file."
It seems like this error comes up when you need to edit the PATH variable in Windows, but that's set up already. And like I said, this is only a problem when it runs from Java.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Luke Hamel
  • 21
  • 3

1 Answers1

0

It works if I change the batch file. I changed "python" to "py" and then specified the file location like:

py %~dp0test.py 

I have no idea why this works and it didn't before but there we go

Luke Hamel
  • 21
  • 3
  • You are probably being bamboozled by [PEP 397](https://www.python.org/dev/peps/pep-0397/), an adjustment intended to help Python version management on Windows systems. – Jongware Nov 04 '18 at 20:51