0

My program is a java program running on java 11.0.11 using maven. I use JAF and JavaMail API in order to send an Email in my code after a button has been pressed. When I run the code using VScode it works alright but when I compile to jar it with the mvn package and then run it with java - jar recipt_generator-1.jar and press the button that is supposed to send the Email it throws the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/mail/Authenticator
        at com.recipt.EndMenu$3.actionPerformed(EndMenu.java:70)
        at java.desktop/java.awt.Button.processActionEvent(Button.java:411)
        at java.desktop/java.awt.Button.processEvent(Button.java:379)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5011)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 22 more

I tried changing maven compiler versions, adding dependencies for javax.mail.jar and activation.jar and checked every jar I use is in the classpath environment variable. I ask for your help because I'm really confused - The program sends the Email perfectly using VScode run option, but when compiling the error keeps showing. I'll appriciate any help.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Liad Or
  • 1
  • 1

2 Answers2

0

JavaMail is not part of the Java API. It compiles with Maven since you probably have it as dependency in your POM and such it's in Maven's build classpath (and most probably in VSCode's run classpath too – I'm not familiar with that).

Running it on the command line with just java - jar recipt_generator-1.jar is not enough. You have to add one if the following with JavaMail's JAR (excerpt from java --help):

   -cp <class search path of directories and zip/jar files>
   -classpath <class search path of directories and zip/jar files>
   --class-path <class search path of directories and zip/jar files>
                 A ; separated list of directories, JAR archives,
                 and ZIP archives to search for class files.

To create a Fat JAR that contains the dependencies of your JAR see:

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
0

Thanks For the help, I managed to figure out that I missed the plugin that is supposed to generate the fat jar. The Second Example is that plugin. Note that it worked for me only when attaching the regular jar plugin, which is the First Example.

First:

      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.receiptgen.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

Second:

plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>

        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.receiptgen.Main</mainClass>
            </manifest>
          </archive>
        </configuration>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>

      </plugin>
Liad Or
  • 1
  • 1