How to add thirdparty library in classpath via ant for java jar? -
i build code via ant , later try run main class.
java -cp my.jar mypackage.myclass
but have similar exception following:
exception in thread "main" java.lang.noclassdeffounderror: org/slf4j/loggerfactory @ org.apache.nutch.crawl.crawlerservice.<clinit>(crawlerservice.java:31) caused by: java.lang.classnotfoundexception: org.slf4j.loggerfactory @ java.net.urlclassloader$1.run(urlclassloader.java:366) @ java.net.urlclassloader$1.run(urlclassloader.java:355) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(urlclassloader.java:354) @ java.lang.classloader.loadclass(classloader.java:423) @ sun.misc.launcher$appclassloader.loadclass(launcher.java:308) @ java.lang.classloader.loadclass(classloader.java:356) ... 1 more
my main class:
import org.slf4j.logger; import org.slf4j.loggerfactory; public class myclass{ private static final logger log = loggerfactory.getlogger(myclass.class); public static void main(string[] args) throws exception { system.out.println("test"); } }
if don't use import org.slf4j.logger;
import org.slf4j.loggerfactory;
, no problem. if use, display exception.
this example. if import other libraries, display noclassdeffounderror
exception those.
my manifest.mf file:
manifest-version: 1.0 ant-version: apache ant 1.8.4 created-by: 1.7.0_17-b02 (oracle corporation) main-class: mypackage.myclass class-path: ./lib
how add libraries don't found in classpath via ant jar?
my problem solved. manifest file update follows:
manifest-version: 1.0 ant-version: apache ant 1.8.4 created-by: 1.7.0_17-b02 (oracle corporation) main-class: mypackage.myclass class-path: lib/slf4j.jar
if multiple jar added want if multiple jar wanted add, should done follows:
class-path: lib/slf4j.jar lib/another.jar
my fault follows: ./lib
or ./lib/*
or lib/*.jar
. understand it,all jars must written in manifest file .
ant script written similar to:
<target name="create_run_jar"> <jar destfile="/home/test/desktop/my.jar"> <manifest> <attribute name="main-class" value="mypackage.myclass"/> <attribute name="class-path" value="lib/slf4j.jar"/> <attribute name="class-path" value="lib/another.jar"/> </manifest> <fileset dir="/home/test/workspace/filemonitor/bin"/> </jar> </target>
Comments
Post a Comment