我正在尝试使用批处理脚本获取我的 java jdk 路径。以下是我目前所得到的:
@echo off
for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)
if "%JAVA_HOME%".==. (
@echo Java.exe not found
@echo Please make sure that java JDK 1.7 or 1.8 is installed
)
在 if 语句中,由于路径位于 C:\Program Files\Common Files\etc..,因此我收到错误“此时不应使用 Common”。但是,如果未找到 Java,则 IF 条件无法正常工作。
如果我删除 JAVA_HOME 周围的“”,则会收到一条错误消息,提示“此时不需要文件”。在这种情况下,如果找到 JAVA.exe,则 IF 条件将正常工作。
为什么它允许 \Program Files 中的空格或“\”然后停止?
答案1
我尝试了你的建议,它显示\Common was not expected
从控制台复制:
C:\tmp>del test.bat
C:\tmp>copy con test.bat
@echo off
for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)
if "%JAVA_HOME%"=="" (
@echo Java.exe not found
@echo Please make sure that java JDK 1.7 or 1.8 is installed
) else (
@echo Java.exe exists in "%JAVA_HOME%"
)
^Z
Files copied: 1.
C:\tmp>test
Java.exe exists in "C:\Program Files (x86)\Common Files\Oracle\Java\javapath\"
模拟缺失-JAVA.EXE 被 ZZZ.EXE 替换:
C:\tmp>del test.bat
C:\tmp>copy con test.bat
@echo off
for /f %%j in ("ZZZ.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)
if "%JAVA_HOME%"=="" (
@echo Java.exe not found
@echo Please make sure that java JDK 1.7 or 1.8 is installed
) else (
@echo Java.exe exists in "%JAVA_HOME%"
)
^Z
Files copied: 1.
C:\tmp>test
Java.exe not found
Please make sure that java JDK 1.7 or 1.8 is installed
C:\tmp>
答案2
构建路径时,请将"
路径中每个单独的条目放在双引号内。
例如:
export XPATH="c:\Program Files\foo":"C:\Program Files\bar"