If anyone is having trouble compiling a Java class from the Windows command line with an error about not being able to find “javac”, the following might help.

First, some background…

From the command line, when you go to run an executable, it’s not feasible to check the entire filesystem for that executable. Instead, it searches the list of directories in the PATH environment variable. This variable holds a semi-colon delimited list (colon delimited on *nix systems) of directories on the filesystem that will be checked, in order, when an executable is run from a command line.

The Java installer basically just unpacks the Java files to a directory. It doesn’t bother to add its bin (short for binary, which basically means executable) directory to the PATH. Therefore, in order to easily access the “java” and “javac” commands from your code’s source directory, you’ll need to add this directory to the PATH variable.

The following page outlines directions on how to edit environment variables in Windows (scroll down to Adding or Editing Environment Variables):
http://vlaurie.com/computers2/Articles/environment.htm

Figure out where you installed Java to (it’s probably under Program Files) and add the full path to the bin directory inside of that Java installation at the end of the PATH variable. Don’t delete anything that is currently in that variable and make sure to add a semi-colon before the Java directory in order to indicate it is a new entry.

If you had a command line running already, you’ll need to restart it to pick up the new variable.

Yes, it’s a bit of a pain in the ass and hidden to change environment variables. Use a real operating system like Linux and it’s much easier.  :)

Comments are closed.