简体   繁体   English

获取“不兼容的魔法值”错误 Java

[英]Getting an "Incompatible magic value' Error Java

Exception in thread "main" java.lang.ClassFormatError: Incompatible magic 
value 813187905 in class file

I understand the error, as it's quite clear.我理解错误,因为它很清楚。 I need to somehow get java's magic value, 0xCAFEBABE (hex) written to the .java file I'm writing programmatically first.我需要以某种方式将 java 的魔法值0xCAFEBABE (hex) 写入我首先以编程方式编写的 .java 文件中。 I've read all the answers to similar questions but they all point to bringing in 3rd party libraries to write the java file programmatically.我已经阅读了类似问题的所有答案,但它们都指向引入 3rd 方库以编程方式编写 java 文件。

I can't help but think that there is a relatively straight forward solution to this without bringing in a 3rd party lib?我不禁认为有一个相对直接的解决方案,而无需引入 3rd 方库? How can I write a hex value to an arbitrary file?如何将十六进制值写入任意文件?

Right now, I'm taking a string.现在,我正在拿一根绳子。 converting it to bytes, and then writing those bytes to a FileInputStream which then dumps to a file with .java extension.将其转换为字节,然后将这些字节写入FileInputStream ,然后转储到扩展名为 .java 的文件。 Code I'm using is below.我正在使用的代码如下。 How can I instead of writing bytes, write hex values?我怎样才能不写字节,而是写十六进制值? Encodings are not my strong suite so this problem is really giving me some trouble.编码不是我的强项,所以这个问题真的给我带来了一些麻烦。

Code I'm using to write my java file:我用来编写我的java文件的代码:

public void writeJavaFile() throws IOException 
{
    String testString = "class test { public test() {} public void printTest()
    { System.out.println(\"yoooo\");}}";
    String filename = "test.java";
    OutputStream outputStream = new FileOutputStream(filename);
    outputStream.write(testString.getBytes());
    outputStream.flush();
    outputStream.close();
}

I was thinking about this problem all wrong and therefore probably posted a misaligned question.我在想这个问题全错了,因此可能发布了一个错位的问题。 I was under the assumption I was needing to compile my own java file to class file which is where the magic value is actually written.我假设我需要将自己的 java 文件编译为类文件,这是实际写入魔术值的地方。

Obviously I can just have java's compiler compile my programmatically written java file.显然,我可以让 java 的编译器编译我以编程方式编写的 java 文件。 Here is the code I used to get this to work:这是我用来让它工作的代码:

   `CustomClassLoader classLoader = new CustomClassLoader();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int result = compiler.run(null,null,null, "test.java");
    System.out.println("Result: " + result);
    if (result != 0) {
       System.out.println("Error compiling class!!");
       return;
    }
    // Read in newly compiled class file
    Path path = Paths.get("test.class");
    byte[] bytes = Files.readAllBytes(path);
    // Load class into JVM
    Class myClass = classLoader.loadClazz(bytes);`

It's more likely that you downloaded a corrupted .class file.您下载了损坏的 .class 文件的可能性更大。 You might want to figure out if you can find the correct file somewhere.您可能想弄清楚是否可以在某处找到正确的文件。 If that is impossible, you can recompile the source code again if you have it.如果这是不可能的,您可以重新编译源代码(如果有的话)。

Even if you manage to write the magic value to the file correctly, the file itself might still contain (unrunnable) gibberish.即使您设法将魔法值正确写入文件,文件本身仍可能包含(无法运行的)乱码。

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

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