Java ClassLoader cant find class

in my project there are - among others - two classes: TheProblem and Server.

public class TheProblem {
    public static void main (String args []) throws ClassNotFoundException {
        ClassLoader.getSystemClassLoader().loadClass("Server");
    }
}

When I execute the code from the command line, everything works just fine. But when I use ANT to execute the code, I get a ClassNotFoundException - although both Server.class and TheProblem.class are inside of the same directory.

The directory structure of my project is fairly simple - I will try to illustrate it here:

root_folder/
- build.xml
- src/
    - TheProblem.java
    - Server.java
- build/
    - TheProblem.class
    - Server.class

Here is an excerpt of my build.xml file:

<?xml version="1.0" encoding="UTF-8" ?>

<project name="JAXB" default="compile">

<path id="project.class.path">
    <pathelement path="${java.class.path}" />
    <pathelement location="build" />
 </path>

 <target name="init" >
    <mkdir dir="build" />
 </target>

 <target name="compile" depends="init" >
     <javac classpathref="project.class.path" srcdir="src" destdir="build"
         includeAntRuntime="false" />
 </target>

 <target name="execute-problem" depends="compile">
     <java classpathref="project.class.path" classname="TheProblem" />
 </target>

 <target name="clean" depends="init">
     <delete dir="build" />
 </target>
 </project>

When I execute ant compile, everything compiles, but when I execute ant execute-problem, the ClassLoader cannot find the Server class and throws a ClassNotFoundException. When I navigate into the build directory and call java TheProblem, it works just fine. I really have no clue, why it doesn't work using ANT.

Thank you very much for taking the time to read this post.


Instead of

<target name="execute-problem" depends="compile">
         <java classpathref="project.class.path" classname="TheProblem" />
     </target> 

try to use this

 <target name="execute-problem" depends="compile">
   <java fork="true" dir="." classname="TheProblem">
     <classpath>
      <path refid="project.class.path" />
     </classpath>
   </java>
 </target> 
链接地址: http://www.djcxy.com/p/44400.html

上一篇: Eclipse,蚂蚁和自定义任务

下一篇: Java ClassLoader不能找到类