0

I created a java application program to read from Gmail address. When I execute the script from IDE (Intellij) the script runs perfectly. But by generating a jar file and trying to execute the jar:

java -jar my-jar-file.jar

I have this error:

java.lang.NoClassDefFoundError: javax/mail/Part
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: javax.mail.Part
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
Exception in thread "main" 

This is my POM File:

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>my.group.id</groupId>
<artifactId>readMail</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>my.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
   ....
</properties>
<dependencies>
    ....
    <!-- POP, SMTP, IMAP -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mailapi</artifactId>
        <version>1.4.3</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1</version>
    </dependency>
    ....
</dependencies>

medkhelifi
  • 1,071
  • 3
  • 17
  • 35
  • If you use -jar and you aren't creating an über-jar you can't specify a class path. Do you have a manifest file with a class path? – Dave Newton Jul 19 '17 at 13:48
  • @DaveNewton I don't know what is a manifest file. I guess I'm not specifying it. – medkhelifi Jul 19 '17 at 13:50
  • You are, that's what defines the main class. My point is that you're running it without surviving the dependencies. There's a Maven exec which will work, or you need to include the dependencies in the jar. – Dave Newton Jul 19 '17 at 13:52
  • @DaveNewton how to include all dependencies in my executable jar ?? – medkhelifi Jul 19 '17 at 13:54
  • In two of the answers, though there are other ways, maybe start there. – Dave Newton Jul 19 '17 at 13:56
  • Question marked as duplicate is not a duplicate of this question. It's a duplicate, but not the one listed. – Dave Newton Jul 19 '17 at 13:58
  • Delete the dependency on `mailapi` as [it is only good for compiling against](https://stackoverflow.com/questions/16807758/java-lang-noclassdeffounderror-com-sun-mail-util-maillogger-for-junit-test-case/28935760?s=1|8.0825#28935760). – jmehrens Jul 19 '17 at 15:05
  • 1
    @DaveNewton I found my Answer in this post, can you add the link in an answer to this post so we can make this one as solved, thank you. https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – medkhelifi Jul 19 '17 at 15:31
  • It's already closed-just leaving the link is sufficient. I can't re-open it on my own. – Dave Newton Jul 19 '17 at 16:54

2 Answers2

0

You have to create a fat jar with the Maven Shade Plugin , that include all dependencies tree.

atrujillofalcon
  • 1,042
  • 1
  • 11
  • 17
-1

https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

try compiling it as a fat jar that embedded the dependency into the jar this might work I am not sure

Hamuel
  • 633
  • 5
  • 16