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

时间:2022-10-29 13:56:28

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 for 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。

5 个解决方案

#1


26  

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 -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,

如果您没有使用任何IDE,请查看javac -cp选项。但是,最好将程序打包在一个jar文件中,并在其中包含所有必需的jar。然后,为了执行你的jar,就像,

java -jar my_program.jar

you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.

你的jar中应该有一个META-INF / MANIFEST.MF文件。请参阅此处,了解操作方法。

#2


144  

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;

首先,如您所见,在您的代码中,您必须导入类。为此,您需要导入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
    

#3


10  

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文件位于同一目录中,则上述工作。

#4


6  

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的特定步骤。

#5


5  

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中类的标准导入。

#1


26  

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 -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,

如果您没有使用任何IDE,请查看javac -cp选项。但是,最好将程序打包在一个jar文件中,并在其中包含所有必需的jar。然后,为了执行你的jar,就像,

java -jar my_program.jar

you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.

你的jar中应该有一个META-INF / MANIFEST.MF文件。请参阅此处,了解操作方法。

#2


144  

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;

首先,如您所见,在您的代码中,您必须导入类。为此,您需要导入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
    

#3


10  

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文件位于同一目录中,则上述工作。

#4


6  

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的特定步骤。

#5


5  

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中类的标准导入。