java applet fail when launched with server “incompatible magic value 1013478509”

前端 未结 3 1472
情书的邮戳
情书的邮戳 2021-01-21 02:38

I\'m having a problem with my applet. I have a school project where I\'m supposed to make a pong-online game. It runs fine offline but when I try to load it from a server I just

相关标签:
3条回答
  • 2021-01-21 03:17

    This means you have a file with the .class extension which is not a class. All classes have to start with magic number of 0xCAFEBABE

    The first four bytes of your "class" reads

    System.out.println(new String(BigInteger.valueOf(1013478509).toByteArray()));
    

    prints

    <htm
    

    so I suspect it's a HTML file.

    0 讨论(0)
  • 2021-01-21 03:20

    The magic value of a valid Java class is 0xCAFEBABE (the hex value of 3405691582), which is the first 4 bytes. But you're getting 0x3C68746D (the hex value of 1013478509) which in turn stands for the ASCII characters <, h, t and m. To see it yourself, run this piece of code:

    int magic = 1013478509;
    ByteBuffer b = ByteBuffer.allocate(4);
    b.putInt(magic);
    System.out.println(new String(b.array()));
    

    This in combination with the applet being served by a website suggests that it's the start of a <html> tag which in turn suggests that it's a HTML document.

    So, the HTTP request to applet has apparently actually returned a HTML document. You should be able to see it yourself when you change the current request URI in browser address bar to point to applet's URL. Then you'll see what the browser actually retrieved when it tried to download the applet. Perhaps it's a simple HTTP 404 error document in flavor of a HTML page.

    To fix it, just make sure that the URL in the <applet> or <object> tag is correct. It's relative to the current request URL as you see in browser address bar. The way how your servlet works is quite strange. You're streaming a HTML file from outside the deploy folder. This suggests that the applet is also outside the deploy folder and thus not reachable by a valid URL at all. You should put both the HTML page and the applet in the web content folder. This way you don't need that servlet anymore.

    0 讨论(0)
  • 2021-01-21 03:28

    According to the Java Language Specification, a proper .class file has starts with the magic number :

    The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

    If you open any compiled .class file with a hex editor and inspect its first bytes, they should be 0xCAFEBABE. 1013478509 in ASCII translates to <htm.

    Make sure you've got the class properly compiled on the server. And more likely, as BalusC already pointed out in his answer, make sure URL's are correct. The <htm... bytes you're getting might be an HTML error document served by the server.

    0 讨论(0)
提交回复
热议问题