【发布时间】:2011-07-24 23:44:56
【问题描述】:
我正在上我的第一堂编程课,现在我即将结束学期。我有一个在线作品集,可以在其中分享我的大学成就。在这个学期的这个时候,我想把我创建的一些简单的小程序上传到我的在线作品集中。我的投资组合托管在 Weebly.com 上。我尝试将文件上传到主机站点并使用简单标签在 html 中运行小程序。我很确定我正在使用正确的目录访问文件。但在我们在那里得出任何结论之前,我决定我应该在本地运行小程序以确保我做的一切都是正确的。我在运行 OS 10.6.6 的 macbook pro 上。在 Java 首选项中,我的 Java SE 版本是 Java SE 6 64 位和 Java SE 6 32 位。我的插件版本是 1.6.0(在 /System/Library/Java/JavaVirtualMachines 中找到)。这些是我机器上唯一的版本。我的研究告诉我,我可能存在版本分歧。一些论坛建议回到插件版本 1.5(虽然,我不知道如何)。我现在很确定苹果已经将 Safari 更新为 64 位版本。我也将 Eclipse 设置为 1.6。对我来说,一切似乎都在同一页上。
是的,我已经一遍又一遍地阅读了这里的所有相关问题。现在大部分都有些过时了。
这是我的小程序代码:
/**
* Class NightScene - Draws a night scene (just for fun).
*
* @author Alex Stout
* @version February 8, 2011
*/
package lab05_1;
import java.awt.*;
import javax.swing.*;
public class NightScene extends JApplet
{
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
g.setColor(Color.BLUE.darker().darker().darker());
g.fillRect(0,0, this.getWidth(), this.getHeight());
this.drawMoon(g);
this.drawStars(g);
this.drawHorizon(g);
}
public void drawStars(Graphics h)
{
for (int i = 0 ; i <= this.getWidth()*5; i++)
{
int x = (int)(Math.random()*this.getWidth());
int y = (int)(Math.random()*this.getHeight());
h.setColor(Color.WHITE);
h.fillOval (x, y, (int) (Math.random()*3)+1, (int) (Math.random()*3)+1);
}
}
public void drawMoon(Graphics j)
{
int x = (int)(Math.random()*(this.getWidth()-200)+50);
int y = (int)(Math.random()*(this.getHeight()-200)+50);
j.setColor(Color.YELLOW.brighter().brighter());
j.fillOval (x, y, this.getWidth()/10, this.getWidth()/10);
j.setColor (Color.BLUE.darker().darker().darker());
j.fillOval (x-(this.getWidth()/100), y-(this.getWidth()/100), this.getWidth()/10, this.getWidth()/10);
}
public void drawHorizon(Graphics k)
{
int xi = 0;
int xj = this.getWidth();
int yj = this.getHeight();
int rh = this.getHeight()-this.getHeight()/8;
for (int i=0; i < xj; i++)
{
k.setColor(Color.BLACK);
k.drawLine(xi, yj, xi, rh);
k.setColor(Color.BLUE);
if(Math.random()<0.50)
{
k.drawLine(xi++, rh++, xi, rh++);
}
else
{
k.drawLine(xi++, rh--, xi, rh--);
}
}
}
}
这是我的 html 代码:
<html>
<Applet code = NightScene.class codebase = "." width = "400" height = "400">
</Applet>
</html>
这是 Java 控制台输出:
Java Plug-in 1.6.0_24
Using JRE version 1.6.0_24-b07-334-10M3326 Java HotSpot(TM) 64-Bit Server VM
User home directory = /Users/myUserName
有人建议使用 codebase = "."所以我尝试了无济于事。无论有没有它,它都不起作用。我尝试输入完整的目录路径,但没有成功。我尝试了引号,并且在类名周围没有引号。最后我尝试了使用和不使用 .class 。我尝试创建一个 lab05_1 子目录,因为这是代码中的包名。没有运气。类文件和 html 文件都在桌面上的同一个文件夹中。该类文件是在 Eclipse 中创建的原始文件的副本,但它具有相同的名称,所以我认为这不会导致在不同目录中出现任何问题。
我不知道还能做什么。请帮忙!这让我厌烦了一个星期。我花了好几个小时在这么简单的事情上。
【问题讨论】:
-
我最幸运的是使用 Firefox 运行 java 小程序,这是值得的。
-
当我在 Eclipse 中运行它时,我忘了提到它确实在 applet 查看器中运行。只是当我尝试在浏览器(Safari 或 Firefox)中运行它们时它们不起作用。
-
@CarlosZ 感谢 CarlosZ,我也一直在尝试 firefox,但无济于事。 ://
-
我不知道发生了什么,但我现在从小程序测试中获得了更多生命。它显示了 java 加载图像,然后通知我它无法加载并且出现错误。控制台现在返回一个更加生动的错误。这是一个 NoClassFound 异常。然后它似乎想要我的包名或其他东西。
Exception: java.lang.NoClassDefFoundError: NightScene (wrong name: lab05_1/NightScene)
标签: java html macos safari applet