web-dev-qa-db-fra.com

Pourquoi Drools ne fonctionne-t-il pas avec Java 8?

Je viens juste d'installer la version finale de Java 8. Lorsque j'essaie de construire mon projet avec Maven, de nombreux tests échouent si j'utilise Java 8, mais je réussis sans problème avec Java 7. Je l'ai essayé avec la ligne de commande avec Java_HOME C:\Program Files\Java\jdk1.8.0 et via Eclipse avec jdk1.8.0 sélectionné comme JRE par défaut dans les JRE installés. Les deux échouent. Notez que les tests qui échouent utilisent Drools 6 (et ses dépendances).

Voici la sortie:

-------------------------------------------------------------------------------
Test set: com.local.lds.rules.LocmChiTest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 1.245 sec <<< FAILURE!
test(com.local.lds.rules.LocmChiTest)  Time elapsed: 0.001 sec  <<< ERROR!
Java.lang.ExceptionInInitializerError
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.Java:62)
    at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.Java:45)
    at Java.lang.reflect.Constructor.newInstance(Constructor.Java:408)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java:187)
    at Sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invoke(MethodReflection.Java:63)
    at mockit.Invocation.proceed(Invocation.Java:192)
    at mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator.createTest(BlockJUnit4ClassRunnerDecorator.Java:59)
    at Sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invokeWithCheckedThrows(MethodReflection.Java:95)
    at mockit.internal.mockups.MockMethodBridge.callMock(MockMethodBridge.Java:75)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.Java:41)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.Java:236)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:15)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.Java:233)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:300)
    at org.Apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.Java:53)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.Java:123)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.Java:104)
    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:483)
    at org.Apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.Java:164)
    at org.Apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.Java:110)
    at org.Apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.Java:175)
    at org.Apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.Java:107)
    at org.Apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.Java:68)
Caused by: Java.lang.RuntimeException: wrong class format
    at org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.Java:279)
    at org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.Java:219)
    at org.Eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.Java:113)
    at org.Eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding.resolve(UnresolvedReferenceBinding.Java:49)
    at org.Eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.Java:122)
    at org.Eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.resolve(ParameterizedTypeBinding.Java:861)
    at org.Eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.Java:100)
    at org.Eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveTypeFor(BinaryTypeBinding.Java:1081)
    at org.Eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.getField(BinaryTypeBinding.Java:869)
    at org.Eclipse.jdt.internal.compiler.lookup.Scope.findField(Scope.Java:1052)
    at org.Eclipse.jdt.internal.compiler.lookup.Scope.findField(Scope.Java:998)
    at org.Eclipse.jdt.internal.compiler.lookup.MethodScope.findField(MethodScope.Java:354)
    at org.Eclipse.jdt.internal.compiler.lookup.BlockScope.getBinding(BlockScope.Java:474)
    at org.Eclipse.jdt.internal.compiler.ast.QualifiedNameReference.resolveType(QualifiedNameReference.Java:930)
    at org.Eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(MessageSend.Java:374)
    at org.Eclipse.jdt.internal.compiler.ast.Expression.resolve(Expression.Java:947)
    at org.Eclipse.jdt.internal.compiler.ast.Block.resolve(Block.Java:90)
    at org.Eclipse.jdt.internal.compiler.ast.IfStatement.resolve(IfStatement.Java:260)
    at org.Eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.Java:456)
    at org.Eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.Java:252)
    at org.Eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.Java:415)
    at org.Eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.Java:1148)
    at org.Eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.Java:1258)
    at org.Eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.Java:539)
    at org.Eclipse.jdt.internal.compiler.Compiler.process(Compiler.Java:763)
    at org.Eclipse.jdt.internal.compiler.Compiler.compile(Compiler.Java:468)
    at org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.Java:405)
    at org.drools.compiler.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.Java:49)
    at org.drools.compiler.rule.builder.dialect.Java.JavaDialect.compileAll(JavaDialect.Java:405)
    at org.drools.compiler.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.Java:46)
    at org.drools.compiler.compiler.PackageRegistry.compileAll(PackageRegistry.Java:110)
    at org.drools.compiler.compiler.PackageBuilder.compileAll(PackageBuilder.Java:1334)
    at org.drools.compiler.compiler.PackageBuilder.compileAllRules(PackageBuilder.Java:975)
    at org.drools.compiler.compiler.CompositeKnowledgeBuilderImpl.buildRules(CompositeKnowledgeBuilderImpl.Java:266)
    at org.drools.compiler.compiler.CompositeKnowledgeBuilderImpl.buildPackages(CompositeKnowledgeBuilderImpl.Java:98)
    at org.drools.compiler.compiler.CompositeKnowledgeBuilderImpl.build(CompositeKnowledgeBuilderImpl.Java:87)
    at org.drools.compiler.kie.builder.impl.AbstractKieModule.buildKnowledgePackages(AbstractKieModule.Java:204)
    at org.drools.compiler.kie.builder.impl.AbstractKieProject.verify(AbstractKieProject.Java:39)
    at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildKieProject(KieBuilderImpl.Java:323)
    at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildAll(KieBuilderImpl.Java:193)
    at com.local.lds.rules.RuleEngineManager.buildKieBase(RuleEngineManager.Java:90)
    at com.local.lds.rules.LocmChiTest.<clinit>(LocmChiTest.Java:33)
    ... 41 more
