如何使用.jar文件中的类? [英] How to use classes from .jar files?

查看:103
本文介绍了如何使用.jar文件中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了Sun for JAR文件的Java教程,但我仍然无法找到解决问题的方法。我需要使用一个名为jtwitter.jar的jar文件中的类,我下载了该文件,然后尝试执行它(我昨天发现.jar文件可以通过双击来执行)并且Vista给了我一个错误说无法从[path] /jtwitter.jar加载Main-Class Manifest属性。

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".

编码.jar文件的人要我导入它,但是在哪里做我存储.jar文件以在我的代码中导入它?我尝试将.jar文件和我的.java文件放在同一目录中,但是没有用。

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.

我正在尝试工作的文件在这里: http://www.winterwell.com/software/jtwitter.php

The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php

我正在使用JCreator LE。

I'm using JCreator LE.

推荐答案

并非每个jar文件都是可执行的。

Not every jar file is executable.

现在,您需要在java文件中导入jar下的类。例如,

Now, you need to import the classes, which are there under the jar, in your java file. For example,

import org.xml.sax.SAXException;

如果您正在使用IDE,那么您应该参考其文档。或者至少在这个帖子中指定你在这里使用的那个。这肯定会让我们进一步帮助你。

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,那么请看 javac -cp 选项。但是,最好将程序打包在 jar 文件中,并在其中包含所有必需的 jar 。 。然后,为了执行你的 jar ,比如,

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,

java -jar my_program.jar

你应该有一个 META-INF / MANIFEST.MF 文件在 jar 中。请参见此处,用于操作方法。

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

这篇关于如何使用.jar文件中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