如何使用多个 .jar 库打包一个小程序? [英] How do I package up an applet with multiple .jar libraries?

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

问题描述

我正在从一个大型现有项目 (Vizster) 创建一个 Java 小程序.我在 Mac OS X 上使用 NetBeans 6.7.1 和 JDK 1.5.

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.

我试图从它的单个输出 .jar 文件中运行小程序,但是当我这样做时,它在 Firefox 的屏幕底部显示小程序已加载",而 java 控制台中没有任何内容,但没有任何显示在小程序的窗口中.在此之前,我在 Firefox 中遇到过不同的错误,包括诸如appletNotLoaded:ClassDefNotFoundError"之类的错误以及安全错误,但我的 java 控制台中从来没有任何输出.这是小程序的 html 文件:

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>

其中zuve.ZuveApplet.class是我的main方法所在的地方,ZuveApplet.jar"是输出jar文件的名字.这里是ZuveApplet.java,主要的方法类:

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);
    }
 }

小程序作为独立的(未嵌入在 html 中)运行得非常好,但我需要嵌入它.Vizster"对象是 JFrame 的扩展,所以我认为我应该能够创建它的一个实例并将其添加到小程序中.会不会比这复杂得多?

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?

不幸的是,我是 Java 和小程序的新手.我已经看到很多关于源树结构是一个问题的论坛帖子,所以:

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) 我使用多个包是否有问题?它们都在我的项目的 src 目录中.

1)is it a problem that I am using multiple packages? They are all in the src directory of my project.

2) 有什么我需要放在我的 java 主目录中的吗?我知道很多人都有类路径问题,但我使用的是现代 IDE,我认为它为我解决了所有这些问题.

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) 将我的项目导入 NetBeans Java Web 应用程序项目时,我应该将小程序作为 .jar 添加到项目中,还是应该添加整个项目?

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) 最初创建这个小程序时,我只有几个源文件和一堆 .jar 库作为依赖项,但是当我检查输出 .jar 时,我看到的只是编译后的源文件.没有来自图书馆的文件的踪迹.这是应该的吗?我注意到,如果我将输出 .jar 从其包含的文件夹中移出,它就无法再独立运行.我认为 .jars 应该是自包含的,这不是真的吗?关于制作可执行 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 executable jar?

5)顺便提一下,html中applet标签中的applet大小是否必须与applet本身的大小完全匹配?

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?

很抱歉,这篇巨大的帖子和令人难以置信的模糊问题,我正在与一个没有人了解小程序或 Java 的团队一起工作(我知道,我们真是太棒了).任何形式的帮助或一般性建议都会有所帮助.

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.

谢谢!

推荐答案

可以在archive属性中指定多个jar:

You can specify multiple jars in the archive attribute:

<applet codebase ="." code="zuve.ZuveApplet.class"
        archive="ZuveApplet.jar,thing.jar,anotherThing.jar"
        height="1000" width="1000"/>

JFrame 是顶级容器,因此您将无法将其添加到您的小程序中.您可以让小程序创建对象的实例并让它打开自己的窗口.更灵活的方法是将您的对象重构为 JPanel.作为 JPanel,它可以添加到 JApplet 或 JFrame,如果您还想支持将其作为应用程序运行.

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.

另请查看 JNLP 因为它可以让您将您的代码部署为小程序或应用程序,并提供 API 用于打印和本地文件访问.简单小程序无法使用的功能.

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.

这篇关于如何使用多个 .jar 库打包一个小程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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