【问题标题】:How to fetch the /jre/bin path from my system [duplicate]如何从我的系统中获取 /jre/bin 路径[重复]
【发布时间】:2015-12-02 09:41:02
【问题描述】:

我有 bat 文件(settings.bat)。我将把这个 bat 文件提供给我的客户。我的客户不了解任何技术。如果我的客户点击我的 bat 文件,我的 java 程序将运行。这是我的 bat 文件数据。

set path = "java installation path"

java addition

java sutraction

pause

在上面的bat中设置路径=我的java软件的安装位置(/jre/bin)。

现在我的要求是,我必须从执行此 bat 的机器中获取 java 安装路径(.../jre/bin)。任何人都可以帮助我解决这个问题。

【问题讨论】:

  • 使用命令where java
  • 为什么不在您的应用程序中打包一个 jre?这样您无需搜索即可精确设置 jre 路径。如果您的客户端没有安装 java,它也可以工作。有了这个解决方案,您将始终拥有正确的 jre 版本。
  • 我的客户端安装了 java。但他没有为此设置路径。如果我把 java 放在我没有得到我的 java 安装路径的地方。
  • 在我的机器中安装了类似 ../jre/bin 的 java 文件夹。现在我想获取包含 ../jre/bin 的路径
  • 我想要一个命令来返回我机器上的 java 安装路径。

标签: java batch-file


【解决方案1】:
  • 首先,永远不要覆盖environment variable path,甚至不要覆盖 暂时地。改为附加您的文件夹:set "path=%path%;%myAppPath%"set "path=%myAppPath%;%path%"
    • 注意并使用正确的" 引号,= 周围没有空格,以避免在所有set commands 中出现任何前导和尾随空格。
    • 存在一个特定的path command,因此您可以使用path %path%;myAppPath%
  • 使用程序的完整路径如果你知道它,例如"%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

这篇oracle文章Deploying the JRE on Windows指出Java安装程序会将java.exe复制到系统目录中:

安装了java.exe 可执行文件的两个副本。一份在 JRE 的bin 目录。第二个副本放置在 C:\windows\systemC:\winnt\system32,取决于系统。 由于它位于系统目录中,因此第二个副本 java.exe 可以从任何目录位置启动无需 给出完整的路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2013-11-10
    • 2011-02-19
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多