Little bird
Little bird

Reputation: 1088

Unable to set JAVA_HOME path into environment variable

I'm able to get PATH and JAVA_HOME on command prompt . But when i see the environment variable then i cant see these variables set in Control Panel\System and Security\System\Advanced System Properties\variables............ I think code does not set them permanently. If i double click the batch file for the second time its showing message that "java.exe not found".

I'm unable to figure out where i'm missing out or written code wrongly? i'm working on Windows 7

Code Tried :-

  @echo off
  SET JAVA_HOME=
  for /f %%j in ("java.exe") do set JAVA_HOME=%%~dp$PATH:j
  IF DEFINED JAVA_HOME GOTO :JAVA_FOUND

 :JAVA_NOT_FOUND
  @echo java.exe not found 
  PAUSE
  CD C:\Users\Pathfinder\Desktop
  ECHO "Time In"
  jdk-6u43-windows-i586.exe /s "/v\"/qn ADDLOCAL=ALL  REBOOT=Suppress JAVAUPDATE=0 
  CUSTOM=1\""
  timeout /t 10 /nobreak
  ECHO "Time Out"
  set path="C:\Program Files (x86)\Java\jre6\bin";%path%
  @echo PATH= %path%
  set JAVA_HOME="C:\Program Files (x86)\Java\jdk1.6.0_43\bin";%JAVA_HOME%
  @echo JAVA_HOME = %JAVA_HOME%
  GOTO :END

  :JAVA_FOUND
   @echo JAVA_HOME =  %JAVA_HOME%
   ECHO "ok1"
   PAUSE
   @echo JAVA_HOME = %JAVA_HOME%
   PAUSE

  :END

Upvotes: 0

Views: 5022

Answers (2)

Little bird
Little bird

Reputation: 1088

Here is the answer . As i'm working on windows 7 so setx works here. It will SET your environment variable .

@echo off
for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
 )
if %JAVA_HOME%.==. (
@echo java.exe not found
PAUSE
CD C:\Users\Pathfinder\Desktop
jdk-6u43-windows-i586.exe /s "/v\"/qn ADDLOCAL=ALL  REBOOT=Suppress JAVAUPDATE=0   
CUSTOM=1\""
setx PATH "%path%;C:\Progra~2\Java\jdk1.6.0_43\bin"
setx JAVA_HOME "C:\Progra~2\Java\jdk1.6.0_43"
) else (
@echo JAVA_HOME = %JAVA_HOME%
PAUSE
)

Upvotes: 1

foxidrive
foxidrive

Reputation: 41287

Use SETX to permanently set an environment variable in Windows.

Upvotes: 3

Related Questions