如何打包一个包含多个.jar 库的小程序?
我正在从一个大型的现有项目 (Vizster) 创建一个 Java 小程序。我在 Mac OS X 上使用 NetBeans 6.7.1 和 JDK 1.5。
我尝试从它的单个输出 .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>
其中 zuve.ZuveApplet.class 是我的 main 方法所在的位置,“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);
}
}
该小程序作为独立程序运行得非常好(未嵌入 html 中),但我需要嵌入它。 “Vizster”对象是 JFrame 的扩展,因此我认为我应该能够创建它的实例并将其添加到小程序中。事情可能比这复杂得多吗?
不幸的是,我对 java 和小程序都很陌生。我看到很多关于源树结构问题的论坛帖子,所以:
1)我使用多个包是一个问题吗?它们都在我项目的src目录中。
2)有什么我需要放在我的java主目录中吗?我知道很多人都有类路径问题,但我使用的是现代 IDE,我认为它已经为我解决了所有问题。
3) 将我的项目导入 NetBeans Java Web 应用程序项目时,我应该将小程序作为 .jar 添加到项目中,还是应该添加整个项目?
4)最初,当我创建这个小程序时,我只有几个源文件和一堆 .jar 库作为依赖项,但是当我检查输出 .jar 时,我看到的只是编译后的源文件。没有找到库中文件的踪迹。这是应该的吗?我注意到,如果我将输出 .jar 从其包含的文件夹中移出,它就无法再独立运行。我认为 .jars 应该是独立的,这不是真的吗?关于制作可执行 jar 有什么我应该知道的吗?
5)顺便说一句,html中的applet标签中表示的applet的大小是否必须与applet本身的大小完全匹配?
对于如此庞大的帖子和令人难以置信的模糊问题,我深表歉意,我正在与一个团队合作,其中没有人对 applet 或 Java 一无所知(我知道我们真的很聪明)。任何形式的帮助或一般性建议都会很有帮助。
谢谢你!
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.
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:
<!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:
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?
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:
1)is it a problem that I am using multiple packages? They are all in the src directory of my project.
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.
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?
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 executable 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?
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.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 archive 属性中指定多个 jar:
JFrame 是顶级容器,因此您无法将其添加到您的小程序中。您可以让小程序创建对象的实例并让它打开自己的窗口。更灵活的方法是将对象重构为 JPanel。作为 JPanel,可以将其添加到 JApplet 或 JFrame(如果您还想支持将其作为应用程序运行)。
另请查看JNLP,因为它可以让您部署代码作为小程序或应用程序,还提供 API 用于打印和本地文件访问。简单小程序无法使用的功能。
You can specify multiple jars in the archive attribute:
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.
Also take a look at JNLP as that lets you deploy your code as an applet or as an application and also provides APIs for printing and local file access. Functionality not available to simple applets.