0

I have created a maven project which needs tools.jar to be included. I have added the jar into the lib folder and included the following in the pom.xml.

<dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <scope>system</scope>
            <version>1.8.0</version>
            <systemPath>${project.basedir}\src\lib\tools.jar</systemPath>
</dependency>

When have also added the following plugin to create executable jar for the project.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>umlparser.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

I have built the jar using "mvn clean package assembly:single" This has created an executable jar file with name myproject-jar-with-dependencies.

But when I am trying to run the jar file with java -jar it is unable to get the tools.jar. I am getting the following error

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main
        at org.umlgraph.doclet.UmlGraph.main(UmlGraph.java:70)
        at umlparser.Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javadoc.Main
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

But when I am exporting it as runnable jar from eclipse it is working as expected. What is the issue with the maven package? Am I missing something here?

EDIT

I have tried to import the tools.jar dependency from the java_home instead of project path.

<dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.8.0_241</version>
            <scope>system</scope>
            <systemPath>${java.home}/../lib/tools.jar</systemPath>
        </dependency>

Still I am getting the same issue.

Note: The tools.jar is not used in my program. It is being used by one of the jars that I have included - "UMLGraph.jar".

K_N
  • 1
  • 3

1 Answers1

1

The com.sun:tools dependency which you have added has a system scope. Dependencies with provided and system scopes will not be includes in such packaged artifacts.

Specifically in this case, you should also actually not be doing so, because this jar comes as part of your JDK. The users of your jar will have a JDK/JRE to run the code and if you include tools jar you will most-likely be causing a conflict with their JDK/JRE and you really don't want to be doing that.

This being said, you could probably try using dependencySet with a scope of system, but I would really not advise this (even if it does the trick).

I'm assuming that on the system where you're running this jar, you simply don't have the JDK on your PATH and you have just the JRE instead. If you fix this, you should not have to do any of this unconventional hacking around.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Thank you for the response. I am running this on my local and it has JDK installed. The Java_Home path is also set in the environment variables correctly to JDK path. Even when I don't include the tools.jar in the dependencies I am getting the same issue. I have tried to include it from java_home but it didn't work out as well. ` com.sun tools 1.8.0_241 system ${java.home}/../lib/tools.jar ` – K_N Apr 24 '20 at 16:06
  • Also the tools.jar is not used in my program. It is being used by one of the jars that I have included - "UMLGraph.jar". – K_N Apr 24 '20 at 16:14