Monday, January 28, 2008

XXX is not recognized as an internal or external command, operable program or batch file

Are you seeing this error message?

XXX is not recognized as an internal or external command, operable program or batch file.

In short, it means you tried to run an executable that your OS (Operating System) couldn't find.

Updates.
25/07/2008 10:20:52 AM. Some extra detail added.
15/12/2008 12:27:42 AM. Fixed up formatting.
2/02/2009 12:52:00 PM. Corrected line continuation characters in Windows batch file excerpt.
16/05/2009 12:35:12 PM. Minor notes added. Note that Windows instructions were for "XP" or earlier.

Index

  1. Executable Not Found
  2. Executable?
  3. Executable not found? But I installed it!
  4. Get on the right PATH
  5. Edit Windows PATH (XP and earlier) - for all users
  6. Edit Windows PATH (XP and earlier) - for yourself only
  7. Edit Linux Bash PATH - for all users
  8. Edit Linux Bash PATH - for yourself only
  9. Script based PATH - for a single run only

Executable Not Found

Are you seeing this error message?

XXX is not recognized as an internal or external command, operable program or batch file.

Substitute XXX for any command you are trying to run. For Java programmers, this will probably mean one of the following:

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

In short, it means you tried to run an executable that your OS (Operating System) couldn't find. Windows outputs messages like the above. In Linux using Bash, the error messages would look like this:

Bash: java: command not found
Bash: javac: command not found
Bash: jar: command not found

Index

Executable?

An 'executable' is any file that can do something when it is run or 'executed' by the OS.

For example, consider the relationship between MS Word and MS Word files. C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE is an executable. When a Windows OS runs this file, MS Word opens and you can create, edit and save Word files. C:\Temp\myPhoneNumbers.doc is not an executable. It is a Word file. By itself, this file cannot actually do anything - you have to open it with MS Word, the executable. So why can I open it just by double clicking on the Word document then? Because OSs are smart enough to recognise most documents by type. It knows the document is a Word document and opens Word, which then opens the documnt and allows you to work with it.

The same relationship holds true for Java class files and the Java executable, javac.exe. HelloWorld.class is not an executable. It cannot be run by the OS. The Java executable is javac.exe, C:\Program Files\Java\jdk1.6.0\bin\java.exe for example. When javac.exe is run by the Windows OS, it can take a Java class file and 'run' whatever instructions are inside it.

In Linux, an executable is usually a file that doesn't have an extension. So, java.exe will be just java. But the class and source files will still have the .class and .java extensions respectively.

Index

Executable not found? But I installed it!

If you get a message like these below, it means you tried to run an executable file that your OS couldn't find.

[Windows]
'java' is not recognized as an internal or external command, operable program or batch file.

[Linux - Bash]
Bash: java: command not found

There are two prime reasons why this happened. A) You didn't actually install it.. yeah right.. or B) even though you installed it, your OS still cannot find the file. Usually, the installation process does whatever is needed to ensure that your OS can find the file, but sometimes this doesn't work out and you have to fix it yourself.

Index

Get on the right PATH

Windows and Linux use a special system variable called "PATH" that tells them where all of their executable files are. The PATH variable actually contains a list of directories that the OS will search in order to find the executable you want to run. When you issue a command to the Windows OS (such as java or winword), it scans through the files in all of the directories in PATH until it finds a file whose name matches your command with a .exe extension added. Linux just looks for the file name: remember, it doesn't use the .exe extension for executables.

Index

Edit Windows PATH (XP and earlier) - for all users

The easiest way to fix this problem is to edit the System PATH variable, which means the change you make will affect all users on your Windows computer.

Go to your Windows desktop, right click on "My Computer" and select "Properties".

Click on the "Advanced" tab and press the "Environment Variables" button.

In the bottom half ("System variables"), scroll down until you see PATH. Click the "Edit" button and you can access the variable.

When I need to change it, I copy the value to a text editor, change it there, then copy and paste the new version back into the "Edit System Variable" dialog and press ok.

Here is the value of my PATH variable.

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\IDM Computer Solutions\UltraEdit-32;C:\Program Files\QuickTime\QTSystem\

