I have created a simple project in java using eclipse ide to test the log4j functionality.
I need to run the application from the command prompt using the jar tool in java.
The logtest.java file is simple java file which defines a logger as below.
It is referencing a Logger which is in another Jar file. below is directory structure.
package com;
import org.apache.log4j.Logger;
public class logtest {
private static Logger logger = Logger.getLogger(logtest.class);
public static void main(String[] args) {
System.out.println("---------------HIIIIII-----------");
}
Below is the directory structure that i have created in the project in eclipse.
src/com/logtest.java
lib/log4j-1.2.17.jar
the manifest files entries are as below and i have manually created the maifest file.
Manifest-Version: 1.0
Main-Class: com.logtest
Class-Path: lib/log4j-1.2.17.jar
The contents of the Jar file are as below which i unzipped using the jar utility.
META-INF/MANIFEST.MF
.project
com/logtest.class
manifest.mf
lib/log4j-1.2.17.jar
.classpath
I have exported the jar file and when i try to run using the commond java -jar JarTest2.jar i am getting the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.logtest.<clinit>(logtest.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I know its a class not found exception but strange thing is that the same jar when i create using jdk1.5 and run command tool the jar runs successfully and the java class executes without any error.
I have no idea why this is going as everything is the same.
mine is a 64 bit windows 7 machine and the other machine where it is running is a 32 bit XP machine.
Of course the version of eclipse ide is different on both machines.
Can you please let me know is there something wrong about what i am doing or am i missing something else.
Thanks
Vikeng21
I’m pretty sure that Java doesn’t support embedded Jars. I’d recommend moving the lib/log4j-1.2.17.jar file out to the file system (so that it creates a sub folder ./lib within the same drctory as your executable Jar & see if that makes a difference.
If you really want to use embedded Jars, you could take a read of One-Jar
You should have a directory structure of
On your file system. The
log4jJar shouldn’t be imbedded (contained within) theJarTest2.jarfile