Location Research Breakthrough Possible @S-Logix pro@slogix.in

How to Set Classpath Using Environment Variables for Jar Files in Java?

Set Classpath in Java

Description for Setting Classpath Using Environment Variables for Jar Files in Java

  • Description: Whenever a Java application executes, it checks for Java classes in the *CLASSPATH* to execute without errors. The *CLASSPATH* commonly contains paths to JAR files, with the OS path separator being `;` in Windows. To specify the classpath while running a program using the command line, you can use the command `java -cp "some.jar;someOther.jar" com.edureka.HelloWorld`. These two JAR files will be available to the program currently executed. If these JARs are set in the *CLASSPATH* environment variable, they will be available to all programs. Additionally, the wildcard `*` can be used to include all JAR files inside a folder.
Screenshots
  • STEP 1: Open Advanced System Settings. In Windows 10, press *Windows Key + Pause Key*, which will open the System settings window. Then, navigate to *Change settings* and select the *Advanced* tab. Alternatively, press the *Windows Key*, click the search box, and type 'advanced system settings'.
  • Open Advanced System Settings

  • STEP 2: Click the option that says *View advanced system settings*.
  • View Advanced System Settings

  • STEP 3: Set the *JAVA_HOME* Environment variable. After clicking on *System Settings*, the box below will appear. Navigate to the *Advanced* tab and click the *Environment Variables* button.
  • Set JAVA_HOME Environment Variable

  • STEP 4: Click the "New" button under *System variables*, and enter *JAVA_HOME* as the "Variable name", and the path to your Java JDK directory under "Variable value".
  • Set JAVA_HOME Path

  • STEP 5: Update the System PATH variable. Follow these steps:
    1. Click on *Path* under *System Variables*.
    2. Click *Edit*.
    3. In the *Edit environment variable* window, click *New*.
    4. Type in the following: `%JAVA_HOME%\bin`.
  • Update System PATH Variable

Related Links