JAVA Environment Variables

  1. Home
  2. Blog
  3. JAVA Environment Variables

JAVA Environment Variables

Once JAVA is installed, our second step is to set environment variables PATH, CLASSPATH and JAVA_HOME for compiling and running of Java applications.

PATH (For Windows Users) 

When we launch any java program from command prompt, operating system use PATH environment variable to locate executable programs. PATH maintains a list of directories for searching executable programs such as “.exe”, “.bat” or “.com”.

When we run command javac HelloTest.java or java HelloTest, Windows searches program in current working directory and all directories listed in PATH for Java Compiler “javac.exe” and Java Runtime “java.exe”.

In case of failure it shows error given below:

C:\Java>javac HelloTest.java
'javac' is not recognized as an internal  or external  command, operable program or batch file

For JAVA applications, PATH must include JDK’s “bin” directory (e.g., “C:\Java\jdk1.8.0_31\bin”), which contains JDK programs such as Java Compiler “javac.exe” and Java Runtime “java.exe”.

Setting permanent path of JDK, you need to follow these steps:

Go to MyComputer Properties --> Advanced tab --> Environment Variables --> new tab of System Variable --> Write path in variable name --> Write path of bin folder in variable value

JAVA path setting1 JAVA path setting2 JAVA path setting3

 

 

 

 

 

 

JAVA path setting4

 

CLASSPATH (For Windows Users) 

CLASSPATH variable maintains path of JAVA class files. As we all know JAVA files can be packaged into JAR. If class file is not found in directories or jar-files defined in CLASSPATH it gives below error.

Exception in thread "main" java.lang.NoClassDefFoundError: TestClass
  at NoClassDefFoundErrorExample.(NoClassDefFoundErrorExample.java:2)
Caused by: java.lang.ClassNotFoundException: TestClass
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 1 more

For setting Classpath

Go to MyComputer Properties --> Advanced tab --> Environment Variables --> new tab of System Variable --> Write "classpath" in variable name --> Write path of lib folder in variable value

JAVA path setting

JAVA_HOME (For Windows Users) 

JAVA_HOME specifies root directory where Java is installed (e.g. C:\Java\jdk1.8.0_31). Not strictly necessary just for using Java, but some other tools may need it, or may need it if something is “non-standard.”

For setting JAVA_HOME

Go to MyComputer Properties --> Advanced tab --> Environment Variables --> new tab of System Variable --> Write "JAVA_HOME" in variable name --> Write path of java installation folder in variable value

JAVA path setting

Once all above setting for environment variable is done, we ready to compile and run Java applications.

1 2 3 20
Let's Share
Show Buttons
Hide Buttons