Unfortunately, nowhere in that list is a Java directory! Luckily, I know where I installed Java to: C:\Program Files\Java\jdk1.6.0. More importantly, if I look inside the bin directory of my Java installation - inside C:\Program Files\Java\jdk1.6.0\bin - I will find java.exe, javac.exe and jar.exe.

I need to edit my PATH to add the Java bin directory, preferably at the front, like so.

C:\Program Files\Java\jdk1.6.0\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\IDM Computer Solutions\UltraEdit-32;C:\Program Files\QuickTime\QTSystem\

Note 1: make sure all paths are separated with semi colons, i.e. ";".

Note 2: you can also use the DOS 8 character short form for directory names, such as what you see when issuing "DIR /X", e.g. "DOCUME~1" for "Documents and Settings".

Note 3: the list of directories is scanned in the order in which they appear in the PATH. So if you have two Java bin directories in a PATH (and both directories contain the executable), the first one will be used.

Warning: make sure you do not change any of the other entries in this list or you may end up breaking some other program.

Press OK and then OK again at the "Environment Variables" dialog. Of course, make sure to use the correct directory where Java was installed or this will not work! I then re-boot my computer - although any new DOS command window I open from this point will have the new version of the PATH.

Index

Edit Windows PATH (XP and earlier) - for yourself only

Sometimes you cannot or should not edit the System properties. Sometimes you don't want other users being able to execute their own Java code on your machine. If it is a shared or public machine, you may not even have the rights to edit these variables. In these cases, you can set the variable so that it only affects you.

Follow the same steps as above, except at the "Environment Variables" dialog, press "New" in the top half of "User variables for your username" This way, you can create your own variable named PATH and consisting solely of the Java bin directory. If by chance a local version already exists, edit this instead.

The local PATH is always added to the front of the global PATH, so your local entries will always be used (scanned) first.

Index

Edit Linux Bash PATH - for all users

For the Bash shell in Linux, you can edit the PATH by adding/editing the following 2 lines in your /etc/profile file:

PATH=$PATH:/usr/java/bin
export PATH

Note 1: make sure all paths are separated with colons, i.e. ":".

Note 2: the list of directories is scanned in the order in which they appear in the PATH. So if you have two Java bin directories in a PATH (and both directories contain the executable), the first one will be used.

Warning: make sure you do not change any of the other entries in this list (if the PATH line already exists) or you may end up breaking some other program.

Of course, make sure to use the correct directory where Java was installed or this will not work! Again, all new shells you open after you save the file will use the new PATH, but you should reboot your machine to make sure all programs already running start up fresh with the new PATH too.

Index

Edit Linux Bash PATH - for yourself only

Follow the same instructions as above, but do it in the ~/.Bash_profile instead - assuming you use the Bash shell. Other shells have their own "local" profile files, so you will need to find out what it is called and edit that instead.

The local PATH is always added to the front of the global PATH, so your local entries will always be used (scanned) first.

Index

Script based PATH - for a single run only

Sometimes there are good reasons why editing the PATH for yourself or all users isn't appropriate.

On a Windows machine, access rights might be locked down tight enough that you cannot even get to "My Computer" > right click > Properties. Alternatively, you may be writing a script (for Linux or Windows) that will be run on some one else's machine where you won't know for sure where Java is installed.

There is another way. You can write a script that will run your Java program for you - a .bat script for Windows, a .sh shell script for Linux. You then set your own PATH in the script before the lines that run the Java code. This version of the PATH will only affect your script and will not change PATH for any other users.

For example, I could write a batch file to create a jar file - using my own PATH - like this.

SET PATH=%PATH%;C:\Program Files\Java\jdk1.6.0\bin

