如何在Mac OS X中的浏览器中使用简单的Hello World Java小程序?

时间:2022-11-29 07:23:53

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。我的applet的代码如下:

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 .class file. 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 notloaded and 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运行applet,它运行正常。但是,如果我在浏览器中打开HTML文件,则会显示applet空间的方块,但不会加载applet。 (“你的浏览器忽略了applet标签。”也没有出现。)浏览器状态栏显示两条消息:Applet HelloWorld未加载和加载Java Applet失败...我尝试使用Firefox 3.0.6,Safari 3.2的applet .1和Opera 9.6.3。他们都不能加载applet。

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 / Applets中打开一个Java演示小程序的HTML,它会在所有三个浏览器中正常加载。

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

是否有一些我在这里缺少的东西,试图让这个简单的小程序加载到浏览器中?

9 个解决方案

#1


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

您没有在applet标记中指定codebase属性,因此我猜您的类无法找到。

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)。将设置设置为“启用/显示控制台”。然后它应该在你启动applet时出现。

#2


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上发布64位VM for Java 6.这意味着浏览器必须作为64位应用程序链接。现在,他们都没有(据我所知)。因此,“简单”解决方案是将Java 5用于可在Java Preferences应用程序中配置的applet。

#3


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标记设置为: ,然后用浏览器打开HTML文件,它应该可以工作。我做了。 codebase =“。”告诉浏览器applet代码与HTML文件位于同一文件夹中。 applet加载失败,因为浏览器显然不知道类代码的位置。

#4


Here is your answer

这是你的答案

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

Replace the packagename with your package..

用您的包替换包名称..

#5


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标签来插入applet(或几乎任何东西)。

#6


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

你没有给apllet一个容器等来展示自己。这是一个示例HelloWorld applet:

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

#7


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 Preferences中打开了Java控制台。当我再次运行applet时,以下输出是我收到的:

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使用JRE版本1.5.0_16适用于Mac OS X v1.0.1的Java HotSpot(TM)客户端VM MRJ插件[启动Java Applet Security @ Fri Feb 06 23:47:20 CST 2009] java.lang .UnsupportedClassVersionError:java.security.SecureClassLoader.defineClass(SecureClassLoader.java:)java.lang.ClassLoader.defineClass(ClassLoader.java:675)java.lang.ClassLoader.defineClass1(Native Method)的.class文件中的错误版本号124)在sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)的java.lang.applet.ClassLoader.loadClass(ClassLoader.java:316),位于java的sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)。 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)在sun.applet.AppletPanel.runLoader(AppletPanel.java:652)的jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)在sun.applet.AppletPanel.run(AppletPanel.java:326)的jep.AppletFramePanel.run(AppletFramePanel.java:176)at 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 .class files 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版本的applet,但尝试使用1.5插件运行它,从而导致UnsupportedClassVersionError。我用1.5版重新编译了applet,并尝试再次在所有三个浏览器中运行它。工作就像一个魅力。

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

有谁知道1.6插件是否在工作?

#8


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

不要在HTML文件中放置类名称的引号。每次我引用引号时,applet都不适用于我。 :)

#9


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首选项中的应用程序控制

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 Settings?用于桌面应用程序的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位

  • J2SE 5.0.......32 bit
  • J2SE 5.0 ....... 32位

  • Java SE 6.....64 bit
  • Java SE 6 ..... 64位

  • J2SE 5.0.......64 bit
  • J2SE 5.0 ....... 64位

  • J2SE 1.4.2 ...32 bit
  • 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 SE 6 64位Java应用程序,因为将Java SE 6拖到该列表的顶部会立即修复问题,并且之后可以从命令行或双击或者可以启动Portecle。 portecle.jar。 (万岁)

#1


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

您没有在applet标记中指定codebase属性,因此我猜您的类无法找到。

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)。将设置设置为“启用/显示控制台”。然后它应该在你启动applet时出现。

#2


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上发布64位VM for Java 6.这意味着浏览器必须作为64位应用程序链接。现在,他们都没有(据我所知)。因此,“简单”解决方案是将Java 5用于可在Java Preferences应用程序中配置的applet。

#3


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标记设置为: ,然后用浏览器打开HTML文件,它应该可以工作。我做了。 codebase =“。”告诉浏览器applet代码与HTML文件位于同一文件夹中。 applet加载失败,因为浏览器显然不知道类代码的位置。

#4


Here is your answer

这是你的答案

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

Replace the packagename with your package..

用您的包替换包名称..

#5


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标签来插入applet(或几乎任何东西)。

#6


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

你没有给apllet一个容器等来展示自己。这是一个示例HelloWorld applet:

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

#7


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 Preferences中打开了Java控制台。当我再次运行applet时,以下输出是我收到的:

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使用JRE版本1.5.0_16适用于Mac OS X v1.0.1的Java HotSpot(TM)客户端VM MRJ插件[启动Java Applet Security @ Fri Feb 06 23:47:20 CST 2009] java.lang .UnsupportedClassVersionError:java.security.SecureClassLoader.defineClass(SecureClassLoader.java:)java.lang.ClassLoader.defineClass(ClassLoader.java:675)java.lang.ClassLoader.defineClass1(Native Method)的.class文件中的错误版本号124)在sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)的java.lang.applet.ClassLoader.loadClass(ClassLoader.java:316),位于java的sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)。 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)在sun.applet.AppletPanel.runLoader(AppletPanel.java:652)的jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)在sun.applet.AppletPanel.run(AppletPanel.java:326)的jep.AppletFramePanel.run(AppletFramePanel.java:176)at 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 .class files 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版本的applet,但尝试使用1.5插件运行它,从而导致UnsupportedClassVersionError。我用1.5版重新编译了applet,并尝试再次在所有三个浏览器中运行它。工作就像一个魅力。

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

有谁知道1.6插件是否在工作?

#8


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

不要在HTML文件中放置类名称的引号。每次我引用引号时,applet都不适用于我。 :)

#9


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首选项中的应用程序控制

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 Settings?用于桌面应用程序的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位

  • J2SE 5.0.......32 bit
  • J2SE 5.0 ....... 32位

  • Java SE 6.....64 bit
  • Java SE 6 ..... 64位

  • J2SE 5.0.......64 bit
  • J2SE 5.0 ....... 64位

  • J2SE 1.4.2 ...32 bit
  • 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 SE 6 64位Java应用程序,因为将Java SE 6拖到该列表的顶部会立即修复问题,并且之后可以从命令行或双击或者可以启动Portecle。 portecle.jar。 (万岁)