简体   繁体   中英

Unable to get javax.mail to be found by package

I've written a program that uses log4j and it set to use the SMTPAppender at Error/Fatal level

   <SMTP name="ErrorMail" subject="iMCtoOVO ERROR" to="addresses@company.com" from="server@client.com" smtpHost="clientgateway.client.com" smtpPort="25" bufferSize="100">
    <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
    </SMTP>

It was working fine with standard Oracle Java, but since switching to OpenJDK, due to licensing, installed with the Microsoft installer, I have not been able to get it to work.

2022-09-15 14:52:15,568 main ERROR Could not create plugin of type class org.apacche.logging.log4j.core.appender.SmtpAppender for element SMTP: java.lang.NoClassDefFoundError: javax/mail/MessagingException java.lang.NoClassDefFoundError: javax/mail/MessagingException

Under Oracle Java, I had only been able to get this to work by putting the javax.mail.jar in the "ext" folder. I can find no such location now on this system. I have tried putting the path in the manifest Class-Path, removing Class-Path from the Manifest and specifying the location by java -cp (see below in case I've lost my ever loving mind) in various formats (eg quoted, \ and / absolute and relative path, wild card etc), I have rebuilt the runnable JAR with all necessary packages built in (Eclipse IDE) and I am out of ideas.

java -cp G:\iMCAlarmParse\lib\javax.mail.jar -Dlog4j.configurationFile=file:/g:/iMCAlarmParse/conf/log4j2-dev.xml -Dimctoovo.WorkPath=g:/imcAlarmParse/ -jar iMCAlarmParse-dev.jar 

I'm sure it's something amazingly dumb I'm missing in the stack trace but I'm at my wits end.

For reference: openjdk 17.0.4.1 2022-08-12 LTS OpenJDK Runtime Environment Microsoft-40354 (build 17.0.4.1+1-LTS) OpenJDK 64-Bit Server VM Microsoft-40354 (build 17.0.4.1+1-LTS, mixed mode, sharing)

Thanks in advance.

pom.xml

<?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>org.example</groupId>
    <artifactId>DemoAppLogs</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.36</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>

</dependencies>
    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--
                            <classpathPrefix>libs/</classpathPrefix>
                            -->
                            <mainClass>
                                org.example.Main
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}
                            </outputDirectory>
                            <!--
                            <outputDirectory>
                                ${project.build.directory}/libs
                            </outputDirectory>
                            -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

package

run mvn clean package

test run

copy your log4j2.xml to target

cd target

java -jar app.jar

target directory list

  • activation-1.1.jar <-- mail jar
  • javax.mail-1.6.2.jar <-- mail jar
  • log4j-api-2.17.2.jar
  • log4j-core-2.17.2.jar
  • log4j-slf4j-impl-2.17.2.jar
  • slf4j-api-1.7.36.jar
  • log4j2.xml <-- Your log4j config
  • app.jar <-- Your App

app.jar/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.1
Built-By: demo
Build-Jdk: 19.0.1
Class-Path: log4j-api-2.17.2.jar log4j-core-2.17.2.jar log4j-slf4j-impl-
 2.17.2.jar slf4j-api-1.7.36.jar javax.mail-1.6.2.jar activation-1.1.jar
Main-Class: org.example.Main
  1. I am not use -cp assign jar file, because I set classpath in app.jar/META-INF/MANIFEST.MF (pom.xml use maven-jar-plugin)

  2. I put all dependency jar into target with app.jar (pom.xml use maven-dependency-plugin)

Probably you are adding an outdated dependency, try

javax.mail:javax.mail-api:1.6.2

instead. You could use https://mvnrepository.com/artifact/javax.mail/javax.mail-api to get the binary, search for different versions, or try to switch to its successor jakarta.mail-api instead. (But this requires updates to the software as the package names changed.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM