java不兼容的魔术值40222320623

p4tfgftt  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(534)

我得到一个奇怪的错误,当我编译我的代码在服务器上下载,并试图在我的电脑上运行它。
我基本上是在一个ec2示例上编译一些java文件,然后将它们加载到一个存储器中供以后使用。
当我把这些文件下载到我的电脑上并试着运行它们时,我发现以下错误:

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

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

  1. public void compileProject()
  2. {
  3. String command = "javac ";
  4. for(String s: this.getPackages())
  5. {
  6. File[] files = new File("/home/benuni/CompileFiles/" + project + "/src/" + s).listFiles(new FilenameFilter() {
  7. public boolean accept(File dir, String name) {
  8. return name.endsWith(".java");
  9. }
  10. });
  11. for(File f: files)
  12. {
  13. command = command + f.getAbsolutePath() + " ";
  14. }
  15. }
  16. try {
  17. System.out.println("command: '"+ command +"'");
  18. Process pro = Runtime.getRuntime().exec(command);
  19. printLines(" stderr:", pro.getErrorStream());
  20. pro.waitFor();
  21. this.moveClassFiles();
  22. } catch (IOException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. } catch (InterruptedException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. } catch (Exception e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }

我用这个方法上传文件:

  1. public void uploadBin()
  2. {
  3. for(String s: this.getPackages())
  4. {
  5. File[] filesInPackage = new File("/home/benuni/CompileFiles/"+this.project+"/bin/"+s).listFiles();
  6. for(File f: filesInPackage)
  7. {
  8. String key = this.project+"/"+this.version+"/bin/"+s+"/"+f.getName();
  9. s3.putObject("devcloud",key,f);
  10. }
  11. }
  12. }

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

mf98qq94

mf98qq941#

如果您使用maven te构建您的项目。尝试禁用maven war插件中的资源过滤 <filtering>false</filtering> 在pom文件中:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <version>2.3</version>
  5. <configuration>
  6. <webResources>
  7. <resource>
  8. <filtering>false</filtering>
  9. <targetPath>WEB-INF/classes</targetPath>
  10. <directory>${project.basedir}/target/classes</directory>
  11. </resource>
  12. </webResources>
  13. <filtering>false</filtering>
  14. </configuration>
  15. </plugin>
ttp71kqs

ttp71kqs2#

好吧,多亏@asah提到了下载出错,我才知道。。
基本上下载是好的,这是我写文件的方式。
当我下载项目的时候,我下载源代码和二进制文件,但是我写这两个文件就像它们是一样的。
因此更改了代码以检查文件类型,然后在必要时使用适当的编写器。如果奇迹般地有人有同样的问题或正在做类似的事情,代码如下:
(请注意,这是5秒前刚刚写好的,是为了解决这个问题,是非常糟糕的书面,我将重构它自己,但我不能为你做一切)

  1. public void download(String project, String version, String location)
  2. {
  3. for(S3ObjectSummary s: getObjectList())
  4. {
  5. String[] data = s.getKey().split("/");
  6. if(data[0].equals(project) && data[1].equals(version))
  7. {
  8. S3Object object = s3.getObject(s3BucketName,s.getKey());
  9. InputStream input = object.getObjectContent();
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  11. File file = new File(location +"/"+ data[0] + "/" + data[2] + "/" + data[3] + "/" + data[4]);
  12. if(!file.exists())
  13. {
  14. try {
  15. file.getParentFile().mkdirs();
  16. file.createNewFile();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. try
  22. {
  23. if(data[4].endsWith(".java"))
  24. {
  25. Writer writer = new OutputStreamWriter(new FileOutputStream(file));
  26. while (true) {
  27. String line = reader.readLine();
  28. if (line == null)
  29. break;
  30. writer.write(line + "\n");
  31. }
  32. writer.close();
  33. }
  34. else if(data[4].endsWith(".class"))
  35. {
  36. System.out.println("Writing Classes");
  37. byte[] buffer = new byte[8 * 1024];
  38. try {
  39. OutputStream output = new FileOutputStream(file.getAbsolutePath());
  40. try {
  41. int bytesRead;
  42. while ((bytesRead = input.read(buffer)) != -1) {
  43. output.write(buffer, 0, bytesRead);
  44. }
  45. } finally {
  46. output.close();
  47. }
  48. } finally {
  49. input.close();
  50. }
  51. }
  52. } catch (FileNotFoundException e) {
  53. e.printStackTrace();
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. }
展开查看全部
k2arahey

k2arahey3#

java告诉您它不是有效的类文件,因为它不是以预期的字节序列开头的( 0xCAFEBABE ). 你下载时出错了。试着在编辑器中检查你的类文件,看看你是否真的有其他的内容在里面。

相关问题