Just recently, when i need to install ant for my final year project, i kind of reminiscent back to that day when i need to set up the path variables and the classpath. It took me about one whole day to figure out how to compile and interpret .java file to run. Frustated i am, yes, because at that time i didn’t have internet connection (hence, googling is not as meaningful). When i did find how it is done by myself, i felt really ecstatic i treat myself a lavish dinner (I didn’t get to eat lavish dinner very often).
When i first started coding using Java, i didn’t use fancy IDEs like Eclipse or NetBeans, instead, i use Text Editor. This way, i get to know the basic of compiling, interpreting, setting classpath, etc. Compiling the sources itself took me about an hour because i didn’t want to go to java directory first then call the javac.exe. So, i didnt want to call cds command every so often anytime i use the command prompt to compile the .java file. I just want to be able to call javac in any working directory. Instead of always go to this directory:
C:Program FilesJavajdk1.5.0_07bin>javac HelloWorld.java
i want to use the javac in any working directory, i.e:
C:My Java Programs>javac HelloWorld.java
How to do this? First of all, you must set the environment variables. In Vista, this can be done on
Control Panel–>System–>Advanced System Settings
In Advanced tab, click Environment Variables button.
In System Variables group, find PATH variable and edit it by adding your Java directory where the javac.exe resides. So, in my case it would be:
some path;C:Program FilesJavajdk1.5.0_07bin;
Click OK, open command prompt and then try typing any name of executable file in the bin directory. It works doesn’t it?
Now, you probably curious about what the environment variables are:
Microsoft defines them to be:
Environment variables are strings that contain information such as drive, path, or file name. They control the behavior of various programs. For example, the TEMP environment variable specifies the location in which programs place temporary files.
So, the PATH variable that we’ve edited specify the location of executable files. Such as dir, ping, cd, etc. By setting the PATH variable, we don’t need to change the working directory to the one that has ping.exe , for example. That is why by adding our java directory to the PATH variable, we can call the command in any working directory.
Now, about the classpath.
Sun has a very comprehensive tutorial on setting the classpath, so just check it out here.
If you want to keep things simpe, you can set the environment variable so you won’t keep typing classpath directory for each cmd session.
So, add the environment variable : java
give the value to the directory where all your java source code, ex:C:java.
Next, when you try to compile and run your java, you can type:
C:anydirectory>java -classpath %java% HelloWorld
There you go!
2 thoughts on “Setting Classpath and Environment Variables”
i hope u can help me on my problem.i’ve tried to change the path for variable. i followed from the instruction in order to enable the java compiler tools. however, i accidentally deleted the original path, can u help me by giving me the exact path for everything?i really need it urgent i have to submit my assignment very soon.thanks.
well i don’t exactly know because the environment variables is depend on ur computer. however, this is mine. The path before java home is the default path for Windows.