-1

I am having bat file(settings.bat).i will give this bat file to my client.my client don't have knowledge no any technology.If my client click on my bat file my java program will be run.This is my bat file data.

set path = "java installation path"

java addition

java sutraction

pause

in the above bat set path = where my java software is installed(/jre/bin).

now my requirement is , i have to fetch java installed path(.../jre/bin)from machine who executing this bat.can any one help me on this.

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
mvswetha
  • 53
  • 8
  • 1
    use the command `where java` – SpringLearner Dec 02 '15 at 09:45
  • Why not packaging a jre with your application? This way you can precisely set the jre path without searching. And if your client doesn't have java installed, it will also work. And with this sollution you always will have the proper jre version. – Tamas G. Dec 02 '15 at 09:50
  • my client installed java.but he is not set path for that.if i put where java i am not getting my java installation path. – mvswetha Dec 02 '15 at 11:36
  • in my mechine java installed like ../jre/bin folder is there.now i want to get the path contains ../jre/bin – mvswetha Dec 02 '15 at 11:37
  • i want one command that should to return java installation path on my mechine. – mvswetha Dec 02 '15 at 11:38

1 Answers1

0
  • First, never overwrite the environment variable path, not even temporarily. Append your folder instead: set "path=%path%;%myAppPath%" or set "path=%myAppPath%;%path%";
    • Note and use proper " quotation and no spacing around = to avoid any leading and trailing spaces in all set commands.
    • There exists a particular path command so you could use path %path%;myAppPath%.
  • Use full path to a program if you know it, e.g. "%myAppPath%\myApp".

But how-to get to know full path to a program on unknown machine? Let's use where command, see examples pasted from my cmd window:

==> 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"

Store path to my application to a variable (last occurrence):

==> set "myAppPath="

==> for /F "delims=" %G in ('where /F notepad.exe 2^>NUL') do @set "myAppPath=%~dpG"

==> set myAppPath
myAppPath=C:\Windows\

(first occurrence):

==> 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\

Put it all together in a batch script:

@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
) 

Output:

==> 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

This oracle article, Deploying the JRE on Windows, states that the Java installer will copy java.exe into the system directory:

Two copies of the java.exe executable are installed. One copy is in the bin directory of the JRE. The second copy is placed in either C:\windows\system or C:\winnt\system32, depending on the system. Because of it's location in the system directory, this second copy of java.exe can be launched from any directory location without giving the full path to it.

JosefZ
  • 28,460
  • 5
  • 44
  • 83