java 如何使用多个 .jar 库打包一个小程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2829388/
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 do I package up an applet with multiple .jar libraries?
提问by mag725
I'm creating a Java applet from a large scale pre-existing project (Vizster). I am using NetBeans 6.7.1 with JDK 1.5 on Mac OS X.
我正在从一个大型预先存在的项目 (Vizster) 创建一个 Java 小程序。我在 Mac OS X 上使用 NetBeans 6.7.1 和 JDK 1.5。
I am attempting to run the applet from it's single output .jar file, but when I do this, it says "applet loaded" at the bottom of screen in Firefox, and there is nothing in the java console, but nothing shows up in the window for the applet. I have had different errors in Firefox previous to this, including errors such as "appletNotLoaded:ClassDefNotFoundError" and also security errors, but there is never any output in my java console. Here is the html file for the applet:
我试图从它的单个输出 .jar 文件中运行小程序,但是当我这样做时,它在 Firefox 的屏幕底部显示“小程序已加载”,并且 java 控制台中没有任何内容,但在小程序的窗口。在此之前,我在 Firefox 中遇到过不同的错误,包括诸如“appletNotLoaded:ClassDefNotFoundError”之类的错误以及安全错误,但我的 java 控制台中从来没有任何输出。这是小程序的 html 文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<applet codebase ="." code="zuve.ZuveApplet.class"
archive="ZuveApplet.jar"
height="1000" width="1000"/>
</body>
</html>
Where zuve.ZuveApplet.class is where my main method is located, and "ZuveApplet.jar" is the name of the output jar file. Here is ZuveApplet.java, the main method class:
其中 zuve.ZuveApplet.class 是我的主要方法所在的位置,“ZuveApplet.jar”是输出 jar 文件的名称。这里是 ZuveApplet.java,主要的方法类:
package zuve;
import vizster.Vizster;
import vizster.VizsterLib;
import java.applet.Applet;
public class ZuveApplet extends Applet {
public static final String DEFAULT_START_UID = "186297";
@Override
public void init() {
new Vizster();
}
public static void main(String[] argv) {
VizsterLib.setLookAndFeel();
//String startUID = argv.length > 0 ? argv[0] : DEFAULT_START_UID;
String startUID = DEFAULT_START_UID;
String file = argv.length > 0 ? argv[0] : null;
new Vizster(startUID, file);
}
}
The applet runs perfectly well as a standalone (not embedded in html), but I need to embed it. The "Vizster" object is an extension of a JFrame, so I figure I should just be able to create an instance of it and add it to an applet. Might it be much more complicated than this?
小程序作为独立的(未嵌入在 html 中)运行得非常好,但我需要嵌入它。“Vizster”对象是 JFrame 的扩展,所以我认为我应该能够创建它的一个实例并将其添加到小程序中。可能比这更复杂吗?
I'm new to java and applets in general unfortunately. I have seen a lot of forum posts about source tree structure being an issue, so:
不幸的是,我对 Java 和小程序并不熟悉。我已经看到很多关于源树结构是一个问题的论坛帖子,所以:
1)is it a problem that I am using multiple packages? They are all in the src directory of my project.
1)我使用多个包是否有问题?它们都在我的项目的 src 目录中。
2)is there anything I need to be placing in my java home directory? I know a lot of people have classpath issues, but I am using a modern IDE which I thought took care of all that for me.
2)有什么我需要放在我的java主目录中的吗?我知道很多人都有类路径问题,但我使用的是现代 IDE,我认为它为我解决了所有这些问题。
3)when importing my project into a NetBeans Java Web Application project, should I be adding the applet to the project as a .jar, or should I add the whole project?
3) 将我的项目导入 NetBeans Java Web 应用程序项目时,我应该将小程序作为 .jar 添加到项目中,还是应该添加整个项目?
4)Originally when I had created this applet, I just had my few source files and a bunch of .jar libraries as dependencies, but when I inspected the output .jar, all I saw were the compiled source files. There was no trace of the files from the libraries. Is this how it should be? I noticed that if I moved the output .jar from its containing folder, it could no longer run standalone. I thought that .jars were supposed to be self contained, is this not true? Is there anything I should know about making an executablejar?
4)最初创建这个小程序时,我只有几个源文件和一堆.jar库作为依赖项,但是当我检查输出.jar时,我看到的只是编译后的源文件。没有来自图书馆的文件的踪迹。这是应该的吗?我注意到,如果我将输出 .jar 从其包含的文件夹中移出,它就无法再独立运行。我认为 .jars 应该是自包含的,这不是真的吗?关于制作可执行jar有什么我应该知道的吗?
5)on a side note, does the size of the applet denoted in the applet tags in the html have to exactly match the size of the applet itself?
5) 在旁注中,html 中的小程序标签中表示的小程序的大小是否必须与小程序本身的大小完全匹配?
Sorry for the enormous post, and the incredibly vague problem, I am working with a team in which nobody knows anything about applets or Java (real brilliant of us, I know). Any sort of help or general suggestions would really help.
很抱歉这篇巨大的帖子,以及令人难以置信的模糊问题,我正在与一个没有人对小程序或 Java 一无所知的团队一起工作(我知道我们真是太棒了)。任何类型的帮助或一般建议都会真正有帮助。
Thank you!
谢谢!
回答by Devon_C_Miller
You can specify multiple jars in the archive attribute:
您可以在存档属性中指定多个 jar:
<applet codebase ="." code="zuve.ZuveApplet.class"
archive="ZuveApplet.jar,thing.jar,anotherThing.jar"
height="1000" width="1000"/>
A JFrame is a top-level container, so you won't be able to add it to your applet. You can have your applet create an instance of your object and let it open its own window. A more flexible approach would be to refactor your object into a JPanel. As a JPanel, it can be added to a JApplet, or to a JFrame if you also want to support running it as an application.
JFrame 是顶级容器,因此您将无法将其添加到您的小程序中。您可以让小程序创建对象的实例并让它打开自己的窗口。更灵活的方法是将您的对象重构为 JPanel。作为 JPanel,它可以添加到 JApplet 或 JFrame,如果您还想支持将其作为应用程序运行。
Also take a look at JNLPas that lets you deploy your code as an applet or as an application and also provides APIsfor printing and local file access. Functionality not available to simple applets.
还可以查看JNLP,因为它允许您将代码部署为小程序或应用程序,并且还提供用于打印和本地文件访问的API。简单小程序无法使用的功能。

