'"java.exe"' is not recognized as an internal or external command,

58,851

Solution 1

If you look at the "ant.bat" file, you will see that it looks for the "java" command in the following way:

  1. If the %JAVACMD% environment variable is set, then it uses that.
  2. Otherwise, if the %JAVA_HOME% environment variable is set, it tries to use %JAVA_HOME%\bin\java.exe
  3. Otherwise, it tries to use java.exe; i.e. it will look on your %PATH%.

In your case, you have %JAVA_HOME% set ... but set to the Java installation's "bin" directory, not to the root of the installation. So the Ant.bat script looks in the wrong place for java.exe.

Just set %JAVA_HOME% correctly, and it should work.

JAVA_HOME C:\Program Files\Java\jdk1.7.0_06

As you can see from the above, you do not need to have the Java "bin" directory on your %PATH% for Ant to work, but it is a good idea to set it anyway. That way you can run the Java commands simply from the command line.

The setting of %CLASSPATH% is not relevant to this problem. Indeed, unless the build.xml file is broken, Ant will ignore your %CLASSPATH% environment variable.

Solution 2

JAVA_HOME is the path of JDK root folder.eg: C:\Program Files\Java\jdk1.7.0_06 but path define C:\Program Files\Java\jdk1.7.0_06\bin

JAVA_HOME C:\Program Files\Java\jdk1.7.0_06
JRE_HOME C:\Program Files\Java\jre1.7.0_06
path = C:\Program Files\Java\jdk1.7.0_06\bin;C:\Program Files\Java\jre1.7.0_06\bin

Solution 3

You need to put the file java.exe in your PATH variable but the JRE in JAVA_HOME

Solution 4

Just delete the following set of files from your %windir/System32 folder. Actually deleting java.exe is enough but for consistency sake just delete all the java related binaries.

  1. java.exe
  2. javaw.exe
  3. javaws.exe

Actually oracle windows installer places a copy of these files into %windir/System32 folder (which I don't understand why) but looks like they are not needed (as they are available anyway under JDK folder where you install them).

I have tried all the various solutions posted in the SO and other forums as well but none of them worked for me. I have also set all the relevant environment variables (JAVA_PATH, CLASS_PATH etc) correctly as well. Finally this is the only solution that has worked for me.

Solution 5

Typically JAVA_HOME should be the parent directory of the "bin" folder.(jre or jdk)

In this case ant expects the java to be from the JDK.

try following in a cmd window

set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_06
set path="%JAVA_HOME%/bin;%path%;
ant

(side note: adding java.exe to path is not a requirement for ant; it is a convenience thing for the user)

Share:
58,851
John John
Author by

John John

Updated on August 12, 2020

Comments

  • John John
    John John over 3 years

    I have downloaded both Java jdk1.7.0_06 and Java jre7. and i added the following system variable JAVA_HOME C:\Program Files\Java\jdk1.7.0_06\bin to my windows 7. But when I type the following in the CMD command line on my windows 7 C:\activiti-5.10\activiti-5.10\setup>ant demo.start to run a demo application I got the following error in the command line '

    "java.exe"' is not recognized as an internal or external command, operable program or batch file

    So does anyone know how i can solve this problem ? BR

  • John John
    John John over 11 years
    thanks for the reply ,, i tried the above commands and they will start the tomcat server successfully,, but when i typed >>ant demo.start i will get the same original Java.exe error?? ant thing i can do ?
  • John John
    John John over 11 years
    thanks for the reply , actually the java.exe are in the bin folder for both the java jdk1.7.0_06 and Java jre7...
  • John John
    John John over 11 years
    thanks for the reply first of all i think you mean jre7 instead of jre1.7.0_06. now when i type ">>ant demo.start" it will give me that the Build Successful. But the java.exe cmd window will open then disappear so the server will not start... any idea what is causing this ,, java.exe cmd window should open automatically and keep running ...
  • John John
    John John over 11 years
    thanks for the reply,, i added the above environment variable,, but now when i type ">>ant demo.start" it will give me that the Build Successful. But the java.exe cmd window will open then disappear so the server will not start... any idea what is causing this ,, java.exe cmd window should open automatically and keep running ...
  • John John
    John John over 11 years
    ok thanks for the reply, i changed the JAVA_HOME to be C:\Program Files\Java\jdk1.7.0_06. and now the tomacat server will start successfully when i type "C:\activiti-5.10\activiti-5.10\setup>ant demo.start". But when i navigate to the following link localhost:8080/activiti-explorer ,, then i will get the following error "The requested resource (/activiti-explorer) is not available". any idea how can i solve this problem?
  • Stephen C
    Stephen C over 11 years
    That is not an Ant problem. That is a problem with the product you are trying to use and/or the instructions you are following. Ask as a new question ... and ask on Superuser rather than SO.
  • Stephen C
    Stephen C over 11 years
    The classpath is not relevant to the problem. If it was relevant, the error message would be different.
  • Stephen C
    Stephen C over 11 years
    @imulsion - If you have %JAVA_HOME% set at all, %PATH% won't be used by the ant.bat script.
  • a_horse_with_no_name
    a_horse_with_no_name over 11 years
    the classpath has absolutely nothing to do with finding the .exe. And besides it shouldn't be used anyway. Defining a global classpath usually creates more problems than it solves.
  • Shera A Khan
    Shera A Khan over 11 years
    I would suggest to check 2 things in your environment: 1. Remove any java path from environment variables(My computer --> ... Environment variables). 2. Try running a sample program by setting path for jdk/ jre.
  • John John
    John John over 11 years
    thanks for the reply,, i just added C:\Program Files\Java\jdk1.7.0_06\bin to my PATH and it worked fine.
  • meakgoz
    meakgoz over 10 years
    @StephenC I have the same problem and tried what you have said before, but still I got stupid response in jenkins : 'C:\Windows\system32\java.exe' is not recognized as an internal or external command What could be the problem and what is the solution for that?
  • Stephen C
    Stephen C about 10 years
    @MeM - What have you set %JAVA_HOME% to? The problem is most likely that you've set it incorrectly.
  • meakgoz
    meakgoz about 10 years
    @StephenC thank you for your concern. I have found the solution (delete the file as explained in this link: coderwall.com/p/gbek2g ). Maybe it helps also someone.
  • Stephen C
    Stephen C almost 8 years
    @MeM - Yes, that can work. But it could also break other things; e.g. if some other application depended on the Java tools that you just deleted.