Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

getting the <APPLET> tag to work

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 25599
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AppletTest must be an applet ; the browser wouldn't know what to do with a main method.
 
Nicholas mariano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your class extends java.applet.Applet class?
 
Paul Clapham
Marshal
Posts: 25599
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Marshal
Posts: 25599
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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".
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic