Reputation: 63
I have an issue with maven. I wrote a java code which is running in my idea. Then i use mvn package in order to build a jar. Still no problems. But for somes reasons i don't understand when i try to execute the jar maven have created i have this error :
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)
Can someone help a newbie like me?
Here is the beginning of java code:
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é");
}
And then here is the 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>
If you need more informations be free to ask. Thanks all.
Upvotes: 6
Views: 15928
Reputation: 3471
You have to include the interested dependencies in your jar. Take a look to this Including dependencies in a jar with Maven or this How can I create an executable JAR with dependencies using Maven?
Upvotes: 1