Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException with ant's java task and classpath

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

like image 765
denchr Avatar asked Feb 12 '26 16:02

denchr


1 Answers

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.

like image 116
seth Avatar answered Feb 15 '26 12:02

seth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!