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 paths.

To test this, I created the following class,

#- Copyright (c) 2015,, All Rights Reserved.
package herong;
public class TempratureConvertorBean {
  private double celsius = 0.0;
  private double fahrenheit = 32.0;
  public double getCelsius() {
    return celsius;
  public void setCelsius(double c) {
    celsius = c;
    fahrenheit = 1.8*c + 32.0;
  public double getFahrenheit() {
    return fahrenheit;
  public void setFahrenheit(double f) {
    fahrenheit = f;
    celsius = (f-32.0)/1.8;
  public String getInfo() {
    return new String("My TempraturConvertorBean - Version 1.00");

I did the following to create a JAR file, herong.jar:

C:\herong>mkdir cls

C:\herong>javac -d cls

C:\herong>jar cvf herong.jar -C cls herong
added manifest
adding: herong/(in = 0) (out= 0)(stored 0%)
adding: herong/TempratureConvertorBean.class(in = 798) (out= 458)...

C:\herong>jar tf herong.jar

I also created a testing class,

#- Copyright (c) 2015,, All Rights Reserved.
import herong.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]);
      double c = b.getCelsius();
      System.out.println("Fahrenheit = "+f);
      System.out.println("Celsius = "+c);

Here is what I did to test using JAR files in a class path:

C:\herong>javac -classpath herong.jar

C:\herong>java -cp .;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: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

'jar' - The JAR File Tool

 JAR - Java Archive File Format

 'jar' - JAR File Tool Command and Options

 Creating the First JAR File - hello.jar

 Managing JAR Files with WinZIP



Using JAR Files in Java Class Paths

 Creating Executable JAR Files

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials


 PDF Printing Version