`java.lang.NoClassDefFoundError` from javaagent built by `maven package`

321 Views Asked by At

I've been trying to build javaagent (containing premain()) to .jar file using maven mvn package, and keep getting java.lang.NoClassDefFoundError exception related to external dependencies. For details, I added kafka-client dependecies and maven-jar-plugin to pom.xml like below

                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib</classpathPrefix>
                            </manifest>
                            <manifestEntries>
                                <mode>development</mode>
                                <url>${project.url}</url>
                                <key>value</key>
                                <Premain-Class>com.my.java.monitoring.Agent</Premain-Class>
                                <Can-Redefine-Classes>true</Can-Redefine-Classes>
                                <Can-Retransform-Classes>true</Can-Retransform-Classes>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>

After reading How can I create an executable JAR with dependencies using Maven?, I tried using maven-assembly-plugin instead, like below

                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <Premain-Class>com.my.java.monitoring.Agent</Premain-Class>
                            </manifestEntries>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <!-- this is used for inheritance merges -->
                            <phase>package</phase>
                            <!-- append to the packaging phase. -->
                            <goals>
                                <goal>single</goal>
                                <!-- goals == mojos -->
                            </goals>
                        </execution>
                    </executions>
                </plugin>

but I get this error => Failed to find Premain-Class manifest attribute in target/my-java-agent.jar Error occurred during initialization of VM agent library failed to init: instrument

Can anyone who has experience building javaagent .jar file with dependencies, please help.

0

There are 0 best solutions below