简体   繁体   English

导入JAR文件的Java代码

[英]Java code to import JAR file

I created a JAR file and then added it to my project's build path. 我创建了一个JAR文件,然后将其添加到项目的构建路径中。 Now how do I import it to my class so I can use it? 现在如何将其导入我的班级以便可以使用? I've only tried import java-class.jar; 我只尝试了import java-class.jar; so far. 至今。

You import classes, not jar files, in your Java source code. 您可以在Java源代码中导入类,而不是jar文件。

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 . 假设您有someJar.jar ,其中包含3个类的类定义, FirstClass.classSecondClass.classThirdClass.class ,它们都在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 . 在源文件的顶部导入名为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 : 将jar放在执行类路径上后,即可正常导入它们:

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

This is because the Java virtual machine has the concept of a "class path". 这是因为Java虚拟机具有“类路径”的概念。 This class path is filled with all files (classes and resources) found in jar files and folders placed on the classpath. 该类路径充满了在放在该类路径上的jar文件和文件夹中找到的所有文件(类和资源)。

For example, if you have two jar and one folder : 例如,如果您有两个jar和一个文件夹:

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. 在JVM POV中,您拥有所有这些文件夹的总和,就好像它们在单个文件夹中一样。

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. 因此,您可以随意导入所需的任何类,并指定完全限定的名称(如果它在jar内或项目bin文件夹中),则可以独立进行。

In fact jars are nothing more than zip files of folders, containing compiled classes (and eventually other resources). 实际上,jar只是文件夹的zip文件,其中包含已编译的类(以及最终的其他资源)。

The class path is declared to the JVM when running a program, using the "-cp" command line switch. 运行程序时,使用“ -cp”命令行开关将类路径声明为JVM。 For example, for the previous 2 jars and one folder, on windows, you would write : 例如,对于Windows上的前2个jar和一个文件夹,您将编写:

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. 将jar添加到项目构建路径后,可以专门导入类,也可以导入java类包。 See the following two examples. 请参阅以下两个示例。

Suppose you want to use some functions of Scanner class in your own class. 假设您想在自己的类中使用Scanner类的某些功能。 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). 然后,您必须将Scanner类导入到您的类中(请记住与Scanner类在同一包中还有很多其他类)。

To import the Scanner class specifically from util package you can use following syntax. 要专门从util包导入Scanner类,可以使用以下语法。

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. 这将仅从util包中导入Scanner类,并且仅允许使用Scanner类的功能。

To import all the classes in the util package you can use following syntax 要导入util包中的所有类,可以使用以下语法

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). 这将导入util包中的所有类,并且允许您使用该类中任何类的任何功能(不仅限于Scanner类的功能)。

The second method is called as wildcard import .There is no performance difference between a specific import and a wildcard import declaration. 第二种方法称为wildcard import 。特定的导入和通配符导入声明之间没有性能差异。

You cannot import .jar file. 您不能导入.jar文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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