I have two classes, ZipComparison and Tczip. Tczip digests a zip file and processes its MD5, while ZipComparison searches for .zip entries, and compares two different versions of the .zip file to determine if they have the same content. For example, in a package_a there is encodes.zip so I want to determine if in the package_b the encodes.zip is the same as the .zip file in package_a. So I process the MD5, and if the they match, then I don't need to copy that MD5. The build.xml is below:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<path id="external.classpath">
<pathelement location="src/commons-codec-1.2.jar"/>
</path>
<target name="clean">
<delete dir="build" />
</target>
<target name="compile">
<mkdir dir="build/classes" />
<javac srcdir="src/tczip" destdir="build/classes">
<classpath>
<path refid="external.classpath" />
</classpath>
</javac>
</target>
<target name="jar">
<mkdir dir="build/jar" />
<jar destfile="build/jar/Tczip.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="tczip.ZipComparison" />
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Tczip.jar" fork="true" />
</target>
</project>
So I'm trying to create an ant build file. ZipComparison utilizes Tczip, but when I compile, the execution is perfect, however, when I do ant run I get the following error:
C:\Users\souzamor\workspace\tczip>ant run
Buildfile: C:\Users\souzamor\workspace\tczip\build.xml
run:
[java] Processing: bhmcommonclient.zip
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
e/commons/codec/binary/Hex
[java] at tczip.Tczip.digest(Unknown Source)
[java] at tczip.Tczip.execute(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.matchMD5(Unknown Source)
[java] at tczip.ZipComparison.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.apache.commons.code
c.binary.Hex
[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:423)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
[java] ... 9 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 4 seconds
I know Tczip is missing on the compile stage, but how can I add that along ZipComparison so I don't have that kind of error ?
Thanks
It looks like the tczip.Tczip class is looking for a class from Apache Commons Codec. Make sure that this jar is on your classpath along with any other dependencies. See this answer to see how to add the classpath to your JAR's manifest when you create your JAR file from an Ant <path/> element.
Also, your XML doesn't use the depends tag to make sure that compile and jar are executed with run, and that's not good build script practice. It should really use depends:
<target name="clean">
<!-- ... -->
</target>
<target name="compile">
<!-- ... -->
</target>
<target name="jar" depends="compile">
<!-- ... -->
</target>
<target name="run" depends="jar">
<!-- ... -->
</target>
Related
Good Morning to every one.
I have a GWT application thath uses ANT for compile.
I am trying to configure the APP on a Jenkins server, but when I start the ANT task to compile, I receive this error:
[java] Exception in thread "main" java.lang.NoClassDefFoundError:
[java] Caused by: java.lang.ClassNotFoundException:
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[java] Could not find the main class: . Program will exit.
[antcall] Exiting /var/quark/repositories/jenkins/workspace/AMSSISAL-NEW_CLS_FRONTEND/build.xml.
In my local PC, I can compile with no problems.
The most strange thing, is on the error, I can't see the ClassName. Normally in this kind of errors you can see "Could not find the main class: packageName/ClassName. Program will exit." but in the log I only see a WhiteSpace instead of the Class name. Anybody knows why this can happen?
EDIT: This is my ANT Task for compile :
<target name="gwtc"depends="javac"description="GWT compile to JavaScript">
<parallel threadsperprocessor="1">
<java failonerror="true"fork="true"classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelementlocation="src"/>
<pathrefid="project.class.path"/>
</classpath>
<jvmarg value="-Xms5120M"/>
<jvmarg value="-Xmx10240M"/>
<jvmarg value="-XX:PermSize=128m"/>
<jvmarg value="-XX:MaxPermSize=1024m"/>
<jvmarg value="-XX:MinHeapFreeRatio=80"/>
<jvmarg value="-XX:MaxHeapFreeRatio=95"/>
<jvmarg value="-verbose:gc"/>
<jvmarg value="-XX:+UseConcMarkSweepGC"/>
<jvmarg value="-XX:+UseCompressedOops"/>
<jvmarg value="-XX:+CMSParallelRemarkEnabled"/>
<jvmarg value="-XX:+CMSIncrementalMode"/>
<jvmarg value="-XX:+CMSIncrementalPacing"/>
<jvmarg value="-XX:+UseParNewGC"/>
<jvmarg value="-XX:+AggressiveOpts"/>
<jvmarg value="-server"/>
<arg value="-logLevel"/>
<arg value="ERROR"/>
<arg value="-style"/>
<arg value="OBF"/>
<arg value="-optimize"/>
<arg value="9"/>
<arg value="-localWorkers"/>
<arg value="4"/>
<arg value="my.GWT.module"/>
</java>
</parallel>
Checking the ANT log I see that one of the <jvmarg> are a empty string.
This provoke that between two of the parameters are two blank spaces.
On windows there is no problem with this, but on linux (With Open JDK and Oracle JDK) the params are cutted in the double space and the rest of the command is ignored after the spaces.
So, the class is not passed as a param, this is the reason of the "java.lang.ClassNotFoundException" don't print the class name, beacuse really, ant are trying to load and empty class in the classloader:
ClassLoader.loadClass("");
This provokes the
java.lang.NoClassDefFoundError:
I am writing an ant task which uses below code :
public class Klazz extends Task{
public void execute() throws BuildException{
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/sample/alltests-fails.html");
}
In eclipse the project named is "test-project" and used "libs" folder which contains the jars (ant.jar, selenium-server-standalone-2.44.0.jar) to be added in the classpath . If I run the code in Eclipse its working fine but while running as an ant task it throws java.lang.ExceptionInInitializerError . Below is the build.xml snippet to create the jar(named custom-task.jar) file which needs to be put in the %ant_home%\lib folder.
<target name="jar" depends="compile" >
<mkdir dir="build/jar" />
<jar destfile="${env.ANT_HOME}/lib/custom-task.jar">
<fileset dir="build/classes" />
<restrict>
<name name="**/*.class" />
<archives>
<zips>
<fileset dir="${basedir}/libs/" includes="**/*.jar" />
</zips>
</archives>
</restrict>
</jar>
</target>
May be the external jars/classes not added properly in the class path while creating the jar through the "jar" task above, resulting some missing class files causing the ExceptionInInitializerError.
Advance thanks for any help on this .
below is the stack trace :
java.lang.ExceptionInInitializerError
at org.cyberneko.html.HTMLScanner.scanEntityRef(HTMLScanner.java:1415)
at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2
059)
at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:920)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499
)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452
)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.pars
e(HTMLParser.java:926)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:2
45)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.ja
va:191)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(Defau
ltPageCreator.java:268)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPa
geCreator.java:156)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient
.java:455)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
77)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
66)
at mypkg.Klazz.execute(Klazz.java:15)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at org.cyberneko.html.HTMLEntities.load0(HTMLEntities.java:101)
at org.cyberneko.html.HTMLEntities.<clinit>(HTMLEntities.java:53)
... 33 more
Total time: 2 seconds
Should there have been any class loading difficulties, I would assume a NoClassDefFoundError or a ClassNotFoundException would occur
The ExceptionInInitializerError is usually not what should draw attention, because it only says "Hey, programmer, an exception happened inside an initialization block"
More about initialization blocks here
Therefore, dealing with the NPE will fix the issue, but unfortunately I've no access to the code that could've caused this. Let me know and I shall edit the answer.
#Vlad Ilie thanks for having a look , its solved now.. the problem is with jar creation ant script .
The earlier ant task for the jar creation is not able to club all the jars in the class path and resulting... classnotfoundexception which in turn caused the ExceptionInInitializerError and NullPointerException .
Below is the fixed "jar" task which is successfully able to add all the jars in the class path .
<target name="jar" depends="compile">
<jar destfile="${env.ANT_HOME}/lib/custom-task.jar" basedir="build/classes" >
<zipgroupfileset dir="${basedir}/libs/" includes="*.jar"/>
</jar>
</target>
Above I used zipgroupfileset which is very handy .
I am trying to run a Java class as part of the deployment of my project (I want to create some resources at deployment, which can then be read at runtime).
For the most part, I am using maven for the build cycle - in particular for dependency management.
This what I've got to; creating a path (run), and adding the dependencies from maven using the maven ant tasks, and then running a target that calls a java class (MyClass), which has been compiled to ...MyClass.class in the target\src directory, using a classpath of that directory and the run path specified above.
<path id="run" />
<artifact:dependencies pathid="run">
<artifact:pom file="pom.xml" id="my_project" />
</artifact:dependencies>
<target name="runMyClass">
<java classname="...MyClass" fork="yes" maxmemory="512M" append="true">
<classpath>
<pathelement location="target\classes"/>
<pathelement id="run" />
</classpath>
</java>
</target>
I know that the target\classes is correct - if I comment out the addition of the run path, it finds the class, but reports that some of the imports in the class are not available on the classpath.
However, when I run this, I get the following stack trace:
C:\somepath\my_project\build.xml:118: java.lang.NullPointerException
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:116)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:32)
Caused by: java.lang.NullPointerException
at org.apache.tools.ant.types.resources.FileResourceIterator.addFiles(FileResourceIterator.java:104)
at org.apache.tools.ant.types.resources.FileResourceIterator.<init>(FileResourceIterator.java:95)
at org.apache.tools.ant.types.Path$PathElement.iterator(Path.java:124)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:123)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:107)
at org.apache.tools.ant.types.resources.BaseResourceCollectionContainer.cacheCollection(BaseResourceCollectionContainer.java:265)
at org.apache.tools.ant.types.resources.BaseResourceCollectionContainer.iterator(BaseResourceCollectionContainer.java:142)
at org.apache.tools.ant.types.Path.iterator(Path.java:710)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:123)
at org.apache.tools.ant.types.resources.Union.list(Union.java:86)
at org.apache.tools.ant.types.Path.list(Path.java:378)
at org.apache.tools.ant.types.Path.addExisting(Path.java:331)
at org.apache.tools.ant.types.Path.addExisting(Path.java:319)
at org.apache.tools.ant.types.Path.concatSpecialPath(Path.java:572)
at org.apache.tools.ant.types.Path.concatSystemClasspath(Path.java:532)
at org.apache.tools.ant.types.CommandlineJava.haveClasspath(CommandlineJava.java:647)
at org.apache.tools.ant.types.CommandlineJava.addCommandsToList(CommandlineJava.java:437)
at org.apache.tools.ant.types.CommandlineJava.getCommandline(CommandlineJava.java:405)
at org.apache.tools.ant.types.CommandlineJava.describeCommand(CommandlineJava.java:482)
at org.apache.tools.ant.taskdefs.Java.checkConfiguration(Java.java:176)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:107)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
... 16 more
To me, this looks like an exception is being thrown in the ant code in the process of adding the path set to the classpath, but I could be wrong.
Can anyone suggest (any of the following):
how I might go about debugging this?
an alternative approach to do what I'm trying to do (described
above)?
A little further playing gave me a workable solution...
Rather than refering to the maven dependencies as a path, I can refer to them using a fileset:
<fileset id="run" />
<artifact:dependencies filesetid="run">
<artifact:pom file="pom.xml" id="my_project" />
</artifact:dependencies>
<target name="runMyClass">
<java classname="...MyClass" fork="yes" maxmemory="512M" append="true">
<classpath>
<pathelement location="target\classes"/>
<fileset refid="run" />
</classpath>
</java>
</target>
I don't know what was going on with the other approach, whether it's user error or a bug, so if anyone has any suggestions, I would appreciate comments.
I am trying to compile and run a simple java class within eclipse. The compile task works fine, and since I do not specify a destination folder the build files are in the same directory as the source. Which is alright, at the moment all I need is to learn how I can run the class with the main() method.
I have tried using the fully qualified name of the class (with package name, etc) and the classname alone, but always I get a java.lang.ClassNotFoundException
Buildfile: C:\Users....\build.xml
run:
[java] java.lang.NoClassDefFoundError: code/control/MyClass
[java] Caused by: java.lang.ClassNotFoundException: code.control.MyClass
[java] at java.net.URLClassLoader$1.run(Unknown Source)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[java] Could not find the main class: code.control.MyClass. Program will exit.
[java] Exception in thread "main"
[java] Java Result: 1
compile:
default:
BUILD SUCCESSFUL
Total time: 234 milliseconds
Below, are the targets taken from my build.xml file:
<target name="default" depends="compile" description="learn">
</target>
<target name="compile" depends="run">
<javac srcdir="src/" />
</target>
<target name="run">
<java classname="code.control.MyClass" fork="true"/>
</target>
I can't figure out why the class is not found. MyClass contains the main() method and since i specify no classpath it should look at the current directory, which is the src/ right?
The development directory is the usual eclipse file structure:
projectName/src/code/control/MyClass
If it is a classpath problem how could I resolve it? I always had problem grasping the concept "put it on your classpath" ... If someone could provide a little bit of explanation with the classpath in the ant context, I would be very thankful.
Thanks for any help on this. The version of ant is 1.7.0
The classpath is where the Java runtime looks for your .class files, similar to how your OS uses the PATH variable to find executables.
Try this in your build script:
<target name="run">
<java fork="true" classname="code.control.MyClass">
<classpath>
<path location="src/"/>
</classpath>
</java>
There's a HelloWorld version for ant that walks through building a Java program with ant.
you should include classpath, e.g.
<java classpath="${bin}" classname="code.control.MyClass">
where ${bin} is your output folder.
change your build.xml as below and try out:
<target name="default" depends="run" description="learn">
</target>
<target name="compile" >
<javac srcdir="src/" />
</target>
<target name="run" depends="compile">
<java classname="code.control.MyClass" fork="true"/>
</target>
I can't figure out why I am getting this exception from my ant build.xml file. I checked and everything is in the classpath. Why must this be so complicated?!
I had trouble with Ant in the past and it seems it always is something related to the classpath. I am pointing to junit.jar using both ways: within eclipse: window->preferences->ant->runtime->Ant Home->Add External Jars, and also within the build.xml script. This time Ant is not able to locate my test class in the junit task. Is there something wrong with the way I am pointing to this class?
<target name="init">
<property name="sourceDir" value="src"/>
<property name="outputDir" value="build" />
<property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" />
</target>
<target name="clean" depends="init">
<delete dir="${outputDir}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${outputDir}" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>
<path id="classpath">
<pathelement location="${outputDir}" />
<pathelement location="${junitLocation}" />
</path>
<target name="testApplication" depends="compile">
<echo>Running the junit tests...</echo>
<junit fork="yes" haltonfailure="yes">
<test name="my.package.MyTest" />
<formatter type="plain" usefile="false" />
<classpath refid="classpath" />
</junit>
</target>
I am always getting:
[junit] Testsuite: my.package.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] my.package.MyTest
[junit] java.lang.ClassNotFoundException: my.package.MyTest
[junit] at java.net.URLClassLoader$1.run(Unknown Source)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Unknown Source)
BUILD FAILED
Apparently, Ant finds junit.jar and attempts to start the test, but why can't it find my test class? I point to the folder with compiled test class. So I know that junit is on Ant's classpath at least, but the ClassNotFound puzzles me.
Any ideas perhaps? Many thanks!
Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.
Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.
You don't need to add the JUnit jar to the classpath. If ant can't find it, the <junit> task won't run.
I suggest trying different ways of specifying the classpath as described in the ant doc at http://ant.apache.org/manual/using.html#path; specifically <classpath path="${srcdir}" /> might help.
If not, ChssPly76's suggestion of running ant -debug from the command line is the best bet. You'll want to bump up the buffer on your command prompt window though, because ant's debugger is extremely verbose.