简体   繁体   中英

Java code to import JAR file

I created a JAR file and then added it to my project's build path. Now how do I import it to my class so I can use it? I've only tried import java-class.jar; so far.

You import classes, not jar files, in your Java source code.

Lets assume you have someJar.jar which contains class definitions for 3 classes, FirstClass.class , SecondClass.class and ThirdClass.class , all of which are in package org.somepackage .

Given the above, you would add

import org.somepackage.FirstClass

at the top of a source file to import the class called FirstClass .

To do this, you need to add it to the classpath when compiling and running, like

java -cp myJar.jar abmyMainClass

or

javac -cp myJar.jar a/*

Once the jar is on the execution class path, you import them normally :

import the.package.and.name.of.TheClass;

This is because the Java virtual machine has the concept of a "class path". This class path is filled with all files (classes and resources) found in jar files and folders placed on the classpath.

For example, if you have two jar and one folder :

A.jar
  com/
    mycompany/
      A.class
      Another.class
B.jar
  com/
    mycompany/
      B.class
      neededImage.gif
bin-folder/
  org/
    apache/
      Something.class

From the JVM POV you have the sum of all these folders, as if they were in a single folder.

So you can freely import whatever class you need, specifying the fully qualified name, independently if it is inside a jar or in your project bin folder.

In fact jars are nothing more than zip files of folders, containing compiled classes (and eventually other resources).

The class path is declared to the JVM when running a program, using the "-cp" command line switch. For example, for the previous 2 jars and one folder, on windows, you would write :

java -cp A.jar;B.jar;bin-folder your.main.class.Here

After adding jar to projects build path you can import classes specifically or you can import java class packages. See the following two examples.

Suppose you want to use some functions of Scanner class in your own class. Then you have to import Scanner class in to your class.(Remember there are lot more other classes in the same package with the Scanner class).

To import the Scanner class specifically from util package you can use following syntax.

import java.util.Scanner;

This will import only the Scanner class from the util package and allow only to use functions of the Scanner class.

To import all the classes in the util package you can use following syntax

import java.util.*;

This will import all the classes in the util package and you are allowed to use any functionality from any class in your class (not only functions of the Scanner class).

The second method is called as wildcard import .There is no performance difference between a specific import and a wildcard import declaration.

You cannot import .jar file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM