You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Rolf Schumacher <ma...@august.de> on 2006/04/08 20:51:38 UTC

calling ant launcher

Dear Tomcat professionals,

I tried to automate some installation over the Web
I got a class cast exception when trying to launch Ant from a servlet.
The program works fine if I execute it from the command line.
In a servlet under Tomcat it causes a

java.lang.ClassCastException: org.apache.tools.ant.Main
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:275)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:96)

I looked up line 275 in Launcher.java:

270        URLClassLoader loader = new URLClassLoader(jars);
271        Thread.currentThread().setContextClassLoader(loader);
272        Class mainClass = null;
273        try {
274             mainClass = loader.loadClass(MAIN_CLASS);
275            AntMain main = (AntMain) mainClass.newInstance();
276            main.startAnt(newArgs, null, null);

Do you have an idea why the loader does load something different
that usually can be casted to AntMain?

Any help appreaciated.

Rolf


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: calling ant launcher

Posted by Rolf Schumacher <ma...@august.de>.
Found a good explenation for my error here:

http://www.jspwiki.org/wiki/A2AClassCastException

Building a servlet that invokes Ant,
one should not use the Launcher.java in favor of the Ant class Project.java:

This is my invoker - assuming ant.jar, ant-launcher.jar and e.g. 
ant-trax.jar in WEB-INF/lib:

    private void execAnt( Properties properties, String buildFile ) {
        try {
            // create a project
            final Project project = new Project();
            // Add the default listener - a simple Logger extending 
BuildLogger
            project.addBuildListener( new AntLogger() );
            // set the classloader
            project.setCoreLoader(this.getClass().getClassLoader());
            // set the input handler, don't know if this is really needed
            project.setInputHandler(new DefaultInputHandler());
            // init
            project.init();
            // set the version and other properties
            project.setUserProperty(MagicNames.ANT_VERSION, "unknown" );
            for( Object property: properties.keySet() )
                project.setUserProperty( (String)property,
                        properties.getProperty( (String)property) );
            // set the path to the build file
            project.setUserProperty(MagicNames.ANT_FILE, buildFile );
            // configure the project
            ProjectHelper.configureProject(project, new File( buildFile) );
            // set the target
            Vector<String> targets = new Vector<String>();
            targets.addElement(project.getDefaultTarget());
            // execute the project
            project.executeTargets(targets);
        } catch( Throwable t ) { log.error( FAILED, t );
        } finally {}
    }


Rolf Schumacher wrote:
> Dear Tomcat professionals,
>
> I tried to automate some installation over the Web
> I got a class cast exception when trying to launch Ant from a servlet.
> The program works fine if I execute it from the command line.
> In a servlet under Tomcat it causes a
>
> java.lang.ClassCastException: org.apache.tools.ant.Main
>    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:275)
>    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:96)
>
> I looked up line 275 in Launcher.java:
>
> 270        URLClassLoader loader = new URLClassLoader(jars);
> 271        Thread.currentThread().setContextClassLoader(loader);
> 272        Class mainClass = null;
> 273        try {
> 274             mainClass = loader.loadClass(MAIN_CLASS);
> 275            AntMain main = (AntMain) mainClass.newInstance();
> 276            main.startAnt(newArgs, null, null);
>
> Do you have an idea why the loader does load something different
> that usually can be casted to AntMain?
>
> Any help appreaciated.
>
> Rolf
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org