如何从我的系统中获取/ jre / bin路径

时间:2015-12-02 09:41:03

标签: java batch-file

我有bat文件(settings.bat).i会将这个bat文件提供给我的client.my客户端没有任何技术知识。如果我的客户端点击我的bat文件,我的java程序将运行。这是我的bat文件数据。

set path = "java installation path"

java addition

java sutraction

pause

在上面的bat set path中我安装了我的java软件(/ jre / bin)。

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

1 个答案:

答案 0 :(得分:0)

  • 首先,永远不要覆盖environment variable path,甚至不要覆盖set commands set "path=%path%;%myAppPath%" 暂时。请改为添加您的文件夹:set "path=%myAppPath%;%path%"";
    • 请注意并使用正确的=引文,path %path%;myAppPath%周围没有空格,以避免所有path command中的任何前导和尾随空格。
    • 存在特定的where command,因此您可以使用"%myAppPath%\myApp"
  • 使用程序的完整路径(如果您知道),例如cmd

但是如何让知道未知计算机上的程序的完整路径?让我们使用Deploying the JRE on Windows,查看从==> 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

<强>输出

java.exe

这篇oracle文章http://sphinxsearch.com/docs/current.html#fields声明Java安装程序会将java.exe复制到系统目录中:

  

安装了bin个可执行文件的两个副本。一份是在   JRE的C:\windows\system目录。第二个副本放在任一个中   C:\winnt\system32java.exe,具体取决于系统。   由于它在系统目录中的位置,这是第二个副本   -rw-r-----+ 1 599076 599076 3371531 Dec 1 08:15 engine.log -rw-r-----+ 1 599076 599076 86949 Dec 2 08:16 engine.log 可以从的任何目录位置启动而不需要   给出完整的路径