macos 如何让一个简单的 Hello World Java 小程序在 Mac OS X 的浏览器中工作?

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

How do I get a simple, Hello World Java applet to work in a browser in Mac OS X?

javahtmlmacosapplet

提问by Phillip Lemky

I'm using Java SE 1.6 on Mac OS X 10.5.6. The code for my applet is as follows:

我在 Mac OS X 10.5.6 上使用 Java SE 1.6。我的小程序的代码如下:

import java.awt.Graphics;
import javax.swing.JApplet;

public class HelloWorld extends JApplet {

    public void paint( Graphics g ) {
            super.paint( g );
            g.drawString( "Hello World!", 25, 25 );
    }

}

I compiled this to a .classfile. The code for my HTML file is as follows:

我将其编译为.class文件。我的 HTML 文件的代码如下:

<html>

<head>
    <title>Hello World</title>
</head>

<body>
    <applet code="HelloWorld.class" height="300" width="300">
            Your browser is ignoring the applet tag.
    </applet>
</body>

</html>

If I run the applet with appletviewer, it works fine. However, if I open the HTML file in a browser, a square appears for the applet space, but the applet doesn't load. (The "Your browser is ignoring the applet tag." doesn't appear either.) The browser status bar shows two messages: Applet HelloWorld notloadedand Loading Java Applet Failed...I tried the applet with Firefox 3.0.6, Safari 3.2.1, and Opera 9.6.3. None of them could load the applet.

如果我用appletviewer运行小程序,它工作正常。但是,如果我在浏览器中打开 HTML 文件,小程序空间会出现一个正方形,但小程序不会加载。(“您的浏览器正在忽略小程序标记。”也没有出现。)浏览器状态栏显示两条消息: 小程序 HelloWorld 未加载加载 Java 小程序失败...我在 Firefox 3.0.6、Safari 3.2 上尝试了小程序.1 和 Opera 9.6.3。他们都无法加载小程序。

If I try opening the HTML for one of the Java demo applets in /Developer/Examples/Java/Applets, it loads fine in all three browsers.

如果我尝试在/Developer/Examples/Java/Applet 中打开其中一个 Java 演示小程序的 HTML ,它会在所有三个浏览器中正常加载。

Is there something I'm missing here, in trying to get this simple applet to load in a browser?

在尝试让这个简单的小程序加载到浏览器中时,我在这里缺少什么吗?

采纳答案by Phillip Lemky

Thank you for all your answers. Some of them pointed me in the right direction to figure out the source of the problem.

感谢您的所有回答。他们中的一些人为我指出了正确的方向,以找出问题的根源。

I turned on the Java Console in Java Preferences. When I ran the applet again, the following output is what I received:

我在 Java 首选项中打开了 Java 控制台。当我再次运行小程序时,我收到以下输出:

Java Plug-in 1.5.0
Using JRE version 1.5.0_16 Java HotSpot(TM) Client VM
MRJ Plugin for Mac OS X v1.0.1
[starting up Java Applet Security @ Fri Feb 06 23:47:20 CST 2009]
java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:605)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:723)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1864)
at jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:652)
at sun.applet.AppletPanel.run(AppletPanel.java:326)
at jep.AppletFramePanel.run(AppletFramePanel.java:176)
at java.lang.Thread.run(Thread.java:613)

Java Plug-in 1.5.0
Using JRE version 1.5.0_16 Java HotSpot(TM) Client VM
MRJ Plugin for Mac OS X v1.0.1
[启动 Java Applet Security @ Fri Feb 06 23:47:20 CST 2009]
java.lang .UnsupportedClassVersionError: 错误版本号在
java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java: 124)
在 sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)
在 java.lang.ClassLoader.loadClass(ClassLoader.java:316)
在 sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
在 java.lang.ClassLoader.loadClass(ClassLoader.java:251)
在 sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:605)
在 sun.applet.AppletPanel.createApplet(
AppletPanel.java:723 )在 sun.plugin。 AppletViewer.createApplet(AppletViewer.java:1864)
在jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)
在sun.applet.AppletPanel.runLoader(AppletPanel.java:652)
在sun.applet.AppletPanel.run(AppletPanel.java :326)
在 jep.AppletFramePanel.run(AppletFramePanel.java:176)
在 java.lang.Thread.run(Thread.java:613)

I installed Java SE 1.6 on my Mac, but I guess it didn't install a 1.6 plug-in. Also, it looks as if .classfiles get stamped with a version number when they are created. I compiled that applet with version 1.6, but tried to run it with a 1.5 plug-in, thus resulting in the UnsupportedClassVersionError. I re-compiled the applet with version 1.5 and tried running it in all three browsers again. Worked like a charm.

我在 Mac 上安装了 Java SE 1.6,但我猜它没有安装 1.6 插件。此外,看起来好像.class文件在创建时被标记了版本号。我用 1.6 版编译了那个小程序,但试图用 1.5 版插件运行它,从而导致UnsupportedClassVersionError。我用 1.5 版重新编译了小程序,并再次尝试在所有三个浏览器中运行它。像魅力一样工作。

Does anyone know if a 1.6 plug-in is in the works?

有谁知道 1.6 插件是否正在开发中?

回答by Daniel Hiller

You do not specify a codebase property in the applet tag, so I'd guess your class can not be found.

您没有在小程序标记中指定代码库属性,所以我猜您的类无法找到。

