Apache Ant Custom Task: Class Not Found Exception from Class itself

I have created an application with two modes:

  • Used from the command line
  • Used by Ant

This works well from the command line, but from ANT the error is a surprise, the Java loader cannot find the main class and an exception was thrown from the main class!

Extracting the main class

package hpms.app.mon.client;
import ....;
public class MonitorMainFrame extends Application implements ProcessListener {
   ....
   public static void main( String[] args ) throws Exception {
      final Class<?> c = Class.forName( MonitorMainFrame.class.getName());
      /* The class was found and we print the constructor */
      System.err.println( c.getConstructor());
      launch( args );                                    // line 544
   }
}

      

Build execution

Buildfile: D:\dev\java\2014\hpms.app.mon\build.xml

compile-sample:

run-servers:

BUILD SUCCESSFUL
Total time: 1 second
Buildfile: D:\dev\java\2014\hpms.app.mon\build.xml

compile-sample:

run-client:
[monitor-client] public hpms.app.mon.client.MonitorMainFrame() throws java.lang.Exception  <==== Trace of the constructor object

BUILD FAILED
D:\dev\java\2014\hpms.app.mon\build.xml:112: java.lang.RuntimeException: java.lang.ClassNotFoundException: hpms.app.mon.client.MonitorMainFrame
        at javafx.application.Application.launch(Application.java:260)
        at hpms.app.mon.client.MonitorMainFrame.main(MonitorMainFrame.java:544)
        at hpms.app.mon.client.ant.AntTask.execute(AntTask.java:79)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="jar">
   ...

   <taskdef
      onerror  ="ignore"
      name     ="monitor-client"
      classpath="bin"
      classname="hpms.app.mon.client.ant.AntTask" />

   <target name="run-client" depends="compile-sample" description="...">
      <monitor-client
         minimized="true"
         autostart="true">
         ...

      

How the class is not found Can an exception be thrown from the class itself?

I am assuming that the Java classloader has loaded the class and the ANT classloader has not ...

+3


source to share





All Articles