Java不兼容的魔法值4022320623

时间:2012-08-24 14:10:16

标签: java amazon-s3 amazon-ec2

我在服务器上编译代码并下载并试图在我的计算机上运行时遇到一个奇怪的错误。

我基本上在EC2实例上编译一些java文件,然后将它们加载到存储器中供以后使用。

当我将文件下载到我的计算机上并尝试运行它时,我收到以下错误:

Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value  
     4022320623 in class file HelloWorldPackage/HelloWorldClass
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)

我正在使用以下方法编译文件:

public void compileProject()
{


    String command = "javac ";
    for(String s: this.getPackages())
    {
        File[] files = new File("/home/benuni/CompileFiles/" + project + "/src/" + s).listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".java");
            }
        });

        for(File f: files)
        {
            command = command + f.getAbsolutePath() + " ";
        }
    }

    try {
        System.out.println("command: '"+ command +"'");
        Process pro = Runtime.getRuntime().exec(command);
         printLines(" stderr:", pro.getErrorStream());

        pro.waitFor();

        this.moveClassFiles();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

并使用此方法上传文件:

public void uploadBin()
{
    for(String s: this.getPackages())
    {
        File[] filesInPackage = new File("/home/benuni/CompileFiles/"+this.project+"/bin/"+s).listFiles();

        for(File f: filesInPackage)
        {
            String key = this.project+"/"+this.version+"/bin/"+s+"/"+f.getName();
            s3.putObject("devcloud",key,f);

        }

    }

}

有谁知道我做错了什么? 当我在计算机上编译它们时,类文件是可运行的,但是当我将它们上传到云并下载它们时我得到了错误?

谢谢,

3 个答案:

答案 0 :(得分:2)

Java告诉你它不是一个有效的类文件,因为它不是以the expected byte sequence (0xCAFEBABE)开头的。下载后出了点问题。尝试在编辑器中检查您的类文件,看看是否真的有其他内容。

答案 1 :(得分:1)

好的,我想通过感谢@Asaph提到下载出错......

基本上下载很好,这是我写文件的方式。

当我下载项目时,我正在下载源代码和二进制文件,但我写的两个文件就好像它们是一样的。

因此更改了代码以检查文件类型,然后在必要时使用相应的编写器。如果有一些奇迹,某人有同样的问题,或者在这里做类似的事情就是代码:

(请注意,这只是在5秒钟之前写的,用于解决问题并写得非常糟糕,即将自行重构,但我无法为您做任何事情)

public void download(String project, String version, String location)
{
    for(S3ObjectSummary s: getObjectList())
    {
        String[] data = s.getKey().split("/");
        if(data[0].equals(project) && data[1].equals(version))
        {
            S3Object object = s3.getObject(s3BucketName,s.getKey());
            InputStream input = object.getObjectContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(input));

            File file = new File(location +"/"+ data[0] + "/" + data[2] + "/" + data[3] + "/" + data[4]);
            if(!file.exists())
            {
                  try {
                      file.getParentFile().mkdirs();
                        file.createNewFile();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
            try 
            {
                if(data[4].endsWith(".java"))
                {
                Writer writer = new OutputStreamWriter(new FileOutputStream(file));
                while (true) {          
                     String line = reader.readLine();           
                     if (line == null)
                          break;            

                     writer.write(line + "\n");
                }

                writer.close();
                }
                else if(data[4].endsWith(".class"))
                {
                    System.out.println("Writing Classes");
                    byte[] buffer = new byte[8 * 1024];

                    try {
                          OutputStream output = new FileOutputStream(file.getAbsolutePath());
                          try {
                            int bytesRead;
                            while ((bytesRead = input.read(buffer)) != -1) {
                              output.write(buffer, 0, bytesRead);
                            }
                          } finally {
                            output.close();
                          }
                        } finally {
                          input.close();
                        }
                }

            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    }
}

答案 2 :(得分:1)

如果您使用Maven te构建您的项目。尝试在 maven-war-plugin 中禁用资源过滤,例如POM文件中的<filtering>false</filtering>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
    <webResources>
        <resource>
            <filtering>false</filtering>
            <targetPath>WEB-INF/classes</targetPath>
            <directory>${project.basedir}/target/classes</directory>
        </resource>
    </webResources>
    <filtering>false</filtering>
  </configuration>
</plugin>