'NoClassDefFoundError' error of java program

637 views
Skip to first unread message

Ryan Wang

unread,
Aug 23, 2020, 9:18:05 PM8/23/20
to or-tools-discuss

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

Ryan Wang

unread,
Aug 26, 2020, 9:28:59 PM8/26/20
to or-tools-discuss
Done, in pom.xml, add plugin onejar-maven-plugin , to package local dependencies into jar file
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
Reply all
Reply to author
Forward
0 new messages