Path & ClassPath
PATH is used by the operating system to find executable files (.exe's in Windows,java,javac,javap) that it can run.
CLASSPATH, on the other hand is specifically used by Java to find .class files that are needed when you run a Java program.
How to execute your first java program.
PATH variable:
Setting the PATH variable allows you "to conveniently run the JDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command."
For example to execute a java program from your “E:\myJavaPrograms\” directory if you didn’t set your PATH variable then you should execute your programs like
E:\myJavaPrograms> c:\jdk\bin\javac MyFirstProgram.java
But if you had your PATH variable then can execute your programs like
E:\myJavaPrograms> javac MyFirstProgram.java
NOTE:
All these executables(javac.exe, java.exe, javadoc.exe, etc.) are in “jdk\bin” folder. So all you have to do is just include this folder path in your PATH variable
In Command Prompt.
set path=”%path%;c:\jdk\bin”
CLASSPATH variable:
Setting the CLASSPATH variable "tells JDK tools and applications where to find third-party (all jar files, zip files that contain .class files) and user-defined classes."
We always include “.” operator in CLASSPATH because that helps the jdk tools to pick up the class files from current working directory(folder)
In Command Prompt.
set classpath=”%classpath%;.;d:\tomcat\servlet-api.jar”
Setting PATH and CLASSPATH in Environment Variables:
Right click on "My Computer", "Advanced" tab, "Environment Variables". We can see two sections
1. User Variables and
2. System Variables
Click New in User Variables
set variable name as JAVA_HOME
set variable value as C:\bea_81\jdk142_05
set variable name as PATH
set variable value as C:\apache-ant-1.7.1\bin;%JAVA_HOME%\bin;.;
% % is used to include the contents in Java_Home
NOTE:
we include %path% and %classpath% so that any previously set path and classpath are also included along with this one.
For further assistance you can look into these links
Setting PATH Variable
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
Setting CLASSPATH Variable
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html
http://faq.javaranch.com/view?HowToSetTheClasspath
Learn about java tools
http://java.sun.com/j2se/1.5.0/docs/tooldocs/index.html
Tomcat:
Modify the startup.bat file in tomcat's bin directory as follows:
set
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_10
for Java_Home - Please point to the jdk folder and not to the bin folder.
set CATALINA_HOME=C:\TOMCAT6
set JRE_HOME=C:\Program Files\Java\jre1.6.0
Compiling and Running java packages
Source File - C:\src\vis\Test.java
C:\src\> javac vis\Test.java
Compile the program from its root directory. That is outside the package structure.
package vis;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Run the application like this.
C:\src\>java vis.Test
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d C:\myclasses and the class is called com.mypackage.MyClass, then the class file is called C:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.
Create folder “class” inside src folder.
Now we have (1) vis and (2) class folders inside “src” folder.
-d <destination> <source>
C:\src\>javac -d class vis\Test.java
C:\src\>javac -sourcepath vis -d class vis\Test.java
Executing the program
-cp <destination> <source>
C:\src\>java -cp class vis.Test


