简体   繁体   English

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

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

I'm using Java SE 1.6 on Mac OS X 10.5.6. 我在Mac OS X 10.5.6上使用Java SE 1.6。 The code for my applet is as follows: 我的小程序的代码如下:

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. 我将此编译为.class文件。 The code for my HTML file is as follows: 我的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. 如果我使用appletviewer运行applet,它可以正常工作。 However, if I open the HTML file in a browser, a square appears for the applet space, but the applet doesn't load. 但是,如果我在浏览器中打开HTML文件,则小程序空间会出现一个正方形,但小程序不会加载。 (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. (“浏览器正在忽略小程序标记。”也不会出现。)浏览器状态栏显示两个消息: 加载 小程序HelloWorld加载Java小程序失败...我在Firefox 3.0.6,Safari 3.2中尝试了小程序.1和Opera 9.6.3。 None of them could load the 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,则在所有这三种浏览器中都可以正常加载HTML。

Is there something I'm missing here, in trying to get this simple applet to load in a browser? 在尝试让此简单小程序加载到浏览器中时,我在这里缺少什么吗?

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. 尝试启用Java控制台输出窗口。 You can do this in "Java Settings" (use spotlight) under the extended options tab (the one with the tree and many checkboxes). 您可以在扩展选项标签(带有树和许多复选框的选项卡)下的“ Java设置”(使用Spotlight)中执行此操作。 Maybe you can see some more information (like ClassNotFoundException) there. 也许您可以在那里看到更多信息(例如ClassNotFoundException)。 Set the setting to "Enable/Show console". 将设置设置为“启用/显示控制台”。 Then it should show up when you start the applet. 然后,当您启动小程序时,它应该会显示出来。

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. 不幸的是,Apple决定只在OS X上发布用于Java 6的64位VM。这意味着浏览器必须链接为64位应用。 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. 因此,“简单”的解决方案是将Java 5用于applet,您可以在Java Preferences应用程序中对其进行配置。

Phil, The comment about the code base by dhiller triggered something that worked for me. Phil,关于dhiller的代码库的评论引发了一些对我有用的事情。 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" > 如果将“ HelloWorld.class”文件与HTML文件放在同一文件夹中,然后将applet标记设置为: < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" > < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" > , then open the HTML file with your browser, it should work. < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" > ,然后使用浏览器打开HTML文件,它应该可以正常工作。 Mine did. 我的 The codebase ="." codebase ="." tells the browser that the applet code is in the same folder as the HTML file. 告诉浏览器小程序代码与HTML文件位于同一文件夹中。 The applet load fails because the browser apparently doesn't know where where the class code is located. applet加载失败,因为浏览器显然不知道类代码的位置。

Here is your answer 这是你的答案

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

Replace the packagename with your package.. 用您的软件包替换软件包名称。

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? 那可能以前被称为Java设置? The version of Java used for a desktop app does not have to be the same version as used in a browser. 用于桌面应用程序的Java版本不必与浏览器中使用的Java版本相同。 I was having a problem launching Portecle (from the command line)- it was reporting a "Bad version number in .class file". 我在从命令行启动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. 显然,我尝试加载的Java应用程序需要64位Java SE 6,因为将Java SE 6拖到该列表的顶部即可立即解决此问题,之后可以从命令行或双击或启动Portecle。 portecle.jar。 (hooray) (万岁)

Don't put quotations around your class name in the HTML file. 不要在HTML文件中的类名两边加上引号。 Every time I put quotations, the applet never works for me. 每次我引用报价时,applet都不会对我有用。 :) :)

I'm not an expert in Web programming, but I think the applet tag is not standard. 我不是Web编程专家,但是我认为applet标签不是标准的。 You have to use the object tag with the proper classid to insert an applet (or pretty much anything). 您必须使用带有正确classid的object标记来插入applet(或几乎任何东西)。

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

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. 我在Java偏好设置中打开了Java控制台。 When I ran the applet again, the following output is what I received: 当我再次运行该applet时,收到的输出如下:

Java Plug-in 1.5.0 Java插件1.5.0
Using JRE version 1.5.0_16 Java HotSpot(TM) Client VM 使用JRE版本1.5.0_16 Java HotSpot(TM)客户端VM
MRJ Plugin for Mac OS X v1.0.1 适用于Mac OS X v1.0.1的MRJ插件
[starting up Java Applet Security @ Fri Feb 06 23:47:20 CST 2009] [在2009年2月6日星期五23:47:20 CST启动Java Applet安全性]
java.lang.UnsupportedClassVersionError: Bad version number in .class file java.lang.UnsupportedClassVersionError:.class文件中的版本号错误
at java.lang.ClassLoader.defineClass1(Native Method) 在java.lang.ClassLoader.defineClass1(本机方法)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675) 在java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) 在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177) 在sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316) 在java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119) 在sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251) 在java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:605) 在sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:605)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:723) 在sun.applet.AppletPanel.createApplet(AppletPanel.java:723)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1864) 在sun.plugin.AppletViewer.createApplet(AppletViewer.java:1864)
at jep.AppletFramePanel.createApplet(AppletFramePanel.java:189) 在jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:652) 在sun.applet.AppletPanel.runLoader(AppletPanel.java:652)
at sun.applet.AppletPanel.run(AppletPanel.java:326) 在sun.applet.AppletPanel.run(AppletPanel.java:326)
at jep.AppletFramePanel.run(AppletFramePanel.java:176) 在jep.AppletFramePanel.run(AppletFramePanel.java:176)
at java.lang.Thread.run(Thread.java:613) 在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. 我在Mac上安装了Java SE 1.6,但我猜它没有安装1.6插件。 Also, it looks as if .class files get stamped with a version number when they are created. 同样,看起来.class文件在创建时似乎带有版本号。 I compiled that applet with version 1.6, but tried to run it with a 1.5 plug-in, thus resulting in the UnsupportedClassVersionError . 我用1.6版编译了该applet,但尝试使用1.5插件运行它,从而导致UnsupportedClassVersionError I re-compiled the applet with version 1.5 and tried running it in all three browsers again. 我用1.5版重新编译了applet,并尝试再次在所有三个浏览器中运行它。 Worked like a charm. 像魅力一样工作。

Does anyone know if a 1.6 plug-in is in the works? 有人知道1.6插件是否正在开发中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM