I have created jar file which contains dependent jar but when I try to run the class file inside that it gives error as
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.TestFlowProcessor.<clinit>(WebMethodsFlowProcessor.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
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)
My Manifest file :
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: test
Build-Jdk: 1.8.0_45
Class-Path: log4j-1.2.17.jar slf4j-api-1.7.10.jar commons-logging-1.1.jar dom4j-1.6.1.jar jsoup-1.8.3.jar junit-3.8.1.jar log4j-1.2.17.jar poi-3.8-20120326.jar poi-ooxml-3.8-20120326.jar poi-ooxml-schemas-3.8-20120326.jar SAPIntegrationDirectory.jar stax-api-1.0.1.jar xmlbeans-2.3.0.jar
Main-Class: com.TestFlowProcessor
I am trying to execute using
java -cp WMTOFuse.jar com.TestFlowProcessor
You could use 'Ant' 'jar' task for this. Refer https://ant.apache.org/manual/Tasks/jar.html
<jar destfile="build/main/checksites.jar">
<fileset dir="build/main/classes"/>
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="lib/main" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Main-Class"
value="com.acme.checksites.Main"/>
</manifest>
</jar>
above creates a single jar file containing inside it contents of multiple jars.
The problem is seems to be with log4j-1.2.17.jar. I have changed it to
log4j-1.2.15.jar & above mentioned issue is resolved. Thanks everyone
for helping me out with this.
Related
This question already has an answer here:
How to include a .jar dependency into the ANT target that generate my final .jar file of my application?
(1 answer)
Closed 8 years ago.
I am pretty new in Ant and I have the following problem trying to create a build.xml file to compile a single class (that contains the main() method) command line application.
So this is the code of the Main class (at this time it is the only class in the application):
import java.sql.*;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
System.out.println(args.length);
if(args.length != 0) {
String partitaIVA = args[0];
String nomePDF = args[1];
}
Connection conn = null;
Statement stmt = null;
try {
Class.forName ("oracle.jdbc.OracleDriver");
TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT+2");
TimeZone.setDefault(timeZone);
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB driver
System.out.println("After obtained connection with DB");
} catch(SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
As you can see the behavior of the main() method is very simple, at this time only obtain a connection with an Oracle database (represented by the Connection conn object). Running it into the IDE (I am using IntelliJ) it works fine (I can see it using the debugger, the **Connection conn is correctly set).
Now I am working on the following build.xml file for the Ant compilation:
<project name="edi-sta">
<description>
EDI-STA
</description>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Main.jar" fork="true"/>
</target>
</project>
After that I have performed in order the clean, compile and jar targets I tried to open the console, access to the build/jar/ directory that contains the Main.jar file and I try to execute it performing the following statement:
C:\Projects\edi-sta\build\jar>java -jar Main.jar
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Unknown Source)
C:\Projects\edi-sta\build\jar>
But, as you can see, now happens a very strange thing, it seems that can't found the class that contains the Oracle driver (oracle.jdbc.OracleDriver) so the ClassNotFoundException is thrown.
I think that this happens because if I open (with WinZip) my generated Main.jar file it only contains the Main.class file and the META-INF folder (that contains only the MANIFEST.MF file) but I have not the ojdbc6.jar file that contains the Oracle driver that I use.
So my question is: what have I to do to include this ojdbc6.jar dependency properly in my generated Main.jar file and avoid the ClassNotFoundException?
Tnx
You can define jar dependencies in the Class-Path attribute of the manifest file of the jar. Read the documentation here - http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
To achieve that from your ant task, use the Class-Path attribute as below
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
<attribute name="Class-Path" value="your-jar-file"/>
....
I used the maven-assembly-plugin to put all my dependencies into a lib folder - I can confirm that the required dependency jar is inside the lib folder.
I am using maven-jar-plugin with the following snippet to link with the dependencies in the lib folder:
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>my.main.class.MainClass</mainClass>
</manifest>
</archive>
</configuration>
I used jd-gui.exe to decompile my jar and I can see that the MANIFEST.MF file contains my dependency as lib/MyLibFile.jar.
However, when I try to run my app in a Linux environment like so:
java -cp MyApp.jar my.class.app.MainClass ...
I get NoClassDefFoundError. However, if I do the following:
java -cp MyApp.jar:lib/* my.class.app.MainClass ...
The app runs.
Could someone please point why MyApp.jar cannot read the dependent library file during runtime?
Update:
Tried with java -jar MyApp.jar and this is also not working. I believe something is wrong with my MANIFEST.MF but I cannot find the issue.
EDIT:
The following is my simplified MANIFEST.MF -
Manifest-Version: 1.0
Built-By: dev
Build-Jdk: 1.7.0_25
Class-Path: lib/LibA.jar lib/LibB.jar lib/MyLibFile.jar
Created-By: Apache Maven
Main-Class: my.main.class.MainClass
Archiver-Version: Plexus Archiver
Project directory structure:
lib/
config/
MyApp.jar
Content of lib directory:
LibA.jar
LibB.jar
MyLibFile.jar
My dependency is in the MyLibFile.jar.
I run my app by first cd into my project directory, then from there I execute java -jar MyApp.jar
This is the stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: dependency/lib/path/ClassName
at my.class.path.MyApp.main(MyApp.java:65)
Caused by: java.lang.ClassNotFoundException: dependency.lib.path.ClassName
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
java -cp MyApp.jar my.class.app.MainClass
The above command doesn't care about the manifest of the jar file. It explicitely sets the classpath to MyApp.jar only, and asks to run the class my.class.app.MainClass instead of the main class set in the manifest.
To execute an executable jar file (i.e. the main class setin the manifest, with all the dependencies listed in the manifest in the classpath), you must use the -jar option instead of the -cp option:
java -jar MyApp.jar
This question already has answers here:
java.lang.NoClassDefFoundError: org/apache/xpath/XPathAPI
(2 answers)
Closed 3 years ago.
I have upgraded my J2EE web application from jdk6,tomcat6 to jdk7 and tomcat7
but while deploying teamcity is giving following error.
[xmltask] java.lang.NoClassDefFoundError: org/apache/xpath/XPathAPI
java.lang.NoClassDefFoundError: org/apache/xpath/XPathAPI
at com.oopsconsultancy.xmltask.jdk14.XPathAnalyser14.analyse(XPathAnalyser14.java:29)
at com.oopsconsultancy.xmltask.XmlReplace.apply(XmlReplace.java:72)
at com.oopsconsultancy.xmltask.XmlReplacement.apply(XmlReplacement.java:61)
at com.oopsconsultancy.xmltask.ant.XmlTask.processDoc(XmlTask.java:707)
at com.oopsconsultancy.xmltask.ant.XmlTask.execute(XmlTask.java:676)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: java.lang.ClassNotFoundException: org.apache.xpath.XPathAPI
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more
The missing class is contained in xalan-2.7.0 (See Maven central). So I think the problem is how your classpath has been set.
Considering that this appears to be an error reported by the xmltask task, does this mean the error is being thrown by ANT?
Google found the following example which might be the answer to your problems:
https://wiki.nci.nih.gov/display/NBIA/Migration+to+Java+7
Add the xalan jar to the classpath of your taskdef:
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
<classpath>
<pathelement path="${common.lib.dir}/xmltask-1.15.1.jar" />
<pathelement path="${common.lib.dir}/xalan-2.7.1.jar"/>
</classpath>
</taskdef>
if using maven add this to your dependencies section:
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.0</version>
</dependency>
I am not sure but, i guess jar named xalan-2.4.0.jar is missing
please download it and place inside.
http://www.java2s.com/Code/Jar/x/Downloadxalan240jar.htm
Add xpath to your classpath and try the deploy again. If you are not sure which xpath jar you need search in the tomcat libraries folder for xpathapi jar
Xpathapi is a jar used for evaluating xpaths (related to xml) in java. if you are not sure which version you need, google for it and fetch the latest xpath api jar .
For now I just remove
from my build.xml, and its working.
I know its not a solution but we have to deliver.
In Build.xml, change the java version to 1.7 and classpath XMLTask from 1.15.1 to 1.16.1.. It works
This is how I solved this, inlcuding Xalan and Serializer
Download xalan-j2-2.7.0.jar & serializer-2.7.0.jar
Update build.xml to include these jars in path
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
<classpath>
<pathelement location="../xmltask-v1.14.jar"/>
<pathelement path="../xalan-j2-2.7.0.jar"/>
<pathelement path="../serializer-2.7.0.jar"/>
</classpath>
</taskdef>
This should get it running.
You should remove import org.apache.xpath.operations.String;.
Here's the error I keep getting at runtime:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
Note, this is a runtime error, not a compile-time one. Both tasks in my build.xml have an identical classpath set, and the compile task runs fine every single time:
<path id="classpath">
<fileset dir="lib" includes="*.jar" />
</path>
<target name="compile">
<mkdir dir="build/classes"/>
<javac
srcdir="src"
classpathref="classpath"
includeantruntime="false"
destdir="build/classes"
/>
</target>
...
<target name="run" depends="clean,compile,jar">
<java
jar="build/jar/${project.name}.jar"
fork="true"
classpathref="classpath"
>
<sysproperty key="java.library.path" path="${path.lib}/windows"/>
</java>
</target>
Trying to run the jar via command-line manually yields the same result:
java -cp .:lib/*.jar -Djava.library.path=lib/windows -jar build/jar/JUtopia.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
Note that the library jarfile is ok:
bash-3.1$ jar -tf lib/lwjgl.jar | grep LWJGLException
org/lwjgl/LWJGLException.class
And the native libraries are in place:
bash-3.1$ ls lib/windows/lwjgl.dll
lib/windows/lwjgl.dll
The question: where the blazes have I gone wrong? I've been beating at this problem for nearly 3 days. Any help would be much appreciated.
Full result stack:
clean:
[delete] Deleting directory C:\Users\mkumpan\Projects\JUtopia\build
compile:
[mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\classes
[javac] Compiling 12 source files to C:\Users\mkumpan\Projects\JUtopia\build\classes
jar:
[mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\jar
[jar] Building jar: C:\Users\mkumpan\Projects\JUtopia\build\jar\JUtopia.jar
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
[java] at JUtopia.<init>(Unknown Source)
[java] at JUtopia.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 2 more
P.S.: Note, I'm using Console2 with bash in a windows environment for my commandline work, thus the windows natives yet linux shell syntax. Using vanilla cmd to run the jar yields the same result.
-jar...
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. - reference
try setting the Class-Path in the JAR
Alternatively try running without the -Jar option, by specifying the main class on the command line
One of the possible causes is that while loading the class LWJGLException it also references another class which can't be found on the classpath. Hence the reported error is sometimes not clear.
Important here is thet you have this NoClassDefFoundError and not ClassNotFoundException which is the error you assume you are having: it cannot find the class LWHLException, yes it can ! But it cannot load it....
I have my application entities in a separate project from my main servlet, and they aren't being DataNucleus enhanced.
Not sure if I'm just breaking the rules or what, but setting the ORM setting on the project doesn't enhance my .class files. The way my workspace is built is by compiling the projects, then running an ant script builds jar files and copies them into the lib directory of my servlet.
I suppose that if I must, I can add some java tasks to my ant scripts to enhance my .class files. If that's the case, an example of the task would be helpful.
I do want to keep my projects are they are, let me know what I need to do to maintain that.
This is my build.xml of the project containing my entities:
<project default="default">
<property name="appengine.sdk.dir" location="C:\superlongpathtomyeclipseplugins\plugins\com.google.appengine.eclipse.sdkbundle_1.6.5\appengine-java-sdk-1.6.5"/>
<import file="${appengine.sdk.dir}/config/user/ant-macros.xml"/>
<target name="default" depends="dist"/>
<target name="dist">
<enhance>
<classpath>
<pathelement path="${appengine.sdk.home}/lib/*"/>
<pathelement path="bin"/>
</classpath>
<fileset dir="bin" includes="**/*.class" />
</enhance>
<jar basedir="bin" destfile="dist\sessionexample.model.jar"/>
</target>
</project>
But now here is the error I'm getting:
java.lang.RuntimeException: Unexpected exception
at com.google.appengine.tools.enhancer.Enhancer.execute(Enhancer.java:76)
at com.google.appengine.tools.enhancer.Enhance.(Enhance.java:71)
at com.google.appengine.tools.enhancer.Enhance.main(Enhance.java:51)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.appengine.tools.enhancer.Enhancer.execute(Enhancer.java:74)
... 2 more
Caused by: java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/Key
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getJavaBeanAccessorAnnotationsForClass(AbstractAnnotationReader.java:238)
at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getMetaDataForClass(AbstractAnnotationReader.java:128)
at org.datanucleus.metadata.annotations.AnnotationManagerImpl.getMetaDataForClass(AnnotationManagerImpl.java:136)
at org.datanucleus.metadata.MetaDataManager.loadAnnotationsForClass(MetaDataManager.java:2278)
at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:385)
at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:743)
at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:545)
at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1252)
... 7 more
Caused by: java.lang.ClassNotFoundException: com.google.appengine.api.datastore.Key
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 com.google.appengine.tools.enhancer.EnhancerLoader.loadClass(EnhancerLoader.java:107)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more
Do I need to keep adding things to my <classpath> until it works?
The DataNucleus project defines many ways to run enhancement. It's for you to choose which one makes most sense to your environment and build system. Any other methods not defined on that page are not supported (by us)
Finally got an ant task to run:
<project default="default">
<property name="appengine.sdk.dir" location="c:\pathtoeclipse\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.6.5\appengine-java-sdk-1.6.5"/>
<import file="${appengine.sdk.dir}/config/user/ant-macros.xml"/>
<target name="default" depends="dist"/>
<target name="dist">
<enhance>
<classpath>
<pathelement path="${appengine.sdk.home}/lib/*"/>
<pathelement path="${appengine.sdk.home}/lib/user/*"/>
<pathelement path="${appengine.sdk.home}/lib/user/orm/*"/>
<pathelement path="bin"/>
</classpath>
<fileset dir="bin" includes="**/*.class" />
</enhance>
<jar basedir="bin" destfile="dist\sessionexample.model.jar"/>
</target>
</project>