Using JAR Files in Java Class Paths

This section provides a tutorial example on how to use JAR files in class paths during Java compilation and execution time.

One advantage of aggregating individual class files into a JAR file is that other Java tools recognize JAR files as collections of class files and allow you to use them in the class path.

For example, I can specify "-classpath herong.jar" when using "javac" and "java" commands to compile and run Java program that uses the "com.herongyang.TempratureConvertorBean" class stored in the JAR file.

To test this idea, I created this class, F2C.java:

/* F2C.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
import com.herongyang.TempratureConvertorBean;
public class F2C {
   public static void main(String[] arg) {
      TempratureConvertorBean b = new TempratureConvertorBean();
      double f = 0.0;
      if (arg.length>0) f = Double.parseDouble(arg[0]);
      b.setFahrenheit(f);
      double c = b.getCelsius();
      System.out.println("Fahrenheit = "+f);
      System.out.println("Celsius = "+c);
      System.out.println(b.getInfo());
   }
}

Here is what I did to test the above program using herong.jar in the class path:

C:\herong>javac -classpath herong.jar F2C.java

C:\herong>java -classpath .;herong.jar F2C 70.0
Fahrenheit = 70.0
Celsius = 21.11111111111111
My TempraturConvertorBean - Version 1.00

This is nice. Right? I can take herong.jar to anywhere on any system. Just add it to "-classpath" for "javac" command, and "-cp" for "java" command.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

jar - The JAR File Tool

 JAR - Java Archive File Format

 jar - JAR File Tool Command and Options

 "jar --create" - Creating New JAR File

 "jar --list" - Listing Files in JAR File

 "jar --extract" - Extracting Files from JAR File

 Managing JAR Files with WinZIP

 META-INF/MANIFEST.MF - JAR Manifest File

 Adding META-INF/MANIFEST.MF to JAR Files

 "jar -C" - Changing Input Directory

Using JAR Files in Java Class Paths

 "jar --update" - Updating Class Files in JAR

 "jar --main-class" - Making JAR File Executable

 Creating Module JAR File

 "jar --module-version" - Updating Module Version in JAR

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB