views:

2938

answers:

4

Hello,

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

+3  A: 

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.

seth
Thanks Seth, that worked. Just wondering about the classpath, isn't it automatically set when Eclipse was installed? I know I can execute java -version from any directory at he command line.I thought this was taken care of automatically, but when I need to "put something" on the classpath, how do I do it? Hope I am not digressing too much from the original question.Thanks again
denchr
When you use 'java -version', that's using the PATH variable to find the java executable. If you need to put something on the classpath, just copy the .class file into one of the directories specified by classpath. You'll need all the directories if you are copying something in a package.
seth
+3  A: 

you should include classpath, e.g.

<java classpath="${bin}" classname="code.control.MyClass">

where ${bin} is your output folder.

janetsmith
Thanks this answer worked too!
denchr
A: 

I think you want to change your depends clause:

. . . . . .

WayneH
A: 

hi..........everyone...........i am running java program thorgh ant using exec command..

but if classname or classpath is worng then in the output i am getting exception .and if everything is classname and classpath is correct am getting correct output.....my question how to determine the exception in ant?..........if anyone knows give d ans with code.....its realy urgent.........thanks.......

kosal