You don't need to create a separate VM. You just need to create a classloader.
Basically, if you want your code to be independant of ant, it will be something like that : URLClassLoader userClassLoader = new URLClassLoader(userPath, TheInterface.class.getClassLoader()); Class userClass = Class.forName(userClassName,true,userClassLoader); TheInterface userObject = userClass.newInstance() But you maybe should take into account a loaderref to allow multiple execution of your task without having to recreate every time a new ClassLoader (this is something rather heavy) Gilles 2007/11/21, todd runstein <[EMAIL PROTECTED]>: > > Gilles, > > The user is providing implementations of an interface I provide. Then > my code (which is called from my ant task) calls interface methods. > So I'm not using reflection to call the method, just to get an > instance of the users class. > > Can you point me to any code that shows how to "create a classloader > that is a child of the classloader in which the interface class is > loaded"? I'm assuming I need to actually launch my application in a > new VM using the classloader I create - any pointers to code that does > demonstrates that would be appreciated as well! > > Todd > > On Nov 21, 2007 12:00 PM, Gilles Scokart <[EMAIL PROTECTED]> wrote: > > That depends on how you want to invoke the code of the user. > > If you are using only reflection to creates the object, but also to > invoke > > the method. The, it is easier. You only have to create a classloader > > defined with the user classpath. > > If you are only using reflection to create an instance o the user > object, > > then you are using interface implemented by the user object. Then it is > a > > little bit more complex. You need to create a classloader that is a > child > > of the classloader in which the interface class is loaded. And your own > > code must also be loaded in a classloader that is the child of the > > classloader in which the interface class is loaded (or the same). > > > > Gilles > > > > 2007/11/21, todd runstein <[EMAIL PROTECTED]>: > > > > > > > > Ramu, > > > > > > Thanks for the suggestions. I've looked at both of these options, but > > > since this is a custom task that I'd like to include with an open > > > source project, neither of those options are ideal. In the "taskdef", > > > I'd like the user of my task to only have to specify where my jar file > > > is. However, because my task uses classes written by the user, I'd > > > like them to define that classpath in the task itself. > > > > > > To be honest, I may not completely understand the entire problem yet. > > > Let me explain what I *think* the problem is, and hopefully someone > > > can tell me how to fix it. > > > > > > Here is the taskdef that I would ask users to include in their build > > > script: > > > > > > <!-- classpathref needs to include migrate4j.jar so ant can find > AntTask > > > --> > > > <taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask" > > > classpathref="lib.classpath" /> > > > > > > Then, here's the actual task in use: > > > > > > <!-- The migrate task needs to know where user supplied classes are > --> > > > <target name="run" depends="compile" > > > > <migrate > > > version="1" > > > url="jdbc:h2:~/testH2" > > > driver="org.h2.Driver" > > > username="sa" > > > password="" > > > packagename="db.migrations" > > > classpathref="user.classpath" > > > /> > > > </target> > > > > > > Not to beat a dead horse, but the taskdef needs to find my code and my > > > code needs to find users code. I think that the classpath in the > > > taskdef is being used to find my task (since the task loads properly), > > > but when code in my task runs, it is too late to change the classpath > > > so my code can't find the users classes. I don't want the user to do > > > anything they wouldn't do with core ant tasks (except for the taskdef > > > entry). I've started looking at the Java, Javac and JUnit tasks to > > > see how they do this, but it's not immediately clear how to replicate > > > their behavior. > > > > > > I'm now looking at the Commandline object to see how that might work > > > with Execute. If anyone has experience using these classes, I welcome > > > any help! I may be about to run into a brick wall, so if I'm > > > obviously going down the wrong path, please let me know. > > > > > > Todd > > > > > > > > > On Nov 20, 2007 8:43 PM, Ramu Sethu <[EMAIL PROTECTED]> wrote: > > > > HI todd > > > > > > > > Have you tried the following option ?? > > > > 1. -lib option > > > > 2. classpath attribute in taskdef > > > > > > > > > > > > > > > > On Nov 21, 2007 2:15 AM, todd runstein <[EMAIL PROTECTED]> wrote: > > > > > > > > > I'm writing an ant task that calls a Java class named > Engine. Engine > > > > > is in a jar file that is included in my taskdef using the > classpath > > > > > tag. This all works great. > > > > > > > > > > The problem I'm having is that one of Engine's methods calls > > > > > "Class.forName()", trying to dynamically add a class file that is > not > > > > > in it's jar file. Here I'm getting a > ClassNotFoundException. I've > > > > > tried a few things to manually set the classpath that Ant uses, > but > > > > > I'm just hacking aimlessly and have had no luck. Could someone > give > > > > > me some direction to manually adding to the classpath that Engine > will > > > > > ultimately work with? > > > > > > > > > > Thanks in advance! > > > > > > > > > > Todd > > > > > > > > > > -- > > > > > > > > > > > > > > > > ---------------------------------------------------------------------- > > > > > The only "dumb question" is the one you were too afraid to ask. > > > > > ________________________________________ > > > > > Check out RouteRuler - Free software for runners, cyclists, > walkers, > > > etc. > > > > > http://routeruler.sourceforge.net > > > > > ________________________________________ > > > > > > > > > > > --------------------------------------------------------------------- > > > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > > > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > > > > > > > > > > > > > > -- > > > > Thank you > > > > Ramu S > > > > > > > > If A is success in life, then A equals x plus y plus z. Work is x; > y is > > > > play; and z is keeping your mouth shut. > > > > - Albert Einstein > > > > > > > > > > > > > > > > -- > > > > > > > > > ---------------------------------------------------------------------- > > > The only "dumb question" is the one you were too afraid to ask. > > > ________________________________________ > > > Check out RouteRuler - Free software for runners, cyclists, walkers, > etc. > > > http://routeruler.sourceforge.net > > > ________________________________________ > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > > > > -- > > Gilles Scokart > > > > > > -- > > > ---------------------------------------------------------------------- > The only "dumb question" is the one you were too afraid to ask. > ________________________________________ > Check out RouteRuler - Free software for runners, cyclists, walkers, etc. > http://routeruler.sourceforge.net > ________________________________________ > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- Gilles SCOKART
