OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Bryany
  • Start date Start date
B

Bryany

Guest

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:

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.)

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.

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.

EDIT: Partial solution​


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

Code:
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!
<h3>The Basic Problem</h3>
<p>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:</p>
<pre><code>start javaw -Djava.library.path="%~dp0bin" -jar bin\SoarJavaDebugger.jar %1 %2 %3 %4 %5
</code></pre>
<p>Now I want to run this .bat file from Python. When I use <code>os.system(BAT_PATH)</code>, I get a popup error window that says,</p>
<blockquote>
<p>"Windows cannot find 'javaw'."</p>
</blockquote>
<p>and a terminal message to the same effect.</p>
<p>(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.)</p>
<h3>The Twist</h3>
<p>Normally I'd think this error meant that that my Java bin folder was not in my <code>%PATH%</code> environment variable, but I double checked that it is, both in my Python environment (using <code>echo $env:PATH</code> in my VSCode terminal) and also in the .bat file environment (using <code>echo %PATH%</code> right before <code>start javaw</code>, and also by using <code>os.system("echo %PATH%")</code> in my Python script right before calling <code>os.system(BAT_PATH)</code>). The Java bin folder path prints out successfully in each case.</p>
<p>My java path is <code>C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.2.13-hotspot\\bin</code>, and I double checked that javaw.exe is there.</p>
<p>I've tried using the <code>/I</code> option on the <code>start</code> command, without any difference.
I tried replacing <code>os.command(BAT_PATH)</code> with <code>subprocess.Popen(BAT_PATH, shell=False)</code>, without any difference.</p>
<p>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 <code>PATH</code> to find <code>javaw</code>, even though it has the path in <code>PATH</code>?</p>
<p>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.</p>
<p>Again, double clicking the .bat file works fine. It just fails when trying to start it from Python.</p>
<h3>EDIT: Partial solution</h3>
<p>I am able to get the file to run by replacing my .bat line with the following:</p>
<pre><code>start %~dp0bin\SoarJavaDebugger.jar %1 %2 %3 %4 %5
</code></pre>
<p>But I would rather keep the <code>-Djava.library.path</code> option to ensure that everything referenced by the jar runs properly on various systems.</p>
<p>And I would also just like to know why I get the original error.</p>
<p>Thanks!</p>
 

Latest posts

U
Replies
0
Views
1
user3658366
U
G
Replies
0
Views
1
Giampaolo Levorato
G
M
Replies
0
Views
1
Marcelo Rodrigo Nascimento
M
Top