Skip to content
Advertisement

Optaplanner – NullPointerException when creating jar file

My program works fine from my IDE (IntelliJ) but for some reason, when I try to create a jar file I get following error when I run the program from a terminal:

Exception in thread “main” java.lang.NullPointerException at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:461)

org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:331)

org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:220)

org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:57)

org.optaplanner.EmployeeRoster.main(EmployeeRoster.java:31)

This is my line 31 in EmployeeRoster:

JavaScript

SOLVER_CONFIG_XML is a String containing my path for my XML solver-config, it looks like this

JavaScript

Also here’s my pom.xml file if that should be relevant:

JavaScript

What might I be doing wrong?

Advertisement

Answer

For me, the issue had to deal with how I wanted to run the jar (java -jar), and consequently how I built the jar. The NullPointerException arose when I upgraded to optaplanner-core/7.4.1 from 6.4.0. This issue was not present back when I was still using 6.4.0.

Exception:

java.lang.NullPointerException at org.kie.internal.io.ResourceFactory.newByteArrayResource(ResourceFactory.java:66) at org.drools.compiler.kie.builder.impl.AbstractKieModule.getResource(AbstractKieModule.java:299) at org.drools.compiler.kie.builder.impl.AbstractKieModule.addResourceToCompiler(AbstractKieModule.java:264) at org.drools.compiler.kie.builder.impl.AbstractKieModule.addResourceToCompiler(AbstractKieModule.java:259) at org.drools.compiler.kie.builder.impl.AbstractKieProject.buildKnowledgePackages(AbstractKieProject.java:243) at org.drools.compiler.kie.builder.impl.AbstractKieProject.verify(AbstractKieProject.java:74) at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildKieProject(KieBuilderImpl.java:250) at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildAll(KieBuilderImpl.java:218) at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildAll(KieBuilderImpl.java:176) at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:503)

The following is a temporary workaround I did to resolve the NullPointerException and just get the application to run.

  1. Use IntelliJ’s gradle to build one big jar with all dependencies.
  2. Unzip the jar, for example, to unzippedJar/ directory.
  3. Modify unzippedJar/META-INF/kie.conf
  4. Rejar the files.
  5. Run the jar with java.

Step 1. Building the jar

JavaScript

Step 2. Unzip

unzip fatJar.jar -d unzippedJar/

Step 3. Modify kie.conf

The “fatJar” task flattened the dependencies – duplicate file names are allowed in the jar. This resulted in four unzippedJar/META-INF/kie.conf files, only one of which was used. Regardless of whichever kie.conf file was used, this was my final kie.conf.

JavaScript

Step 4. Rejar

For whatever reason, specifying the MANIFEST.MF file did nothing for me, hence I left it out.

jar cf rejard.jar .

Step 5. Run the jar

java -cp rejard.jar path.to.class.with.main.method

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement