简体   繁体   English

使用服务器“不兼容的魔法值1013478509”启动时,Java applet失败

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

I'm having a problem with my applet. 我的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 不相容的魔法值1013478509

I'm using jetty-all-8.1.8.v20121106.jar, and servlet-api-3.0.jar 我正在使用jetty-all-8.1.8.v20121106.jar和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. 有效Java类的神奇值是0xCAFEBABE (十六进制值3405691582 ),这是前4个字节。 But you're getting 0x3C68746D (the hex value of 1013478509 ) which in turn stands for the ASCII characters < , h , t and m . 但是你得到0x3C68746D (十六进制值1013478509 ),它反过来代表ASCII字符<htm 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. 这与网站提供的applet相结合,表明它是<html>标签的开头,而这反过来表明它是一个HTML文档。

So, the HTTP request to applet has apparently actually returned a HTML document. 因此,对applet的HTTP请求显然实际上返回了一个HTML文档。 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. 当您将浏览器地址栏中的当前请求URI更改为指向applet的URL时,您应该能够自己查看。 Then you'll see what the browser actually retrieved when it tried to download the applet. 然后,您将看到浏览器在尝试下载applet时实际检索到的内容。 Perhaps it's a simple HTTP 404 error document in flavor of a HTML page. 也许这是HTML页面风格的简单HTTP 404错误文档。

To fix it, just make sure that the URL in the <applet> or <object> tag is correct. 要解决此问题,请确保<applet><object>标记中的URL正确无误。 It's relative to the current request URL as you see in browser address bar. 它与您在浏览器地址栏中看到的当前请求URL相关。 The way how your servlet works is quite strange. servlet的工作方式很奇怪。 You're streaming a HTML file from outside the deploy folder. 您正在从deploy文件夹外部流式传输HTML文件。 This suggests that the applet is also outside the deploy folder and thus not reachable by a valid URL at all. 这表明applet也在deploy文件夹之外,因此根本无法通过有效的URL访问。 You should put both the HTML page and the applet in the web content folder. 您应该将HTML页面和小程序放在Web内容文件夹中。 This way you don't need that servlet anymore. 这样你就不再需要那个servlet了。

This means you have a file with the .class extension which is not a class. 这意味着你有一个扩展名为.class的文件,它不是一个类。 All classes have to start with magic number of 0xCAFEBABE 所有类都必须以幻数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. 所以我怀疑这是一个HTML文件。

According to the Java Language Specification , a proper .class file has starts with the magic number : 根据Java语言规范 ,正确的.class文件以幻数开头:

The magic item supplies the magic number identifying the class file format; 魔法物品提供识别类文件格式的幻数; it has the value 0xCAFEBABE. 它的值为0xCAFEBABE。

If you open any compiled .class file with a hex editor and inspect its first bytes, they should be 0xCAFEBABE . 如果使用十六进制编辑器打开任何已编译的.class文件并检查其第一个字节,则它们应为0xCAFEBABE 1013478509 in ASCII translates to <htm . ASCII中的1013478509转换为<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. 更有可能的是,正如BalusC在他的回答中已经指出的那样,确保URL是正确的。 The <htm ... bytes you're getting might be an HTML error document served by the server. 您获得的<htm ...字节可能是服务器提供的HTML错误文档。

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

相关问题 运行Java Applet时不兼容的魔术值1013084704 - Incompatible magic value 1013084704 when running java applet Safari问题:java.lang.ClassFormatError:类文件中的魔术值1013478509不兼容 - Safari Issue: java.lang.ClassFormatError: Incompatible magic value 1013478509 in class file 尝试运行Facelets中嵌入的小程序时,魔术值1010792557不兼容 - Incompatible magic value 1010792557 when trying to run applet embedded in Facelets Java 不兼容的魔法值 4022320623 - Java Incompatible magic value 4022320623 尝试运行 Java jar 文件时出现“ClassFormatError: Incompatible magic value” - “ClassFormatError: Incompatible magic value” when trying to run Java jar file Google站点中的Java小程序和“不兼容的幻数”错误 - Java applet in Google Sites and the “Incompatible Magic Number” error 部署的JNLP小程序找不到库:不兼容的魔术值1130458734 - Deployed JNLP applet cannot find library: Incompatible magic value 1130458734 Applet不会从GlassFish(不兼容的魔术值)运行。 从OS X Web服务器正常运行 - Applet won't run from GlassFish, Incompatible magic value. Runs fine from OS X web server 获取“不兼容的魔法值”错误 Java - Getting an "Incompatible magic value' Error Java 不兼容的魔法值 1008813135 - Incompatible magic value 1008813135
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM