File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes getting the <APPLET> tag to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "getting the <APPLET> tag to work" Watch "getting the <APPLET> tag to work" New topic
Author

getting the <APPLET> tag to work

Nicholas mariano
Greenhorn

Joined: Jun 14, 2011
Posts: 5
I am a novice programmer. I have a very basic knowledge of Java, but I have never worked with html or with Java applets. I recently wrote my first applet using Netbeans IDE 6.9.1. When I open the file and hit run, the applet appears as a new window, so I know it works.
My goal is to put the applet on a webpage, but so far, I have been unable to do that. Could anyone give me a clear rundown of the steps I should take?

I have tried simply putting:

<APPLET CODE="AppletTest.class" WIDTH="1100" HEIGHT="700">
</APPLET>


but it gives me this error message.



java.lang.ClassFormatError: Incompatible magic value 1008813135 in class file AppletTest
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassFormatError: Incompatible magic value 1008813135 in class file AppletTest

Do I need to do something special with the file so that the tag will recognize it? Is there somewhere I need to move my .class file, and if so, how do I do that? Any help would be appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, it's clear that the browser's Java support is finding your class file just fine... it's complaining about its contents.

The "magic number" it's complaining about is the first four bytes of the class file. Those bytes are supposed to contain a specific set of values (actually "CAFEBABE" in hexadecimal), but they don't. What you see in the error message is the decimal value of the magic number; 1008813135 decimal is equal to 3C21444F in hexadecimal.

And when you look at an ASCII table, you can see that 3C21444F hexadecimal corresponds to the four characters "<!DO". And that suggests to me that what the browser is downloading from your so-called "class" file is actually some kind of XML or HTML document which starts with a DOCTYPE. So put your actual compiled class file there instead.>
Nicholas mariano
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Okay. So, just to be sure, there is no problem with the html tag itself?

I will look around for it, but where would this XML/HTML file be coming from, and how can I find where it is located?
I found where that file was and moved my class file there. I am still getting a similar error message.

Something I forgot to mention in my original post is that AppletTest is a main method that creates a HelloWorldPanel(), which is in a separate class. Do I need to somehow also call HelloWorldPanel as a parameter for the applet?

Thanks for your response.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41487
    
  51
AppletTest must be an applet ; the browser wouldn't know what to do with a main method.


Ping & DNS - my free Android networking tools app
Nicholas mariano
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Oh my. That was stupid of me. Okay. I will try to convert what I have into an actual applet and see how it goes. Thanks for the help and advice.
Nicholas mariano
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Okay. It no longer has a main method and is contained within one class. I found the HTML document called AppletTest and moved AppletTest.class to the same location. The same magic number error is occurring which means it is still reading the wrong file for some reason. Any suggestions?
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Does your class extends java.applet.Applet class?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Nicholas mariano wrote: I found the HTML document called AppletTest and moved AppletTest.class to the same location.

The same location as what?
The same magic number error is occurring which means it is still reading the wrong file for some reason. Any suggestions?

Here's how you should have things set up:

1. You have an HTML file with the <APPLET> element in it. You put that somewhere so that the URL http://yaddayaddayadda/something.html will cause it to open in the browser; on the server side it's in some directory.

2. You have an applet named AppletTest.class; it should be in the same directory as the HTML file. (Was that what you meant by "the same location"?)

At this point you should be able to type the URL http://yaddayaddayadda/AppletTest.class into your browser. This should cause the browser to ask you what you want to do with that file. If you see HTML, then clear the cache in your browser.
Nicholas mariano
Greenhorn

Joined: Jun 14, 2011
Posts: 5
@Zandis Yes, it extends JApplet.

@Paul I searched through My Computer and found an HTML file with the same name as my applet. When I open that file, it opens a new window with file:///C:/Users/USERNAME/Documents/NetBeansProjects/PROJECTNAME/build/AppletTest.html in the URL bar, and the working applet in the window.
Is this the yadayadayada.html or am I looking in the wrong place?

By the same place, I mean that when I found the HTML in the build folder, I moved the applet to the same folder.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Nicholas mariano wrote:I searched through My Computer and found an HTML file with the same name as my applet.


Then this is your problem. If you have an HTML file named "Applet.class", then the browser's JVM will try to load it as a Java class, and will fail with exactly the message you have been describing. Your HTML files should have names which end with ".htm" or with ".html".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the <APPLET> tag to work