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

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

问题描述

我在 Sun 上阅读了 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

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

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

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

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