How to enable tools.jar in uberjar (using maven-shade-plugin)?


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">



mvn install

succeeds (i.e. maven sees the dependency tools.jar

at build time), but on execution I have

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/api/JavacScope
    at test.Test.main(
Caused by: java.lang.ClassNotFoundException:



source to share

2 answers

The maven-shade plugin assumes system dependencies and does not actually include them in a fat jar

A quick dirty workaround is to install tools.jar in your maven repository and reference it as a compile-dependent dependency

The best workaround is to use maven-assembly-plugin and include tools.jar as<file>



Most of the dependencies are <scope>system</scope>

not available for plugins.

Also, including the tools.jar in your uberjar doesn't carry over badly in newer JVM versions. This is fine if you have a super controlled and limited deployment environment, but it is usually best to discover tools.jar and use it.

Here's an example from the maven-javadoc-plugin (it looks at tools.jar and then checks that the active classloader has classes from tools.jar)

In your case, instead of searching,

you can search

to check for a valid environment.

From there it would be trivial to create a new ClassLoader with your uberjar + tools.jar and then execute what you need from the valid scope Thread.currentThread().setContextClassLoader()



All Articles