简体   繁体   English

无法修复Java中的NoClassDefFound错误

[英]Can't fix NoClassDefFound error in Java

I build project in eclipse - swing applet and now I'm trying to run it in a browser. 我在Eclipse中构建项目-swing applet,现在我试图在浏览器中运行它。

I have 3 packages, let's say they are called: "pkgApplet", "pkgFirst", "pkgSecond" with .class files. 我有3个程序包,假设它们被称为:带有.class文件的“ pkgApplet”,“ pkgFirst”,“ pkgSecond”。 In pkgApplet I have class "main" with method main() . 在pkgApplet中,我具有方法main()类“ main”。 No matter what I do, I can't run this applet in browser. 无论我做什么,我都无法在浏览器中运行此小程序。 Currently my html code looks like this 目前我的html代码看起来像这样

<applet code="bin/pkgApplet/main" height="1000" width="1000"/>

Browser gives this error every time no matter how I modify applet tag: 无论我如何修改applet标签,浏览器都会给出此错误:

NoClassDefFoundError with message bin/pkgApplet/main(wrong name: applet/main)

I tried using codebase attribute, packing applet into .jar file and using archive attribute, but nothing seems to work. 我尝试使用codebase属性,将applet包装到.jar文件中,并使用archive属性,但是似乎没有任何效果。 Do you have any idea what am I doing wrong? 你知道我在做什么错吗?

Your applet format should be: 您的小程序格式应为:

<applet codebase="bin" code="pkgApplet.main" height="1000" width="1000"></applet>

bin is the default target directory (for Eclipse) so will require the codebase attribute as shown above. bin是默认目标目录(对于Eclipse),因此将需要codebase属性,如上所示。 For this to work, your HTML file needs to be located in your project directory. 为此,您的HTML文件必须位于项目目录中。

Note classes in Java start with uppercase, while package names are lowercase. 注意Java中的类以大写字母开头,而程序包名称则为小写字母。 Also its helpful to name classes describing what they do. 命名类描述它们的作用也很有帮助。 You could have instead: 您可以改为:

<applet codebase="bin" code="pkgapplet.MyMainApplet" height="1000" width="1000"></applet>

Do you realize that nothing in main will be called by your applet client? 您是否意识到applet客户端不会调用main Any startup functionality should be placed in the init method. 任何启动功能都应放在init方法中。

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

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