jar cmf manifest.txt ^
HelloWorld.jar ^
org/bram/helloworld/*.java ^
org/bram/helloworld/*.class

The biggest advantage here is in terms of portability. You can make this script part of the files you copy over to other machines and just need to change PATH to make the script work. The biggest disadvantage is that you need extra effort to manage and write the script. Still, how much easier is it to manage a script than to manage someone else's System variables?

Thanks to Cody and Erik for asking me the question that inspired this post.

14 comments:

PanchoVA said...

Thank you EVERYBODY for this post. It was just what I needed - in the description AND the solution. Please don't ever dump this site. It will be helpful to many. Keep up the good work. (After Windows XP SP3, my command prompt was misbehaving just as you described.)

RobertMarkBram said...

You are more than welcome, and it makes me so happy to hear this post helped you. :)

Thank you for leaving a comment!

Rob
:)

vbor said...

thnx helped me alot

dave said...

This was a fantastic tutorial Rob. I am using Java as a student and reading the Java docs and the Java forums I still couldn't figure this out. Well done!

RobertMarkBram said...

Glad it could help you both, Dave and Vbor. :)

There are plenty of tricky little issues like this!

Rob
:)

kristy said...

I changed the path variable a few days ago and it was working fine but it happened again and when i go to either compile or run my file - 'javac' is not recognized as an internal or external command, operable program or batch file. - still comes up!

RobertMarkBram said...

Hi Kristy,

I can think of a few things that you might try checking..

* Check your path again - has it changed?
* Perhaps you didn't reboot after making the change and it hasn't taken effect yet? Maybe you're using a command window you opened before you made the change.
* If your JDK is in the path, is the path correct? Try running "java" or "javac" using the complete path, e.g. when I run this: "C:\Program Files\Java\jdk1.6.0_10\bin\java" I see Java usage message, which tells me that it is indeed the java executable.
* Is there any other part of the path that is incorrect and may be preventing Windows from parsing your JDK path?
* Have you upgraded Java or installed a new JDK? Maybe they have changed the path.
* Are you using a script that is setting PATH or JAVA_HOME? If so, are they correct?
* Is your JDK old (1.3 or before) and installed in a path that has spaces in its name?
* Has someone un-installed Java from the expected path or removed the .exe files from the bin dir of your java install?
* Are you absolutely sure you are running the command correctly? I.e. "java" or "javac" all in lower case, not "Java" or "JAVA" etc.

Just a few ideas you can try.

Good luck.

Rob
:)

Mark said...

Thank you for your post, but it really is helpful.. but I have one question. What is if "C:\Program" is not recognized as an internal or external file command what then do you do

RobertMarkBram said...

Hi Mark,

Looks like you are running a command that exists in the Program Files directory, but which doesn't surround the absolute path with quotes. For example,

WITH QUOTES = GOOD

C:\>"c:\Program Files\Java\jdk1.6.0_10\bin\java.exe" -version
java version "1.6.0_10"
Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)


WITHOUT QUOTES = BAD

C:\>c:\Program Files\Java\jdk1.6.0_10\bin\java.exe -version
'c:\Program' is not recognized as an internal or external command,
operable program or batch file.


Rule of thumb: surround any path that includes spaces with quotes.

Good luck,

Rob
:)

Anonymous said...

Thank you sooo much for these step by step explanations and instructions. You really helped save me from a mental break down. :p

Miffy said...

Hi Rob, I'm new to computing stuff and I was wondering if you could help me with a problem that I cant solve. When I try to run a program there is an error message: "could not create the java virtual machine". In the command line if I type java-version it come up with a: "Exception in thread "main" jaca.lang.NoClassDefFoundError: version" Could you tell me how to fix this? Many Thanks :)

RobertMarkBram said...

Hi Miffy,

Can you please give a bit more information? I would like to know the exact commands you are running - and the exact output you are getting from running them. Can you also output your PATH? On Windows, do it from a command line by running:
echo %PATH%

Either copy & paste this info from the command line or perhaps provide a screen shot.. To enable selection of text in a Windows command prompt, click on the top left corner of the command window, select properties > select Options tab, check "QuickEdit Mode" > click OK. You can then click and drag your mouse on the command prompt to select text and right click to actually copy it.

John said...

Thank's alot ... was able to do this for Windows 7 to. Basically just do what this says ... change the system environmental variable PATH to the path that the bin folder lies in. I was trying to run an exe. for a college project that was just saved on a flashdrive and moved to the desktop to test ... just had to type a cd desktop first ... this saved me weeks of a headache ... much appreciated!

RobertMarkBram said...

Thank you for your comment John - glad I helped you. :)