Java不兼容魔术值4022320623

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

我基本上是在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);

}

}

}

有人知道我在做什么错吗?当我在计算机上编译这些类文件时,这些类文件是可运行的,但是当我将它们上传到云中并下载它们时,出现错误?

谢谢,

回答:

好吧,感谢@Asaph提到下载出错了,我想通了。

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

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

因此,更改了代码以检查文件类型,然后在必要时使用适当的编写器。如果有人奇迹般地遇到了相同的问题或正在做类似的事情,则代码如下:

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();

}

}

}

}

以上是 Java不兼容魔术值4022320623 的全部内容, 来源链接: utcz.com/qa/402399.html

回到顶部