Proguard:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.ObjectMapper

时间:2016-02-01 18:24:12

标签: java javafx jackson proguard obfuscation

我们正在尝试使用Proguard 5.2来混淆JavaFX中的应用程序,该应用程序具有Jackson Json作为Json序列化程序。应用程序在没有使用Proguard的情况下工作正常,但是当我们对代码进行模糊处理时,我们只是通过实例化一个新的ObjectMapper来获得异常,Java声称它找不到这个类。

ObjectMapper mapper = new ObjectMapper();

异常消息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at br.ufmg.dcc.isajavafx.excelreader.ISAExcelTemplate.toJSON(ISAExcelTemplate.java:315)
    at br.ufmg.dcc.isajavafx.controller.MainController.checkConfigurationFiles(MainController.java:607)
    at br.ufmg.dcc.isajavafx.controller.MainController.initialize(MainController.java:632)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at br.ufmg.dcc.isajavafx.ISAJavaFX.start(ISAJavaFX.java:30)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 15 more
Exception running application mypackage.myclass

这是我们的proguard配置文件:

-libraryjars /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar
-libraryjars /usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar
-libraryjars target/lib/jackson-annotations-2.7.0.jar
-libraryjars target/lib/jackson-core-2.7.0.jar
-libraryjars target/lib/jackson-databind-2.7.1-SNAPSHOT.jar
-libraryjars target/lib/commonj.sdo-2.1.1.jar
-libraryjars target/lib/commons-codec-1.9.jar
-libraryjars target/lib/controlsfx-8.40.11.jar
-libraryjars target/lib/javax.persistence-2.1.0.jar
-libraryjars target/lib/poi-3.13.jar
-libraryjars target/lib/poi-ooxml-3.13.jar
-libraryjars target/lib/poi-ooxml-schemas-3.13.jar
...
-keepnames class org.codehaus.jackson.** { *; }
-keepnames class com.fasterxml.jackson.databind.** { *; }
-dontwarn com.fasterxml.jackson.databind.**
-keep public class classes.to.be.serialized** {public private protected *;}

classes.to.be.serialized的所有成员都是公共的,有些是原始类型,其他的不是基元,也应该被忽略。

问题是我们收到的错误就像找不到杰克逊图书馆一样。在混淆之前和之后,罐子都在我们的类路径中。

1 个答案:

答案 0 :(得分:0)

解决了 问题出在jackson-databind版本上。其他libs版本是2.7.0,数据绑定是2.7.1-SNAPSHOT。我不知道为什么问题只出现在程序被混淆时,但解决方案是将jackson libs均衡为同一版本。 我也在这篇文章中得到了解决方法: 解析JSON对象时出现NoClassDefFoundError JsonAutoDetect