Caused by: org.Eclipse.jdt.internal.compiler.classfmt.ClassFormatException
    at org.Eclipse.jdt.internal.compiler.classfmt.ClassFileReader.<init>(ClassFileReader.Java:372)
    at org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler$2.createNameEnvironmentAnswer(EclipseJavaCompiler.Java:303)
    at org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.Java:274)
    ... 82 more
testNoFeatured(com.local.lds.rules.LocmChiTest)  Time elapsed: 0 sec  <<< ERROR!
Java.lang.NoClassDefFoundError: Could not initialize class com.local.lds.rules.LocmChiTest
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.Java:62)
    at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.Java:45)
    at Java.lang.reflect.Constructor.newInstance(Constructor.Java:408)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java:187)
    at Sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invoke(MethodReflection.Java:63)
    at mockit.Invocation.proceed(Invocation.Java:192)
    at mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator.createTest(BlockJUnit4ClassRunnerDecorator.Java:59)
    at Sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invokeWithCheckedThrows(MethodReflection.Java:95)
    at mockit.internal.mockups.MockMethodBridge.callMock(MockMethodBridge.Java:75)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.Java:41)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.Java:236)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:15)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.Java:233)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:300)
    at org.Apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.Java:53)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.Java:123)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.Java:104)
    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:483)
    at org.Apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.Java:164)
    at org.Apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.Java:110)
    at org.Apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.Java:175)
    at org.Apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.Java:107)
    at org.Apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.Java:68)
testFeaturedSort(com.local.lds.rules.LocmChiTest)  Time elapsed: 0.001 sec  <<< ERROR!
Java.lang.NoClassDefFoundError: Could not initialize class com.local.lds.rules.LocmChiTest
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at Sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.Java:62)
    at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.Java:45)
    at Java.lang.reflect.Constructor.newInstance(Constructor.Java:408)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java:187)
    at Sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invoke(MethodReflection.Java:63)
    at mockit.Invocation.proceed(Invocation.Java:192)
    at mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator.createTest(BlockJUnit4ClassRunnerDecorator.Java:59)
    at Sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:483)
    at mockit.internal.util.MethodReflection.invokeWithCheckedThrows(MethodReflection.Java:95)
    at mockit.internal.mockups.MockMethodBridge.callMock(MockMethodBridge.Java:75)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.Java:41)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.Java)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.Java:236)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:15)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.Java:233)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:300)
    at org.Apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.Java:53)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.Java:123)
    at org.Apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.Java:104)
    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:483)
    at org.Apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.Java:164)
    at org.Apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.Java:110)
    at org.Apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.Java:175)
    at org.Apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.Java:107)
    at org.Apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.Java:68)

Alors, est-ce un problème avec Java 8, Maven, le plugin Surefire ou Drools?

18
dhalsim2

Cela ressemble à un bogue dans Drools (dans le compilateur Eclipse qui est le comportement par défaut):

Ils cherchent à le réparer dans 6.1.0.Beta2.

En attendant, essayez cette solution workaround: Remplacez la dépendance ecj par la version 4.3.1.

16
Geoffrey De Smet

J'ai résolu ce problème en modifiant pom in maven. 

    <dependency>
        <groupId>org.Eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.5.1</version>
    </dependency>

    <!-- core -->
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>5.5.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>5.5.0.Final</version>
        <exclusions>
            <exclusion>
                <groupId>org.Eclipse.jdt.core.compiler</groupId>
                <artifactId>ecj</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
2
breezily

ECJ, qui est utilisé pour compiler le code Java dans vos règles, n’a commencé à prendre en charge la syntaxe Java 8 que récemment avec Luna (4.4); une mise à jour est donc nécessaire. 

Si vous souhaitez utiliser la syntaxe Java 8 dans vos règles, vous voudrez également utiliser Drools 6.4.0.Final, car ils reconnaissent maintenant Java 8 en tant que Java 8 (ils mappaient 8 sur 7 pour résoudre les problèmes liés à ECJ 4.3) 

La version MVEL 2.2.X ou ultérieure, qui constitue une alternative abrégée à la syntaxe Java, prend également en charge Java 8.

0
Mr. Bix

Mettez à jour org.Eclipse.jdt.core.jar à la dernière version disponible.

Dans notre cas, nous avons mis à jour org.Eclipse.jdt.core-3.5.1.v_972_R35x.jar à org.Eclipse.jdt.core_3.11.2.v20160128-0629.jar.

C'est le jar qui décide de la version Java avec laquelle le code va être compilé.

une fois votre vieux code compatible Java-6, il est compilé en Java -8. Vous pouvez le faire en utilisant Java -8 sans une telle erreur. Ce sont des étapes éprouvées . S'il vous plaît revenir en arrière en cas de doute.

0
tanmaya pati

Mettez à jour votre org.Eclipse.jdt.internal.compiler (ecj.jar) vers la dernière version, utilisez ecj-4.4.jar et essayez.

0
Sandeep N