0

I am trying to execute the following on the command line via the java runtime environment.

Runtime rt = Runtime.getRuntime();

String runtime = "cmd /c start cmd.exe /k \"cd /d C:\\Users\\User\\Documents\\ & python book.py \" "

rt.exec(runtime);

When running the command prompt directly, i.e. python book.py (assuming that I have already changed the location to the correct directory), python runs fine without any issues.

However, when done through java, the command prompt window looks different, with C:\WINDOWS\system32\cmd.exe instead of displaying Command Prompt.

The above java runtime also gives me 'python' is not recognized as an internal or external command, operable program or batch file. (Whereas the normal command prompt the runs python perfectly fine)

How would I go about including my path and environment variables such that python, or any other path/environment variable, is recognized when I run the command prompt from java?

jshamble
  • 391
  • 2
  • 14
  • 1
    Try to put the location of python in `PATH` environment variable. Not sure what you are trying to do with 'cmd /c....' Take a look at https://stackoverflow.com/questions/14894993/running-windows-shell-commands-with-python – clinomaniac Oct 25 '18 at 22:18
  • I did, but you gave me an idea, I listed the path by typing "path" in both command prompts and they appear to be different. – jshamble Oct 25 '18 at 22:45
  • 1
    `cmd.exe /k \"cd /d C:\\Users\\User\\Documents\\ & python book.py \" "` is sufficient. You are starting CMD.exe and telling it to quit after running the `start` command, which starts programs in unusual ways, to start CMD.exe and then tell CMD to behave as if started normally. And all that is before you actually do anything useful. – CatCat Oct 25 '18 at 23:24

1 Answers1

0

This may sound naiive but apparently, the solution was to restart the computer. I guess the PATH variables in the command prompt that was run JAVA weren't synchronized like in the other instances of running the command prompt directly.

jshamble
  • 391
  • 2
  • 14
  • Please take a look on [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) Then you know why a restart of Windows helped to get modified `PATH` also set in all processes started after Windows start which where already running before Windows restart and before modification of __system__ `PATH` environment variable. – Mofi Oct 26 '18 at 09:40