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:
Related
My goal is to start tomcat using Ant. Here is my script:
<target name="tomcat-start">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" dir="${tomcat.home}">
<classpath>
<fileset dir="${tomcat.home}/bin">
<include name="bootstrap.jar"/>
<include name="tomcat-juli.jar"/>
</fileset>
</classpath>
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
</java>
</target>
After script execution I receive this output:
java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.apache.catalina.startup.Bootstrap.<clinit>(Bootstrap.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
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)
... 1 more
Exception in thread "main"
Java Result: 1
I've checked: org.apache.juli.logging.LogFactory class is presented in tomcat-juli.jar!
What could be wrong?
For some unknown weird reasons tomcat doesn't start in my system even when I launch it from command line using java -jar command.
However, I managed to start it using java -cp "bin\bootstrap.jar;bin\tomcat-juli.jar" org.apache.catalina.startup.Bootstrap command, executed from tomcat.home directory.
The code in ant which does the same:
<exec executable="java" dir="${tomcat.home}">
<arg line="-cp bin\bootstrap.jar;bin\tomcat-juli.jar"/>
<arg value="org.apache.catalina.startup.Bootstrap"/>
</exec>
If you want to stick with using catalina, the proper method is to add \setenv.bat with contents like:
set CLASSPATH=D:\tomcat\apache-tomcat-7.0.42\bin\bootstrap.jar;D:\tomcat\apache-tomcat-7.0.42\bin\tomcat-juli.jar
I discovered that by reading catalina.bat. Someone might want to update this with the proper Tomcat doc
Check out the startup.sh batch file or shell script of tomcat. It has bunch of other files apart from bootstrap.jar. check setclasspath.sh. It has all jars are being set in class path.
Other more clean approach would be to invoke startup.sh script from ant. This will do everything for what it takes to start Tomcat Server.
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>
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 have some trouble with getting a Java application to run in the console and/or with Ant.
I know that a lot of starting issues are related to the classpath being not set or incorrectly set, though I'm fairly sure I set it correctly, so my search only yielded results on that.
Here is the general setup of my application:
classes are in packages model, view and controller. controller.Controller is the class with the main method. I am using objectdb as my JPA provider.
I am using Ant to compile my application.
After compiling, I can run my application from ant with the following script:
<target name="run" description="default build process">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath" />
</classpath>
</java>
</target>
where ${main-class} is controller.Controller and classpath consists of /lib and /dist folders (the application's jar file is compiled to /dist)
Now I tried copying all .jar files from /lib and /dist to one separate folder and run them withjava -jar cooking.jar -cp . which results in
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
at model.jpa.JPAModelFactory.<init>(JPAModelFactory.java:28)
at model.jpa.JPAModelFactory.<init>(JPAModelFactory.java:24)
at controller.Controller.<init>(Controller.java:59)
at controller.Controller.main(Controller.java:116)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
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)
... 4 more
So I tried ant and slightly modified above build target to:
<target name="run2" description="default build process">
<java fork="true" jar="${dist.dir}/${ant.project.name}.jar">
<classpath>
<path refid="classpath" />
</classpath>
</java>
</target>
which results in the same error. I don't understand why.
Just to test it, I tried running from the command line by specifying the main class directly: java -cp . controller.Controller which for some reason cannot even locate the class (it's there, I confirmed it):
Exception in thread "main" java.lang.NoClassDefFoundError: controller/Controller
Caused by: java.lang.ClassNotFoundException: controller.Controller
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)
Could not find the main class: controller.Controller. Program will exit.
I have set JAVA_HOME to my JDK's path, and CLASSPATH to my JRE's/lib path.
OS is Windows 7 64 bit, Java version is 1.6.0_25-b06
I am puzzled by two things:
a) Why is Java unable to locate controller.Controller, even though it is present in the .jar file and the .jar file is in the current directory?
b) What am I doing wrong that calling Java with -jar seems to mess up the lookup mechanisms.
Any help is highly appreciated.
The class path should consist of
directories with class files (in their proper package directory)
jar files.
You cannot point the class path to a directory of jars. Things are different when running a application server (eg Tomcat), which will load jars from a directory for you.
though I'm fairly sure I set it correctly
The evidence is against you. The JVM is telling you that you have not set it correctly.
What do you think that ref 'classpath' pointing to? Where do you assume its values are coming from? They should be defined inside the Ant build.xml, right? Like this:
<path id="production.class.path">
<pathelement location="${production.classes}"/>
<pathelement location="${production.resources}"/>
<fileset dir="${production.lib}">
<include name="**/*.jar"/>
<exclude name="**/junit*.jar"/>
<exclude name="**/*test*.jar"/>
</fileset>
</path>
<path id="test.class.path">
<path refid="production.class.path"/>
<pathelement location="${test.classes}"/>
<pathelement location="${test.resources}"/>
<fileset dir="${test.lib}">
<include name="**/junit*.jar"/>
<include name="**/*test*.jar"/>
</fileset>
</path>
If you're creating an executable JAR, you need to specify the main class and classpath in the manifest, as CoolBeans correctly pointed out in the comment. The 3rd party JAR locations have to be relative to the executable JAR. You should package them with your executable JAR in such a way that the relative path is easy to sort out and understand.
I've found this happens when I specify both the <classpath> and the jar="..." in the target. I removed the jar="...", placed that .jar into the <classpath> list and it ran after that.
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>