0

I am trying to run a script in Ant and I am running into an issue related to class loaders and class paths. Here're the specifics of my script

<taskdef name="jmxInvoke" classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask" classpath=${someClassPath} />
    <script language="javascript>
     importClass(org.apache.catalina.ant.jmx.Arg);

     var jmxInvokeTask = project.createTask("aJMXInvokeAntTask")

     var someArg = new Arg();
     someArg.setValue("someValue");
     someTask.addArg(someArg);
    </script>

When I have "catalina-ant.jar" in my ANT_HOME/lib, the above script runs fine. I am trying to update the script with the assumption that I cannot copy catalina-ant.jar to ANT_HOME/lib in other environments. Hence I was trying to find a way where I have the catalina-ant.jar available to the classpath when this script is run. As a result I tried to specify the classpath during which I get an exception saying there is no such "addArg" method in "jmxInvokeTask" which takes an argument of type "Arg". This was very confusing to me, since JMXAccessorInvokeTask does have this method. As I looked deeper, I found that Ant uses a different classloader when we specify a classpath. Also, when I did a comparison of the class loaders, it seems like there are two different class loaders used when we call "project.createTask" and when we say something like "new Arg()". Has anyone else solved this problem? Thanks.

Seagull
  • 2,219
  • 6
  • 25
  • 33

2 Answers2

0

See How to load an optional task into ant without -lib or global installation? for solution, you may also use the ant-classloader task.

Community
  • 1
  • 1
Rebse
  • 10,307
  • 2
  • 38
  • 66
  • Unfortunately, I tried a solution similar to the first link and I also tried the "ant-classloader" task. I think what makes my situation unique is the the way I am instantiating "Arg". The problem is one classloader is loading the "JMXInvokeTask" and the other class loader is instantiating "Arg". The solutions you suggested don't address this scenario. I hoped that the ant-classloader might help, but I still see the same error :'-( – Seagull Feb 25 '14 at 03:06
0

Alright, I was able to resolve this problem.

I am sure there might be a more elegant solution, but what I ended up doing was using

jmxInvokeTask.getClass().getClassLoader().loadClass("org.apache.catalina.ant.jmx.Arg").newInstance();

instead of doing just "new....". This way I used the same classloader that was used to create the jmxInvokeTask.

Seagull
  • 2,219
  • 6
  • 25
  • 33