pythonjavawindowsbatch-filepath

.bat script can't find javaw when called from Python os.command


The Basic Problem

I have a batch script that opens a Java .jar file. When I double click the .bat file, it opens the .jar fine. The .bat code:

start javaw -Djava.library.path="%~dp0bin" -jar bin\SoarJavaDebugger.jar %1 %2 %3 %4 %5

Now I want to run this .bat file from Python. When I use os.system(BAT_PATH), I get a popup error window that says,

"Windows cannot find 'javaw'."

and a terminal message to the same effect.

(I want to run this script from Python so I can run the Jar file using some other setup work that the .bat file performs. I'm only showing the relevant portion of the .bat file here though. I tested removing everything else from the .bat file and it does not affect this problem.)

EDIT: Why use the .bat file at all? I did not write this .bat file; it is tied to a software package I am invoking with my Python script, and I would rather not duplicate everything it does in my script if possible nor bypass the invoked software's normal chain of processing.

The Twist

Normally I'd think this error meant that that my Java bin folder was not in my %PATH% environment variable, but I double checked that it is, both in my Python environment (using echo $env:PATH in my VSCode terminal) and also in the .bat file environment (using echo %PATH% right before start javaw, and also by using os.system("echo %PATH%") in my Python script right before calling os.system(BAT_PATH)). The Java bin folder path prints out successfully in each case.

My java path is C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.2.13-hotspot\\bin, and I double checked that javaw.exe is there.

I've tried using the /I option on the start command, without any difference. I tried replacing os.command(BAT_PATH) with subprocess.Popen(BAT_PATH, shell=False), without any difference.

EDIT: Using where javaw returns INFO: Could not find files for the given pattern(s).

And spelling out javaw.exe with the extension makes no difference in the above.

I am using a work Windows machine that comes with an enterprise security software setup that I can't tweak. (I don't have an admin login.) Could the security system prevent my computer from using PATH to find javaw, even though it has the path in PATH?

I don't want to use a CALL command that hardcodes the javaw.exe path, because I want this script to work on multiple machines where the Java path might vary.

Again, double clicking the .bat file works fine. It just fails when trying to start it from Python.

Why does it behave this way?

EDIT: Partial solution

I am able to get the file to run by replacing my .bat line with the following:

start %~dp0bin\SoarJavaDebugger.jar %1 %2 %3 %4 %5

But I would rather keep the -Djava.library.path option to ensure that everything referenced by the jar runs properly on various systems.

And I would also just like to know why I get the original error.

Thanks!


Solution

  • Many thanks to @Mofi for time spent helping to diagnose. It led me in the right path:

    The problem was that the Python script was subtly mangling the PATH variable by adding items to it with a ":" instead of a ";". (The code was originally written for a Mac environment.) This caused the first default PATH item to be mashed with the newly added ones, and in this case the first item was the Java path.

    So while a printing of PATH from either Python or .bat at first appeared to include the Java path, it was not really there due to the ":". And the script of course worked fine when not run from Python.