You have to set the "path" if you wanna compile from anywhere. Otherwise you have to set the working directory to where javac.exe is located (in bin folder inside the directory jdk is installed to) and compile. This is not practical.
Follow these steps to add bin folder to path variable.
Go to:
System Properties -> Advanced (tab) -> Environment Variables
In User Variables for <user>, created New variable. Name = Path, Value = path of the bin folder inside jdk folder. Eg: C:\Program Files\jdk-1.5\bin
Press OK and close it.
Now it should work.