Java 如何使用 .jar 文件中的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/460364/
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
How to use classes from .jar files?
提问by Gaurav Dadhania
I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
我在 Sun 上阅读了 JAR 文件的 Java 教程,但我仍然找不到解决我的问题的方法。我需要使用名为 jtwitter.jar 的 jar 文件中的一个类,我下载了该文件并尝试执行它(我昨天发现可以通过双击它们来执行 .jar 文件),Vista 给了我一个错误说“无法从 [path]/jtwitter.jar 加载 Main-Class Manifest 属性”。
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
编写 .jar 文件的人想让我导入它,但是我在哪里存储 .jar 文件以将它导入到我的代码中?我尝试将 .jar 文件和我的 .java 文件放在同一目录中,但没有用。
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
我正在尝试使用的文件在这里:http: //www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
我正在使用 JCreator LE。
采纳答案by Adeel Ansari
Not every jar file is executable.
并非每个 jar 文件都是可执行的。
Now, you need to import the classes, which are there under the jar, in your java file. For example,
现在,您需要在 java 文件中导入 jar 下的类。例如,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
如果您正在使用 IDE,那么您应该参考其文档。或者至少指定您在此线程中使用的是哪一个。这肯定能让我们进一步帮助您。
And if you are not using any IDE, then please look at javac -cpoption. However, it's much better idea to package your program in a jar
file, and include all the required jar
s within that. Then, in order to execute your jar
, like,
如果您没有使用任何 IDE,那么请查看javac -cp选项。但是,将您的程序打包在一个jar
文件中并在其中包含所有必需的jar
s 是更好的主意。然后,为了执行你的jar
,比如,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF
file in your jar
. See here, for how-to.
你应该有一个META-INF/MANIFEST.MF
文件在你的jar
. 请参阅此处,了解操作方法。
回答by workmad3
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
编译/运行代码时,您需要将 .jar 文件放入类路径中。然后您只需使用 .jar 中类的标准导入。
回答by Jon Skeet
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
正如 workmad3 所说,您需要将 jar 文件放在您的类路径中。如果您从命令行编译,则意味着使用 -classpath 标志。(避免使用 CLASSPATH 环境变量;这是 IMO 的一项痛苦。)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.
如果您使用的是 IDE,请告诉我们是哪一个,我们可以帮助您完成特定于该 IDE 的步骤。
回答by kgiannakakis
You need to add the jar file in the classpath. To compile your java class:
您需要在类路径中添加 jar 文件。编译你的java类:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
要运行您的代码(前提是 MyClass 包含一个 main 方法):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
您可以在任何地方拥有 jar 文件。如果 jar 文件与您的 java 文件在同一目录中,则上述工作有效。
回答by GabrieleV
Let's say we need to use the class Classname
that is contained in the jar file org.example.jar
假设我们需要使用Classname
包含在 jar 文件中的类org.example.jar
And your source is in the file mysource.java
Like this:
而您的来源在文件中,mysource.java
如下所示:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
首先,如您所见,您必须在代码中导入类。要做到这一点,你需要import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
其次,编译源码时,必须引用jar文件。
Please note the difference in using :
and ;
while compiling
请注意使用:
和;
编译时的区别
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
如果您使用的是类似 Unix 的操作系统:
javac -cp '.:org.example.jar' mysource.java
如果您在 Windows 下:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
之后,您将获得字节码文件 mysource.class
Now you can run this :
现在你可以运行这个:
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
如果您使用的是类似 Unix 的操作系统:
java -cp '.:org.example.jar' mysource
如果您在 Windows 下:
java -cp .;org.example.jar mysource