简体   繁体   English

无法通过 package 找到 javax.mail

[英]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我编写了一个使用 log4j 的程序,它设置为在错误/致命级别使用 SMTPAppender

   <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.它在标准 Oracle Java 上运行良好,但自从切换到 OpenJDK 后,由于许可,使用 Microsoft 安装程序安装,我无法让它运行。

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.在 Oracle Java 下,我只能通过将 javax.mail.jar 放入“ext”文件夹中才能使其正常工作。 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 以各种格式(例如引用,\和/绝对和相对路径、通配符等),我已经用内置的所有必要包(Eclipse IDE)重建了可运行的 JAR,但我没有想法。

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)供参考: 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 , 分享)

Thanks in advance.提前致谢。

pom.xml 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 package

run mvn clean package运行mvn clean package

test run测试运行

copy your log4j2.xml to target将您的 log4j2.xml 复制到目标

cd target

java -jar app.jar

target directory list目标目录列表

  • activation-1.1.jar <-- mail jar activation-1.1.jar <-- 邮件 jar
  • javax.mail-1.6.2.jar <-- mail jar javax.mail-1.6.2.jar <-- 邮件 jar
  • log4j-api-2.17.2.jar log4j-api-2.17.2.jar
  • log4j-core-2.17.2.jar log4j-核心-2.17.2.jar
  • log4j-slf4j-impl-2.17.2.jar log4j-slf4j-impl-2.17.2.jar
  • slf4j-api-1.7.36.jar slf4j-api-1.7.36.jar
  • log4j2.xml <-- Your log4j config log4j2.xml <-- 你的 log4j 配置
  • app.jar <-- Your App app.jar <-- 你的应用

app.jar/META-INF/MANIFEST.MF 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)我没有使用 -cp 分配 jar 文件,因为我在 app.jar/META-INF/MANIFEST.MF 中设置了类路径(pom.xml 使用 maven-jar-plugin)

  2. I put all dependency jar into target with app.jar (pom.xml use maven-dependency-plugin)我将所有依赖项 jar 放入带有 app.jar 的目标中(pom.xml 使用 maven-dependency-plugin)

Probably you are adding an outdated dependency, try可能您正在添加过时的依赖项,请尝试

javax.mail:javax.mail-api:1.6.2 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.您可以使用https://mvnrepository.com/artifact/javax.mail/javax.mail-api来获取二进制文件,搜索不同的版本,或者尝试切换到它的后继者 jakarta.mail-api。 (But this requires updates to the software as the package names changed.) (但这需要更新软件,因为 package 名称已更改。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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