java 如何在 HTML 中嵌入 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7531740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:21:52  来源:igfitidea点击:

How to embed jar in HTML

javahtmlapplet

提问by Bill Software Engineer

There are a lot of resources on this already but I just can't seem to get it to work. What am I doing wrong? The jar file is at:

已经有很多关于此的资源,但我似乎无法让它发挥作用。我究竟做错了什么?jar 文件位于:

http://www.alexandertechniqueatlantic.ca/multimedia/AT-web-presentation-imp.jar

http://www.alexandertechniqueatlantic.ca/multimedia/AT-web-presentation-imp.jar

And the code I am using to embed is:

我用来嵌入的代码是:

<APPLET ARCHIVE="multimedia/AT-web-presentation-imp.jar" 
        CODE="ImpViewer.class" 
        WIDTH=100% 
        HEIGHT=100%>
</APPLET>

The test page I am using is at:

我正在使用的测试页面位于:

http://www.alexandertechniqueatlantic.ca/test.php

http://www.alexandertechniqueatlantic.ca/test.php

When I download the jar it runs fine, so I am certain the problem is only with the html embedding. Pleas help!

当我下载 jar 时它运行良好,所以我确定问题仅在于 html 嵌入。请帮忙!

Also, I get the following error:

另外,我收到以下错误:

java.lang.ClassCastException: ImpViewer cannot be cast to java.applet.Applet

java.lang.ClassCastException:ImpViewer 无法转换为 java.applet.Applet

回答by Andrew Thompson

java.lang.ClassCastException: ImpViewer cannot be cast to java.applet.Applet

The 'applet' is notan applet.

“小程序”不是小程序。

BTW - nice UI. Like the way the red splash fades in to the 'Welcome Introductory Workshop' page. Very smooth.

顺便说一句 - 漂亮的用户界面。就像红色飞溅淡入“欢迎介绍研讨会”页面的方式一样。很流畅。

Launch it from a link using Java Web Start(& please don't try and cram such a beautiful UI into a web page).

使用Java Web Start从链接启动它(请不要尝试将如此漂亮的 UI 塞进网页中)。



If the client insists on the GUI being crammed into a web site then (slap them for me &) try this hack.

如果客户坚持要将 GUI 塞进网站,那么(为我打耳光&)试试这个 hack。

/*
<APPLET 
    ARCHIVE="AT-web-presentation-imp.jar" 
    CODE="ImpViewerApplet" 
    WIDTH=720 
    HEIGHT=564>
</APPLET>
*/
import java.awt.*;
import java.applet.*;
import java.util.*;

public class ImpViewerApplet extends Applet {

    public void init() {
        setLayout(new BorderLayout());
        Window[] all = Window.getWindows();
        ArrayList<Window> allList = new ArrayList<Window>();
        for (Window window : all) {
            allList.add(window);
        }
        String[] args = {};
        ImpViewer iv = new ImpViewer(); 
        iv.main(args);

        all = Window.getWindows();
        for (Window window : all) {
            if (!allList.contains(window) && window.isVisible()) {
                if (window instanceof Frame) {
                    Frame f = (Frame)window;
                    Component[] allComp = f.getComponents();
                    Component c = f.getComponents()[0];
                    f.remove(c);
                    f.setVisible(false);
                    add(c);
                    validate();
                }
            }
        }
    }
}

The emphasis is on the word 'hack'.

重点是“hack”这个词。

  1. The Framewill flash onto screen before disappearing.
  2. It will only work at 720x564 px, unlike the java.awt.Framewhich was resizable to any size. But then, your '100%' width/height was being a bit optimistic anyway. Some browsers will honour those constraints, others will not.
  1. Frame会闪烁屏幕上消失之前。
  2. 它只能在 720x564 像素下工作,与java.awt.Frame可调整为任何大小不同的 。但是,无论如何,你的“100%”宽度/高度有点乐观。有些浏览器会遵守这些限制,有些则不会。

回答by Sam

It took a bit of effort, but your ImpViewer class has the following definition:

这需要一些努力,但您的 ImpViewer 类具有以下定义:

public class ImpViewer extends ImWindow
  implements Printable, Runnable
{
   [...]

ImpViewer is NOT an Appletlike it needs to be, but is instead an ImWindow. It should inherit from either Appletor perhaps ImApplet.

ImpViewer 不是Applet它应有的样子,而是一个ImWindow. 应该无论从继承Applet或者可能ImApplet

At either rate, Andrews idea of using Java Web Start is legit. The app you have looks more like a desktop app.

不管怎样,Andrews 使用 Java Web Start 的想法都是合法的。您拥有的应用程序看起来更像是桌面应用程序。

回答by Edwin Buck

An Appletis a Java component which handles the right calls to show up embedded in a web page. The product you have (the JAR file) contains everything necessary to run the program; however, it does not have the correct interface (the applet) for running that program embedded in a web page.

AnApplet是一个 Java 组件,它处理正确的调用以显示嵌入网页中。您拥有的产品(JAR 文件)包含运行程序所需的一切;但是,它没有用于运行嵌入在网页中的程序的正确界面(小程序)。

Talk to the author of the product (of if that author is not available, look for documentation) and see if a applet interface is available. Perhaps it is only a matter of using a different class name. If it looks like such an interface is not available, then no one has done the necessary work to make it "embeddable" in a web page. Without knowing your product in more detail, it's not easy to determine if the effort to create an Applet interface into the product is easy or not.

与产品的作者交谈(如果该作者不可用,请查找文档)并查看小程序界面是否可用。也许这只是使用不同的类名的问题。如果看起来这样的界面不可用,那么没有人做必要的工作使其“嵌入”在网页中。如果不更详细地了解您的产品,就很难确定在产品中创建 Applet 界面是否容易。

If you don't have the source code, then the amount of effort to develop an Applet interface to what you have is even greater than the unknown amount of effort it would have been with the source code.

如果您没有源代码,那么开发 Applet 接口的工作量甚至比使用源代码所做的未知工作量还要大。

There are a few products that do allow applications to be viewed and controlled from a web browser, even when the application in question wasn't designed to be embedded in a web page. These products tend to be expensive and proprietary; but, if it is truly mission-critical (and if it makes enough money) then the expense and effort might be bearable. With such a solution, the web browser actually opens a window into a configured "application server" which launches the application in full screen mode every time the connection is established. Yes, it is an odd architecture; however, such an odd architecture exists purposefully as that's really the only way possible to do some things when the application can't run in other environments.

有一些产品确实允许从 Web 浏览器查看和控制应用程序,即使所讨论的应用程序不是为嵌入网页而设计的。这些产品往往价格昂贵且专有;但是,如果它真的是关键任务(并且如果它赚了足够的钱),那么费用和努力可能是可以承受的。使用这样的解决方案,Web 浏览器实际上会打开一个窗口,进入配置的“应用程序服务器”,每次建立连接时,该服务器都会以全屏模式启动应用程序。是的,这是一个奇怪的架构;然而,这种奇怪的架构是有目的地存在的,因为当应用程序无法在其他环境中运行时,这确实是做某些事情的唯一方法。

Look to Citrix for such a solution in the event that you can afford it (remember there's extra windows licenses involved) and you can tolerate it's performance and quirks.

如果您负担得起,请向 Citrix 寻求此类解决方案(请记住,涉及额外的 Windows 许可证)并且您可以容忍它的性能和怪癖。