How to Set Path and Classpath in Java
Java PATH and Java CLASSPATH are two confusing environment variables for Java beginners. Both are operating system level Environment Variables.
What are Environment Variables ?
Many operating systems use environment variables to pass configuration information to applications. Java environment variables are key/value pairs, where both the key and the value are strings. There are many subtle differences between the way environment variables are implemented on different Operating Systems. For example, Variables in Windows are NOT case-sensitive , while UNIX does not. The way environment variables are used also varies. Environment variables are typically named in uppercase, with words joined with underscore such as: JAVA_HOME .
Environment Variables examples
Many problems in the installation and running of Java applications are caused by incorrect setting of environment variables especially in confoguring PATH, CLASSPATH and JAVA_HOME.
How to set Java path in Windows
Java PATH is the environment variable where we specify the locations of binaries. When you run a program from the command line, the operating system uses the PATH environment variable to search for the program in your local file system. In Java, for run any program we use 'java.exe' and for compile java code use javac.exe . These all executable(.exe) files are available in bin folder so we set path up to bin folder. The Operating System will look in this PATH for executable. You can set the path environment variable temporary (command line) and Permanent.
Set path from Windows command line (CMD)
Open command prompt and type the following on command prompt.
Set Permanent Path of Java in Windows
In Windows inorder to set
- Step 1: Right Click on MyComputer and click on properties
- Step 2: Click on Advanced System Setting
- Step 3: Select Advanced Tab and Click on Environment Variables
- Step 4: Then you get Environment Variable window and Click on New...
Then you get a small window "New System Variable" and there you can set "Variable Name" and "Variable Value". Set Variable Name as "path" and Variable Value as "your jdk path".
Click "OK" button. Now you set your Java Path and next is setting up ClassPath.
How to Set Classpath for Java on Windows
Java CLASSPATH is the path for Java application where the classes you compiled will be available. It is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable. If CLASSPATH is not set, it is defaulted to the current directory. If you set the CLASSPATH , it is important to include the current working directory (.). Otherwise, the current directory will not be searched.
In Windows inorder to set ClassPath :
Repeat the above steps: Steps1 to Step4 .
Then you get a small window "New System Variable" and there you can set "Variable Name" and "Variable Value". Set Variable Name as "ClassPath" and Variable Value as "your class path" (ex: C:\Program Files\Java\jdk1.8.0\lib\* ).
Using wildcards in java classpath
Wild cards were introduced from Java 6 . Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For ex. a classpath entry consisting simply of * expands to a list of all the jar files in the current directory.
where %MAINCLASS% is the class containing your main method.
How to use a wildcard in the classpath to add multiple jars
where %MAINJAR% is the jar file to launch via its internal manifest.
If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like Jar*, *.jar, hiber* etc.
The following entry does not work:
Correct entry is :