用于导入 JAR 文件的 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25730098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java code to import JAR file
提问by KTF
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.
我创建了一个 JAR 文件,然后将它添加到我的项目的构建路径中。现在我如何将它导入我的班级以便我可以使用它?我import java-class.jar;
到目前为止只尝试过。
回答by Simone Gianni
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". This class path is filled with all files (classes and resources) found in jar files and folders placed on the classpath.
这是因为 Java 虚拟机具有“类路径”的概念。这个类路径填充了在放置在类路径上的 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. For example, for the previous 2 jars and one folder, on windows, you would write :
运行程序时,使用“-cp”命令行开关向 JVM 声明类路径。例如,对于前面的 2 个 jars 和一个文件夹,在 Windows 上,您可以这样写:
java -cp A.jar;B.jar;bin-folder your.main.class.Here
回答by ControlAltDel
To do this, you need to add it to the classpath when compiling and running, like
为此,您需要在编译和运行时将其添加到类路径中,例如
java -cp myJar.jar a.b.myMainClass
java -cp myJar.jar a.b.myMainClass
or
或者
javac -cp myJar.jar a/*
javac -cp myJar.jar a/*
回答by The Head Rush
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
which 包含 3 个类的类定义FirstClass.class
,SecondClass.class
和ThirdClass.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
.
回答by Chathura Priyankara
After adding jar to projects build path you can import classes specifically or you can import java class packages. See the following two examples.
将 jar 添加到项目构建路径后,您可以专门导入类,也可以导入 java 类包。请参见以下两个示例。
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).
假设你想在自己的类中使用 Scanner 类的一些功能。然后,您必须将 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.
第二种方法称为 as wildcard import
。特定导入和通配符导入声明之间没有性能差异。
You cannot import .jar file.
您不能导入 .jar 文件。