简体   繁体   中英

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

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 get an empty frame with a red text in it. when I click the text I get the message:

incompatible magic value 1013478509

I'm using jetty-all-8.1.8.v20121106.jar, and servlet-api-3.0.jar

The class that starts up the server looks like this:

public class TheServer extends HttpServlet {
    private static final long serialVersionUID = 1L; 
    private Scanner sc;
    private String webSite;
    private PrintWriter out;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        resp.setStatus(HttpServletResponse.SC_OK);
        out = resp.getWriter();

        sc = new Scanner(new File("F:\\Users\\Johan\\Workspace Kurs 5\\PongOnline\\bin\\pong.html"));
        webSite = "";
        while(sc.hasNext())
            webSite += sc.nextLine();
        sc.close();

        out.println(webSite);
        System.out.println(webSite);
    }

    public static void main(String...args) throws Exception {
        ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS);
        context.addServlet(TheServer.class, "/");

        Server server = new Server(666);
        server.setHandler(context);
        server.start();
        server.join();
    }
}

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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