Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » » How To Find Java In A System and Set In The Path?

Image Credit:http://android.downloadatoz.com
It is very common to the error java is not recognized as an internal command,operable program or batch file error while invoking any jar or trying to run a jar by providing command java -jar jarname.jar. For me it was very tough to distribute any jar file as most of my team members were unable to open the jar file from command prompt. To resolve this , I had to come up with something new. I work in a closed environment where we do not have admin rights to set the class path in environment variable.
The process we follow:
Open command prompt and set the java (JDK or JRE). Then try to open the jar file. My objective was to do something that will dynamically find java and set in path.
There is a command in cmd in windows -7, i.e where java ,it immidiately shows up C:\windows\System32\
I happened to navigate to oracle's page here.
It was said under Java.exe executable section-
Two copies of 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:\Windows\System32
or
C:\WinNT\System32
depending upon the system.
Because of this the second copy of the java.exe, java can be launched from any directory without giving path.
It was more than enough for me to set the java path and initiate a jar file
While googling more for a concrete solution I found the below Stackoverflow links-

  1. Question -4681090
  2. Question- 17602113
  3. Question-3930383
I found these solution was very nice. And I took the third one: Thanks to Glob for the solution.







@ECHO off
SET KIT=JavaSoft\Java Runtime Environment
call:ReadRegValue VER "HKLM\Software\%KIT%" "CurrentVersion"
IF "%VER%" NEQ "" GOTO FoundJRE
SET KIT=Wow6432Node\JavaSoft\Java Runtime Environment
call:ReadRegValue VER "HKLM\Software\%KIT%" "CurrentVersion"
IF "%VER%" NEQ "" GOTO FoundJRE
SET KIT=JavaSoft\Java Development Kit
call:ReadRegValue VER "HKLM\Software\%KIT%" "CurrentVersion"
IF "%VER%" NEQ "" GOTO FoundJRE
SET KIT=Wow6432Node\JavaSoft\Java Development Kit
call:ReadRegValue VER "HKLM\Software\%KIT%" "CurrentVersion"
IF "%VER%" NEQ "" GOTO FoundJRE
ECHO Failed to find Java
GOTO :EOF
:FoundJRE
call:ReadRegValue JAVAPATH "HKLM\Software\%KIT%\%VER%" "JavaHome"
ECHO %JAVAPATH%
SET PATH=%JAVAPATH%\bin
// run your jar--I modified a little bit


GOTO :EOF
:ReadRegValue
SET key=%2%
SET name=%3%
SET "%~1="
SET reg=reg
IF DEFINED ProgramFiles(x86) (
IF EXIST %WINDIR%\sysnative\reg.exe SET reg=%WINDIR%\sysnative\reg.exe
)
FOR /F "usebackq tokens=3* skip=1" %%A IN (`%reg% QUERY %key% /v %name% 2^>NUL`) DO SET "%~1=%%A %%B"
GOTO :EOF


«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply