我正在使用playobufs与play framework 2.1.3没有问题。然后我需要将protobufs转换为JSON,所以我包括
"com.googlecode.protobuf-java-format" % "protobuf-java-format" % "1.2"
Build.scala中的。
尝试使用
将任何protobuf转换为JSONJsonFormat.printToString(message);
这在以开发模式运行时(以播放运行开始)会导致以下错误
play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: com/google/protobuf/InvalidProtocolBufferException]]
...
Caused by: java.lang.NoClassDefFoundError: com/google/protobuf/InvalidProtocolBufferException
...
Caused by: java.lang.ClassNotFoundException: com.google.protobuf.InvalidProtocolBufferException
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) ~[na:1.6.0_51]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_51]
at java.net.URLClassLoader.findClass(URLClassLoader.java:190) ~[na:1.6.0_51]
at java.lang.ClassLoader.loadClass(ClassLoader.java:306) ~[na:1.6.0_51]
at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ~[na:1.6.0_51]
at sbt.PlayCommands$$anonfun$53$$anonfun$55$$anon$2.loadClass(PlayCommands.scala:535) ~[na:na]
如果在生产模式下开始播放,我没有任何错误。
如果我将protobuf-java-format的源代码放在我的app文件夹中,我就可以让它在开发模式下工作。作为临时解决方案,这有效,但我想知道处理这个问题的正确方法。
其他信息: 根据下面的建议,我检查了播放类路径,播放依赖关系,并搜索了我的系统,我只包含了一个jar副本。
我可以顺利运行:
Exception e = new InvalidProtocolBufferException()
当我尝试使用protobuf-java-format库中的任何静态方法时,抛出NoClassDefFoundError。例如:
XmlFormat.printToString(message)
在开发模式下不起作用,但在生产中起作用(播放开始)。有趣的是,它所说的无法找到的课程是不同的:
[RuntimeException: java.lang.NoClassDefFoundError: com/google/protobuf/Message]
我正在使用protobuf库中的方法而没有其他地方的问题,所以我知道它们被包含在类路径中。
从谷歌,我已经能够找到另一个有类似问题的实例: https://groups.google.com/forum/#!msg/play-framework/i0RNcu8PZOY/J7cy18xsg3oJ
我无法弄清楚如何重构代码以使其正常工作。
答案 0 :(得分:0)
您确定该课程存在于1.2中吗?我发现它存在于2.3版本中。
听起来像是一个类加载器问题,然后protobuf-java-format jar在一个无法访问另一个jar的类加载器中。最好的办法是确保当你运行应用程序时,这个jar和另一个protobuf jar最终会出现在同一个目录中,所以它们最终会出现在同一个类加载器中。
你可以做的其他事情就是调用每个类中的类并获取类加载器,并且让类加载器父类也可以查看类加载器的层次结构。
调试时另一个非常有用的方法是XXXXX.class.getProtectionDomain()。getCodeSource()。getLocation()
将protXuf中存在的类替换为XXXXX,就像您的类没有加载问题的异常类一样,protobuf-java-format在加载时也遇到问题,并且还有来自protobuf-java-format的任何类。这将告诉您JVM从哪里加载两个罐子。