Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Set Classpath in Java?

Published on Sep 03,2019 371 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

In this article, we will cover how to set Java classpath, and take a look at how to set JAVA_HOME as an environment variable in Windows 10 operating system. Following pointers will be covered in this article,

So let us get started with this article on How to Set Java Classpath?

Why do I need to set JAVA_HOME?

Many programs which are developed in Java require JAVA_HOME to set in an environment variable to execute error-free one example of such program is Tomcat. When we set up environment variables programs search for executable paths like in this case javac will be located. JAVA_HOME should be pointing to JDK but not JRE.

Moving on with this article on How to Set Java Classpath?

JAVA_HOME, CLASSPATH, PATH Overview

JAVA_HOME is the path to the installation directory of JDK which is not same as JRE installation home the installation path is ‘C:Program FilesJavajdk1.8.0_191’. In JRE_HOME we store the java runtime environment path which is in my case ‘C:Program FilesJavajre1.8.0_191’

CLASSPATH

Whenever a Java application executes it checks for Java classes in CLASSPATH in order to execute without any error. It commonly contains paths to Jar files with OS path separator in windows it is ‘;’.

If you want to specify classpath while running any program using command line you can use Java -cp “some.jar;someOther.jar” com.edureka.HelloWorld . Here these two jars will be available to the program currently executed. If these jars are set in CLASSPATH environment variable it will be available to all the programs. Also, there is a wildcard * to include all the jar files inside one folder.

PATH

If we want to execute java command in command prompt we need to set PATH whose value is always JAVA_HOMEbin. If it is not set but JAVA_HOME is set then you can execute it in the following way C:%JAVA_HOME%/bin/java -version, after this is done you simply have to execute java command.

Moving on with this article on How to Set Java Classpath?

Open Advanced System Settings

In windows 10 we need to press Windows Key + Pause Key, which will open the System settings window. Head ahead to Change settings and select Advanced tab.

Alternatively:

Press ⊞ Windows Key and click on the search box ‘Type here to search’ .

Image- How to set Classpath in Java- Edureka

1. Search advanced system settingsIn the box, which is shown in the screenshot below.
2. Click on the option which says View advanced system settings .

Image- How to set Classpath in Java- Edureka

Moving on with this article on How to Set Java Classpath?

Set JAVA_HOME Environment variable

Once you click on System Settings below box will appear head to Advanced tab, click on ‘Environment Variables’.

Image- How to set Classpath in Java- Edureka

Click the “New” button Under “System variables” and enter JAVA_HOME as “Variable name” and the path to your Java JDK directory under “Variable value”

In ‘System Variables’ click on the ‘New’ Button and type JAVA_HOME in field ‘Variable name:’ and type in the path of JDK in ‘Variable value:’.

Image- How to set Classpath in Java- Edureka

We should add the path as System Variable.

Moving on with this article on How to Set Java Classpath?

Update System PATH

1. In the Environment Variables window under System variables click on the path.
2. Click Edit.
3. In the Edit environment variable window click New.
4. Then type in the following%JAVA_HOME%bin .

Image- How to set Classpath in Java- EdurekaClick on OK.

Moving on with this article on How to Set Java Classpath?

Test your configuration

Run Command Prompt
1. Type in the following echo %JAVA_HOME%
This will echo the directory path of JAVA_HOME or it will echo nothing if Variable is not set.
echo %JAVA_HOME%

Output:

C:Program FilesJavajdk1.8.0_191

To check version type java -version:
1. Type following in command line tool
java -version

This will print out the version of Java as in my case:

java version “1.8.0_191”
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191, mixed mode)

Moving on with this article on How to Set Java Classpath?

Setup JAVA_HOME, CLASSPATH and PATH For Linux

1. In vi editor open /etc/profile # vi /etc/profile .
2. Add export JAVA_HOME=/usr/jdk1.8.0.
3. Add export CLASSPATH=$CLASSPATH:/home/LOG4J_HOME/log4j-2.2.16.jar:..
4. Add export PATH=$PATH:/usr/jdk1.8.0/bin.
5. Run # . /etc/profile to activate these settings.
6. Execute # java -version to verify whether the settings are correct or not.

Now you would have understood “How to Set Classpath in Java”. Thus we have come to an end of this article. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.