com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

报错日志
我生成了一个jar包、然后放到android工程中使用、android工程报错如下。

[2019-04-28 18:00:15 - exs] Dx 1 error; aborting
[2019-04-28 18:00:15 - exs] Conversion to Dalvik format failed with error 1
[2019-04-28 19:07:57 - exs] Dx 
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
	at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
	at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
	at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
	at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
	at com.android.dx.command.dexer.Main.processClass(Main.java:704)
	at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
	at com.android.dx.command.dexer.Main.access$300(Main.java:83)
	at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
	at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
	at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
	at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
	at com.android.dx.command.dexer.Main.processOne(Main.java:632)
	at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
	at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
	at com.android.dx.command.dexer.Main.run(Main.java:246)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:188)
	at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:786)
	at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:597)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:726)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:321)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:396)
	at org.eclipse.core.internal.resources.Project$1.run(Project.java:618)
	at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
	at org.eclipse.core.internal.resources.Project.internalBuild(Project.java:597)
	at org.eclipse.core.internal.resources.Project.build(Project.java:124)
	at com.android.ide.eclipse.adt.internal.project.ProjectHelper.doFullIncrementalDebugBuild(ProjectHelper.java:1143)
	at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.launch(LaunchConfigDelegate.java:155)
	at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:855)
	at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:704)
	at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1047)
	at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
...while parsing an/hh/meq/a.class

参考资料
https://stackoverflow.com/questions/24662801/bad-class-file-magic-or-version
错误原因
我的电脑上默认用的JDK版本是1.8

C:\Users\Administrator
λ echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_144

我打的jar包版本因此也是1.8(怎么看jar包的jdk版本?直接调用这个类,然后看类的源码、底部就会出现jar的版本信息。如果这样不行,就直接用javap命令具体参考这个https://www.cnblogs.com/zyun/p/8028025.html)

/* Location:           F:\imt\exs\libs\an.hh.cc.jar
 * Qualified Name:    an.hh.cc.S
 * Java Class Version: 8 (52.0)
 * JD-Core Version:    0.7.1
 */

我的android工程用的jdk版本是1.6

选中工程--点击右键----properties---java compiler

总结:jdk版本不一致导致以上错误(jar包1.8版本、android工程1.6)
解决方法
方法一:安装1.7的jdk,这样打出的jar不报错
方法二:修改android工程jdk为1.8

你可能感兴趣的:(Android)