- 首先,从来没有覆盖environment variable
path
,甚至 暂不。请附加您的文件夹:set "path=%path%;%myAppPath%"
或set "path=%myAppPath%;%path%"
;
- 使用,如果你知道它完整路径程序,例如
"%myAppPath%\myApp"
。
但如何-去知道未知机器上完整路径程序?让我们用where
command,看到我的cmd
窗口粘贴示例:
==> where /F notepad.exe
"C:\Windows\System32\notepad.exe"
"C:\Windows\notepad.exe"
==> where /F pspad.exe
INFO: Could not find files for the given pattern(s).
==> where /F pspad.exe 2>NUL
==> where /R "%ProgramFiles%" /F pspad.exe 2>NUL
==> where /R "%ProgramFiles(x86)%" /F pspad.exe 2>NUL
"C:\Program Files (x86)\PSPad editor\PSPad.exe"
存储路径,以我的应用程序给一个变量(最后一次出现):
==> set "myAppPath="
==> for /F "delims=" %G in ('where /F notepad.exe 2^>NUL') do @set "myAppPath=%~dpG"
==> set myAppPath
myAppPath=C:\Windows\
(第一次出现):
==> set "myAppPath="
==> for /F "delims=" %G in ('where /F notepad.exe 2^>NUL') do @if not defined myAppPath set "myAppPath=%~dpG"
==> set myAppPath
myAppPath=C:\Windows\System32\
在批处理脚本把它放在一起:
@ECHO OFF
SETLOCAL EnableExtensions
set "myApp=%~1"
if not defined myApp set "myApp=java.exe"
set "myAppPath="
rem search in %path% environment variable
for /F "delims=" %%G in ('
where /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no1
:no1
rem search in %ProgramFiles% folder recursively
if not defined myAppPath for /F "delims=" %%G in ('
where /R "%ProgramFiles%" /F %myApp% 2^>NUL
') do if not defined myAppPath set "myAppPath=%%~dpG" & goto :no2
:no2
rem search in %ProgramFiles(x86)% folder recursively
if not defined myAppPath (
if defined ProgramFiles^(x86^) for /F "delims=" %%G in ('
where /R "%ProgramFiles(x86)%" /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no3
)
:no3
rem search in %SystemDrive%\ disk recursively
if not defined myAppPath for /F "delims=" %%G in ('
where /R %SystemDrive%\ /F %myApp% 2^>NUL
') do set "myAppPath=%%~dpG" & goto :no4
:no4
rem populate results
if defined myAppPath (echo could run "%myAppPath%%myapp%") else (
echo '%myapp%' not found in %SystemDrive%\ disk
)
输出:
==> 34039382.bat notepad
could run "C:\Windows\System32\notepad"
==> 34039382.bat iexplore.exe
could run "C:\Program Files\Internet Explorer\iexplore.exe"
==> 34039382.bat pspad.exe
could run "C:\Program Files (x86)\PSPad editor\pspad.exe"
==> 34039382.bat psexec.exe
could run "C:\Utils\Sysinternals\psexec.exe"
==> 34039382.bat
'java.exe' not found in C:\ disk
这个预言文章,Deploying the JRE on Windows,指出了Java安装程序将复制java.exe
到系统目录:java.exe
可执行的
两个副本安装。一个副本是 JRE的bin
目录。根据系统的不同,第二个副本将被放置在 C:\windows\system
或C:\winnt\system32
之中。 由于它在系统目录中的位置,因此可以从任何目录位置 java.exe
的第二个副本而不是 给出它的完整路径。
使用命令'where java' – SpringLearner
为什么不打包你的应用程序的JRE?这样你可以精确设置jre路径而不用搜索。如果你的客户端没有安装java,它也可以工作。有了这种溶剂,你总是会有适当的jre版本。 –
我的客户端安装了java.but,但他没有为那个设置路径,如果我把哪里的Java我没有得到我的Java安装路径。 – mvswetha