Reputation: 191
If I run a program that exists in %PATH% in Windows when there is a program with the same name in the current folder it will select the one in current folder instead. Is there any way to prevent this (to disregard current folder)?
The reason for my question is that I have a Java program that I have placed in the Run-key in the registry like this:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
MyProgram = javaw -cp MyProgram.jar com.mycompany.MyProgram
The problem is that on one computer I tested on it didn't work. It complained about the wrong JRE version (1.7 instead of 1.8). I found that on this computer someone installed JRE 1.8 over 1.7, but the 1.7 files are still left in Windows\System32.
If I launch javaw -cp MyProgram.jar com.mycompany.MyProgram manually in a command prompt it works, unless I have Windows\System32 as current folder.
The problem is that when Windows starts the programs during startup (from the Run-key in registry) it has System32 as current folder and therefore it will use the wrong JRE-version.
I can fix this on this specific computer by removing the incorrect exe-files from System32, but I want it to work when someone else installs it somewhere else, and the best way I have come to think of is to forcing Windows to use %PATH%, but I don't know how.
Upvotes: 1
Views: 700
Reputation: 11
You can avoid this by starting in another directory where java is unlikely to exist like so:
cmd /c (cd\ && start /b javaw -cp MyProgram.jar com.mycompany.MyProgram)
start /b
is there to avoid the cmd-window to stick around
Upvotes: 1
Reputation: 57252
@echo off
set "to_run=javaw.exe"
for %%# in ("%to_run%") do set "ppath=%%~$PATH:#"
echo %ppath%
"%ppath%" -cp MyProgram.jar com.mycompany.MyProgram
for more info about %~$PATH:
syntax check for /?
Upvotes: 4
Reputation: 36337
If I run a program that exists in %PATH% in Windows when there is a program with the same name in the current folder it will select the one in current folder instead. Is there any way to prevent this (to disregard current folder)?
No. This is the mechanism most operating systems work: Since you explicitely start the program from a certain folder, your program runs with that folder as working directory.
The problem is that on one computer I tested on it didn't work. It complained about the wrong JRE version (1.7 instead of 1.8). I found that on this computer someone installed JRE 1.8 over 1.7, but the 1.7 files are still left in Windows\System32.
Then your solution definitely shouldn't be changing the working directory but explicitely enforcing a minimal Java version by explicitely calling the right one with the correct paths set.
Just use a cmd batch file that correctly calls Java.
Upvotes: 0