1

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.

j1h00
  • 11
  • 2
  • How do you run this? – Johannes Kuhn Apr 20 '22 at 12:19
  • I used `mvn package` command to build .jar file && used following command to run javaagent jar file => `java -javaagent:target/jvm-monitoring-agent-1.0 SNAPSHOT.jar -jar test/MultithreadExample.jar` // thanks for comment! –  j1h00 Apr 20 '22 at 12:38
  • Dependencies are not automatically added from the manifest - except when you run that jar with `java -jar`. You may need to use the Instrumentation instance to add jars to the system class loader. – Johannes Kuhn Apr 20 '22 at 14:04
  • Thanks for your answer. I'll try instrumentation :) –  j1h00 Apr 20 '22 at 14:51
  • I hope you were able to solve your problem with Johannes' hints. Otherwise, I can take a look at your minimal sample project reproducing the problem, ideally on GitHub. – kriegaex Apr 21 '22 at 02:31
  • Hi kriegaex, I figured it out! I tried Instrumentation API, refer to [Adding jar file to instrumentation path](https://stackoverflow.com/questions/38213651/adding-jar-file-to-instrumentation-path). Thank you! –  j1h00 Apr 21 '22 at 08:15
  • 1
    Exactly. If you want those libraries to be contained in the agent JAR, you either need to shade (flatten) and optionally relocate their package names in order to avoid the system path thingy, or to unpack the nested JARs to a temp dir and from there add them to the path like you just did. What is not possible is to add nested JARs to the path directly, because the JVM expects a file system path. `JarFile` needs a `File` for construction, you cannot use NIO virtual paths or so, which IMO is a design flaw in the JDK. – kriegaex Apr 21 '22 at 11:40
  • Wow, the fact that I just got into java programming makes me hard to understand all the information you've given, but I'll try to find out. Thank you so much kriegaex! –  j1h00 Apr 22 '22 at 08:44

0 Answers0