简体   繁体   English

如何使用 .jar 文件中的类?

[英]How to use classes from .jar files?

I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem.我在 Sun 上阅读了 JAR 文件的 Java 教程,但我仍然找不到解决我的问题的方法。 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".我需要使用名为 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?编写 .jar 文件的那个人想让我导入它,但是我在哪里存储 .jar 文件以将它导入到我的代码中? I tried putting both the .jar file and my .java file in the same directory, didn't work.我尝试将 .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。

Let's say we need to use the class Classname that is contained in the jar file org.example.jar 假设我们需要使用jar文件org.example.jar包含的类Classname

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: 如果您使用的是类似Unix的操作系统:

     javac -cp '.:org.example.jar' mysource.java 
  • If you are under windows: 如果您在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: 如果您使用的是类似Unix的操作系统:

     java -cp '.:org.example.jar' mysource 
  • If you are under windows: 如果您在Windows下:

     java -cp .;org.example.jar mysource 

Not every jar file is executable. 并非每个jar文件都是可执行文件。

Now, you need to import the classes, which are there under the jar, in your java file. 现在,您需要在java文件中导入jar下的类。 For example, 例如,

import org.xml.sax.SAXException;

If you are working on an IDE, then you should refer its documentation. 如果您正在使用IDE,则应参考其文档。 Or at least specify which one you are using here in this thread. 或者至少在此线程中指定您正在使用哪个。 It would definitely enable us to help you further. 无疑,它将使我们能够进一步为您提供帮助。

And if you are not using any IDE, then please look at javac -cp option. 如果您没有使用任何IDE,请查看javac -cp选项。 However, it's much better idea to package your program in a jar file, and include all the required jar s within that. 但是,最好将程序打包到jar文件中,并在其中包含所有必需的jar Then, in order to execute your jar , like, 然后,为了执行您的jar ,例如,

java -jar my_program.jar

you should have a META-INF/MANIFEST.MF file in your jar . 您的jar应该有一个META-INF/MANIFEST.MF文件。 See here , for how-to. 有关如何操作,请参见此处

You need to add the jar file in the classpath. 您需要将jar文件添加到类路径中。 To compile your java class: 编译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. 您可以在任何地方使用jar文件。 The above work if the jar file is in the same directory as your java file. 如果jar文件与Java文件位于同一目录中,则可以完成上述工作。

You need to put the .jar file into your classpath when compiling/running your code. 编译/运行代码时,需要将.jar文件放入类路径中。 Then you just use standard imports of the classes in the .jar. 然后,您只需使用.jar中类的标准导入即可。

As workmad3 says, you need the jar file to be in your classpath. 正如workmad3所说,您需要将jar文件放在类路径中。 If you're compiling from the commandline, that will mean using the -classpath flag. 如果从命令行进行编译,则意味着使用-classpath标志。 (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.) (避免使用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,我们可以为您提供特定于该IDE的步骤。

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

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