I write a java program using or-tools, it runs normally in development(Intellij idea), but when I use maven to package the program into a jar, it will throw 'NoClassDefFoundError'
What did I do in Intellij idea:
1. Create a project in Maven
2. Download and extract binary distribution of Or-tools, with version v7.8.7959
3. Configure Intellij to add Or-tools library:
4. The main program, very simple:
public class App
{
static {
System.loadLibrary("jniortools");
}
public static void main( String[] args ) throws Exception {
CpModel model=new CpModel();
}
}
5. Run it, no error
What did I do to release the program:
1. Configure pom.xml, add local dependencies, and use 'maven-shade-plugin':
<dependencies>
...
<dependency>
<groupId>com.google.ortools</groupId>
<artifactId>ortools-java</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/or-tools_v7.8.7959 /lib/com.google.ortools.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/or-tools_v7.8.7959/lib/protobuf.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>wzy.jsp.simplest.demo.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<plugin>
</plugins>
...
</build>
2. Package the project: 'mvn clean package', get the final jar file
3. Execute the jar file , and specify the lib:' java -jar "-Djava.library.path=../src/or-tools_v7.8.7959/lib" JspAlgorithmService.jar'
4. Error occurs: "java.lang.NoClassDefFoundError: com/google/ortools/sat/CpModel"
So does anyone know how to deal with this issue? I think my pom.xml may have some mistakes or lack something, but I'm not be familiar with maven