views:

755

answers:

7

Hello,

I am trying to run a java task from ant. I am trying to run the "org.apache.tools.ant.launch.Launcher" class. I keep on getting the "NoClassDefFoundError" without any class name being specified. I am also getting a "ClassNotFoundException" along with that displaying a message "Could not find the main class: . Program will exit". Here's a snippet of the error

 [java] Exception in thread "main" java.lang.NoClassDefFoundError: 
 [java] Caused by: java.lang.ClassNotFoundException: 
 [java]  at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
 [java]  at java.security.AccessController.doPrivileged(Native Method)
 [java]  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
 [java]  at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 [java]  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 [java]  at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
 [java]  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
 [java] Could not find the main class: .  Program will exit.
 [java] Java Result: 1

Now I am trying to run an ant class from an ant jar and i specifiy the classpath where this class file resides using the "classpathref" attribute, however I still get this message. I checked the ant jar to check the Manifest and the "main" class is specified properly (it's "org.apache.tools.ant.launch.Launcher") . I have exhausted all my resources. Please help ! ! !

ps: My environment is Eclipse on Ubuntu 9.04

A: 

From this line:

[java] Could not find the main class: .  Program will exit.

it looks as though your call to java.exe is finding a . where it expects a class name. Perhaps you are trying to indicate the classpath on the commandline but are neglecting to preface that with the -cp or -classpath flag.

akf
here's what i have in build.xml<java classname="org.apache.tools.ant.launch.Launcher" classpathref="webtest.lib" jvm="${javahome.dir}/bin/java" fork="true" failonerror="false">
Safder
@Safder. Perhaps you should show how `webtest.lib` is defined.
Alexander Pogrebnyak
+1  A: 

This can be a misleading error that is not actually about a class missing from the classpath. If you are using Tomcat it can be due to missing conf files in $CATALINA_BASE/conf

It could also be a misconfigured ant installation, please check your JAVA_HOME and ANT_HOME env variables or try another ant installation.

Alb
+2  A: 

Most likely your classpath is misconfigured.

At a minimum the CLASSPATH should include:

  • ant.jar and ant-launcher.jar
  • jars/classes for your XML parser
  • the JDK's required jar/zip files

(from the ant manual)

Also you seem to be relaunching ant in the current directory (executing the same build.xml). Maybe you'll want to set the "dir" property.

kritzikratzi
+1  A: 

Ant launcher expects the following params

java -Dant.home=c:\ant org.apache.tools.ant.launch.Launcher [options] [target]

I am affraid we can't proceed answering you if you don't paste your whole build.xml file.

Just try to give your full sample as below:

   <java
            classname="org.apache.tools.ant.launch.Launcher"
            fork="true"
            failonerror="true"
            dir="${sub.builddir}"
            timeout="4000000"
            taskname="startAnt"
    >
        <classpath>
            <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
        </classpath>
        <arg value="-buildfile"/>
        <arg file="${sub.buildfile}"/>
        <arg value="-Dthis=this"/>
        <arg value="-Dthat=that"/>
        <arg value="-Dbasedir=${sub.builddir}"/>
        <arg value="-Dthe.other=the.other"/>
        <arg value="${sub.target}"/>
 </java>

This would be extremely helpful to provide you with a possible misunderstanding.

Hope this helps,

Ernani

Ernani Joppert
+3  A: 

It looks like the Ant task is trying to run Java, but is somehow passing an empty string to the JVM as the name of the class to run. I can get the same stacktrace if I run the JVM directly with a quoted empty string:

C:\>java ""
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: .  Program will exit.

(This is on Windows, but I don't think that makes much of a difference.)

I can only suggest following up on Alexander Pogrebnyak's comment to akf's answer. Perhaps the webtest.lib property has spaces in it?

Also, is there a good reason for calling ant directly via java, rather than using the ant task?

Pourquoi Litytestdata
A: 

When in doubt, invoke ant -v and watch all your variable declarations, and the whole commandline sent to Java.

Certain path-like quantities are eagerly evaluated, while others are lazily evaluated. I've had plenty of problems where I used one of the former, when my Ant script intended to create a jar that would be used by a later task. Then by the time I invoked the call, it had already pruned my jar from the classpath.

If I had to make a wild guess, I'd bet your commandline looked something like:

java ... -classpath org.apache.tools.ant.launch.Launcher

instead of

java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher

like you expected

Jason
A: 

Hey everyone

Thanks for your input on this matter, but I was able to resolve this matter long back. My apologies, I should have updated this thread. I do not remember the exact details of what I did, but I do remember that it was a JVM issue. I was using Hotspot, and I think I changed it to use JRocket, and things started working after that. That's all I remember. Thanks.

Safder