Try to enable the java console output window. You can do this in "Java Settings" (use spotlight) under the extended options tab (the one with the tree and many checkboxes). Maybe you can see some more information (like ClassNotFoundException) there. Set the setting to "Enable/Show console". Then it should show up when you start the applet.

尝试启用 java 控制台输出窗口。您可以在扩展选项选项卡(带有树和许多复选框的选项卡)下的“Java 设置”(使用聚光灯)中执行此操作。也许您可以在那里看到更多信息(如 ClassNotFoundException)。将设置设置为“启用/显示控制台”。然后它应该在您启动小程序时出现。

回答by Guemundur Bjarni

Unfortunately Apple decided to only release a 64bit VM for Java 6 on OS X. The implication of this is that the browsers have to be linked as 64bit apps. Right now, none of them does (as far as I know). So the "easy" solution is to use Java 5 for applets which you can configure in the Java Preferences application.

不幸的是,Apple 决定只在 OS X 上发布 Java 6 的 64 位 VM。这意味着浏览器必须链接为 64 位应用程序。现在,他们都没有(据我所知)。因此,“简单”的解决方案是将 Java 5 用于小程序,您可以在 Java 首选项应用程序中对其进行配置。

回答by Jeff Jensen

Phil, The comment about the code base by dhiller triggered something that worked for me. If you put the "HelloWorld.class" file in the same folder as your HTML file, and then set your applet tag to: < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" >, then open the HTML file with your browser, it should work. Mine did. The codebase ="."tells the browser that the applet code is in the same folder as the HTML file. The applet load fails because the browser apparently doesn't know where where the class code is located.

Phil,dhiller 对代码库的评论引发了一些对我有用的事情。如果将“HelloWorld.class”文件与 HTML 文件放在同一文件夹中,然后将小程序标记设置为: < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" >,然后用浏览器打开 HTML 文件,它应该可以工作。我的做到了。该codebase ="."告诉浏览器的小应用程序代码是在同一文件夹中的HTML文件。小程序加载失败,因为浏览器显然不知道类代码位于何处。

回答by Dave Nathanson

Just a note. This thread helped get me on track, so I'd just like to add a helpful note. August 9th 2010.

只是一个注释。这个线程帮助我走上正轨,所以我只想添加一个有用的注释。2010 年 8 月 9 日。

In Mac OS X 10.5.8 Leopard the version of Java used is controlled by an app in the path Mac HD/Applications/Utilities/Java Preferences

在 Mac OS X 10.5.8 Leopard 中,使用的 Java 版本由路径 Mac HD/Applications/Utilities/Java Preferences 中的应用程序控制

That may have formerly been named Java Settings? The version of Java used for a desktop app does not have to be the same version as used in a browser. I was having a problem launching Portecle (from the command line)- it was reporting a "Bad version number in .class file".

那可能以前被命名为 Java 设置?用于桌面应用程序的 Java 版本不必与浏览器中使用的版本相同。我在启动 Portecle(从命令行)时遇到问题 - 它报告了“.class 文件中的错误版本号”。

As of August 2010 the Mac OS X 10.5.8 default Java sequence is:

截至 2010 年 8 月,Mac OS X 10.5.8 默认 Java 序列为:

  • J2SE 5.0.......32 bit
  • J2SE 5.0.......32 bit
  • Java SE 6.....64 bit
  • J2SE 5.0.......64 bit
  • J2SE 1.4.2 ...32 bit
  • J2SE 5.0.......32 位
  • J2SE 5.0.......32 位
  • Java SE 6.....64 位
  • J2SE 5.0.......64 位
  • J2SE 1.4.2 ...32 位

Apparently the Java app I was trying to load needed Java SE 6 64 bit, because dragging Java SE 6 to the top of that list instantly fixed the problem, and Portecle was launchable after that from either the command line or with a double-click or portecle.jar. (hooray)

显然,我尝试加载的 Java 应用程序需要 Java SE 6 64 位,因为将 Java SE 6 拖到该列表的顶部立即解决了该问题,之后可以从命令行或双击启动 Portecle 或portecle.jar。(万岁)

回答by ra4king

Don't put quotations around your class name in the HTML file. Every time I put quotations, the applet never works for me. :)

不要在 HTML 文件中的类名周围加上引号。每次我引用时,小程序都不适合我。:)

回答by Java D

Here is your answer

这是你的答案

<html>
   <applet code="packagename.HelloWorld.class" height="300" width="300">
</html>

Replace the packagename with your package..

用你的包替换包名..

回答by Charlie Martin

You're not giving the apllet a container etc to show itself in. Here's an example HelloWorld applet:

你没有给小程序一个容器等来显示自己。这是一个HelloWorld 小程序示例

/*
  File.......: MyHello_JApplet.java
  Description: Basic JApplet example.  (Run by a browser.)
  Programmer.: Michael Thomas
  Date.......: Updated 09/09/01, Orig 09/09/01

*/
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Container;
import java.awt.Color;

public class MyHello_JApplet extends JApplet {

  public void init() {
    Container objContainer = super.getContentPane();
    objContainer.setBackground( Color.white );
  }
  public void paint(Graphics g) {
    g.drawString("Hello World from JApplet (Swing - JApplet).",10,25);
  }
}

回答by pyon

I'm not an expert in Web programming, but I think the applet tag is not standard. You have to use the object tag with the proper classid to insert an applet (or pretty much anything).

我不是Web 编程方面的专家,但我认为applet 标记不是标准的。您必须使用具有正确 classid 的 object 标记来插入小程序(或几乎任何东西)。