gpt4 book ai didi

java - ClassNotFoundException : org. apache.log4j.Logger

转载 作者:行者123 更新时间:2023-12-02 09:19:48 29 4
gpt4 key购买 nike

我对 Maven 有疑问。我写了一个java代码,它在我的想法中运行。然后我使用 mvn package 来构建一个 jar。仍然没有问题。但由于某些原因,我不明白当我尝试执行 maven 创建的 jar 时出现此错误:

 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at infra.Main.main(Main.java:53)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
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)

有人可以帮助像我这样的新手吗?

这是java代码的开头:

package infra;

import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

private static String companyName = "lolilol";

private static String baseUrl = "https://" + companyName + ".inspirecloud.net";

private static String loginApi = "/api/publish/Users/Login";

private static String loginUrl = baseUrl + loginApi;

private static String fetchBatchesApi = "/api/query/Messenger/ListBatchesQueryByUploadTime";

private static String fetchBatchesUrl = baseUrl + fetchBatchesApi;

private static String detailBatcheApi = "/api/query/Messenger/ReportQuery";

private static String detailBatcheUrl = baseUrl + detailBatcheApi;

private static Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.local.fr", 8080));

private static int nbEssais = 4;

private static String header = "nbClic;Email;Id;Name;Customer;CompanyName;LastUploadTime;Type;MessageType;EmailId;ToEmailAdr;SendindState;Delivered;DeliveryError;FirstView;LastView;ViewCount;Unsubscribe;ServiceUsed;CustomerId;UrlLandingPage;GMC1;GMC2;GMC3;GMC4;GMC5;\n";

private static Logger logger = Logger.getLogger(Main.class);

public static void main(String[] args) {


logger.info("Début des logs");

HttpsURLConnection loginCon = null;
HttpsURLConnection fetchBatchCon = null;
HttpsURLConnection detailBatchCon;

FileOutputStream fos = null;
Scanner ficIn = null;

int retry;

String email;
String password;
String type;
String from;
String to;


String cookie = null;
String lineIn;
String[] params;
ArrayList<String> messagesMail = new ArrayList<String>();


// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};

try
{
SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
logger.error("Problème sécurité");
}

然后这是 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>trackingGMC</groupId>
<artifactId>TrackingGMC</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>infra.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>


<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

</project>

如果您需要更多信息,请随时询问。谢谢大家。

最佳答案

您必须在 jar 中包含感兴趣的依赖项。看看这个Including dependencies in a jar with Maven或者这个How can I create an executable JAR with dependencies using Maven?

关于java - ClassNotFoundException : org. apache.log4j.Logger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825391/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com