You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Marzia Forli <ma...@yahoo.com> on 2010/08/02 14:32:01 UTC

tomcat 7.0 embedded usage

Can somebody please help me to setup a hello world example of embedded usage of tomcat 7 with servlet 3.0 support...
I have a simple annotated HelloWorld servlet and would like to launch it from my eclipse environment. I have:
tomcat-catalina.jar
tomcat-servlet-api.jar
tomcat-juli.jar
tomcat-annotations-api.jar
tomcat-api.jar
tomcat-util.jar
tomcat-coyote.jar
on my test classpath which is "target/test-classes" and have my project on "target/classes"... I am using latest eclipse, java releases. Below is the code of where I am now...
Thanks

import java.io.*;
import org.apache.catalina.startup.*;

public class EmbeddedTomcat {
    private final Tomcat server;

    public EmbeddedTomcat (final String host, final int port, final String contextPath, final String... classPaths) {
        final String tempPath = System.getProperty("java.io.tmpdir");
        final File tempDirectory = new File(tempPath);
        System.setProperty("catalina.base", tempDirectory.getAbsolutePath());

        final File appBase = new File(tempDirectory, "webapps");
        appBase.mkdir();

        server = new Tomcat();
        server.setBaseDir(tempDirectory.getAbsolutePath());
        server.getHost().setAppBase(appBase.getAbsolutePath());

        server.setHostname(host);
        server.setPort(port);

        final File appDirectory = new File("target/test-classes", "webapps" + contextPath);
        server.addWebapp(null, contextPath, appDirectory.getAbsolutePath());
    }

    public void start () throws Exception {
        server.start();
    }

    public void stop () throws Exception {
        server.stop();
    }

    public static void main (final String[] args) {
        final EmbeddedTomcat container = new EmbeddedTomcat("localhost", 8080, "/", "target/classes", "target/test-classes");
        try {
            container.start();
            System.in.read();
            container.stop();
        } catch (final Exception problem) {
            System.exit(100);
        }
    }
}



      


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


Re: tomcat 7.0 embedded usage

Posted by Mark Thomas <ma...@apache.org>.
On 22/08/2010 04:04, Mondain wrote:
> This was blogged earlier this week..
> http://copperykeenclaws.wordpress.com/2010/08/19/embedding-tomcat-7/
> I'll be using this info for the Red5 plugin very soon..

The POM error will be fixed for 7.0.3 onwards.

As an aside, if you find a problem please report it. It might not get
fixed immediately but it will get fixed.

Mark

> 
> <http://copperykeenclaws.wordpress.com/2010/08/19/embedding-tomcat-7/>Paul
> 
> On Wed, Aug 4, 2010 at 2:34 AM, David Calavera <da...@gmail.com>wrote:
> 
>> Hi,
>>
>> I don't know if that's your problem but I use the method server.await to
>> allow the server to wait.
>>
>> On Mon, Aug 2, 2010 at 2:32 PM, Marzia Forli <ma...@yahoo.com>
>> wrote:
>>
>>> Can somebody please help me to setup a hello world example of embedded
>>> usage of tomcat 7 with servlet 3.0 support...
>>> I have a simple annotated HelloWorld servlet and would like to launch it
>>> from my eclipse environment. I have:
>>> tomcat-catalina.jar
>>> tomcat-servlet-api.jar
>>> tomcat-juli.jar
>>> tomcat-annotations-api.jar
>>> tomcat-api.jar
>>> tomcat-util.jar
>>> tomcat-coyote.jar
>>> on my test classpath which is "target/test-classes" and have my project
>> on
>>> "target/classes"... I am using latest eclipse, java releases. Below is
>> the
>>> code of where I am now...
>>> Thanks
>>>
>>> import java.io.*;
>>> import org.apache.catalina.startup.*;
>>>
>>> public class EmbeddedTomcat {
>>>    private final Tomcat server;
>>>
>>>    public EmbeddedTomcat (final String host, final int port, final String
>>> contextPath, final String... classPaths) {
>>>        final String tempPath = System.getProperty("java.io.tmpdir");
>>>        final File tempDirectory = new File(tempPath);
>>>        System.setProperty("catalina.base",
>>> tempDirectory.getAbsolutePath());
>>>
>>>        final File appBase = new File(tempDirectory, "webapps");
>>>        appBase.mkdir();
>>>
>>>        server = new Tomcat();
>>>        server.setBaseDir(tempDirectory.getAbsolutePath());
>>>        server.getHost().setAppBase(appBase.getAbsolutePath());
>>>
>>>        server.setHostname(host);
>>>        server.setPort(port);
>>>
>>>        final File appDirectory = new File("target/test-classes",
>> "webapps"
>>> + contextPath);
>>>        server.addWebapp(null, contextPath,
>> appDirectory.getAbsolutePath());
>>>    }
>>>
>>>    public void start () throws Exception {
>>>        server.start();
>>>    }
>>>
>>>    public void stop () throws Exception {
>>>        server.stop();
>>>    }
>>>
>>>    public static void main (final String[] args) {
>>>        final EmbeddedTomcat container = new EmbeddedTomcat("localhost",
>>> 8080, "/", "target/classes", "target/test-classes");
>>>        try {
>>>            container.start();
>>>            System.in.read();
>>>            container.stop();
>>>        } catch (final Exception problem) {
>>>            System.exit(100);
>>>        }
>>>    }
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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


Re: tomcat 7.0 embedded usage

Posted by Mondain <mo...@gmail.com>.
This was blogged earlier this week..
http://copperykeenclaws.wordpress.com/2010/08/19/embedding-tomcat-7/
I'll be using this info for the Red5 plugin very soon..

<http://copperykeenclaws.wordpress.com/2010/08/19/embedding-tomcat-7/>Paul

On Wed, Aug 4, 2010 at 2:34 AM, David Calavera <da...@gmail.com>wrote:

> Hi,
>
> I don't know if that's your problem but I use the method server.await to
> allow the server to wait.
>
> On Mon, Aug 2, 2010 at 2:32 PM, Marzia Forli <ma...@yahoo.com>
> wrote:
>
> > Can somebody please help me to setup a hello world example of embedded
> > usage of tomcat 7 with servlet 3.0 support...
> > I have a simple annotated HelloWorld servlet and would like to launch it
> > from my eclipse environment. I have:
> > tomcat-catalina.jar
> > tomcat-servlet-api.jar
> > tomcat-juli.jar
> > tomcat-annotations-api.jar
> > tomcat-api.jar
> > tomcat-util.jar
> > tomcat-coyote.jar
> > on my test classpath which is "target/test-classes" and have my project
> on
> > "target/classes"... I am using latest eclipse, java releases. Below is
> the
> > code of where I am now...
> > Thanks
> >
> > import java.io.*;
> > import org.apache.catalina.startup.*;
> >
> > public class EmbeddedTomcat {
> >    private final Tomcat server;
> >
> >    public EmbeddedTomcat (final String host, final int port, final String
> > contextPath, final String... classPaths) {
> >        final String tempPath = System.getProperty("java.io.tmpdir");
> >        final File tempDirectory = new File(tempPath);
> >        System.setProperty("catalina.base",
> > tempDirectory.getAbsolutePath());
> >
> >        final File appBase = new File(tempDirectory, "webapps");
> >        appBase.mkdir();
> >
> >        server = new Tomcat();
> >        server.setBaseDir(tempDirectory.getAbsolutePath());
> >        server.getHost().setAppBase(appBase.getAbsolutePath());
> >
> >        server.setHostname(host);
> >        server.setPort(port);
> >
> >        final File appDirectory = new File("target/test-classes",
> "webapps"
> > + contextPath);
> >        server.addWebapp(null, contextPath,
> appDirectory.getAbsolutePath());
> >    }
> >
> >    public void start () throws Exception {
> >        server.start();
> >    }
> >
> >    public void stop () throws Exception {
> >        server.stop();
> >    }
> >
> >    public static void main (final String[] args) {
> >        final EmbeddedTomcat container = new EmbeddedTomcat("localhost",
> > 8080, "/", "target/classes", "target/test-classes");
> >        try {
> >            container.start();
> >            System.in.read();
> >            container.stop();
> >        } catch (final Exception problem) {
> >            System.exit(100);
> >        }
> >    }
> > }
> >
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
>



-- 
http://gregoire.org/
http://code.google.com/p/red5/
http://code.google.com/p/blue5/

Re: tomcat 7.0 embedded usage

Posted by David Calavera <da...@gmail.com>.
Hi,

I don't know if that's your problem but I use the method server.await to
allow the server to wait.

On Mon, Aug 2, 2010 at 2:32 PM, Marzia Forli <ma...@yahoo.com> wrote:

> Can somebody please help me to setup a hello world example of embedded
> usage of tomcat 7 with servlet 3.0 support...
> I have a simple annotated HelloWorld servlet and would like to launch it
> from my eclipse environment. I have:
> tomcat-catalina.jar
> tomcat-servlet-api.jar
> tomcat-juli.jar
> tomcat-annotations-api.jar
> tomcat-api.jar
> tomcat-util.jar
> tomcat-coyote.jar
> on my test classpath which is "target/test-classes" and have my project on
> "target/classes"... I am using latest eclipse, java releases. Below is the
> code of where I am now...
> Thanks
>
> import java.io.*;
> import org.apache.catalina.startup.*;
>
> public class EmbeddedTomcat {
>    private final Tomcat server;
>
>    public EmbeddedTomcat (final String host, final int port, final String
> contextPath, final String... classPaths) {
>        final String tempPath = System.getProperty("java.io.tmpdir");
>        final File tempDirectory = new File(tempPath);
>        System.setProperty("catalina.base",
> tempDirectory.getAbsolutePath());
>
>        final File appBase = new File(tempDirectory, "webapps");
>        appBase.mkdir();
>
>        server = new Tomcat();
>        server.setBaseDir(tempDirectory.getAbsolutePath());
>        server.getHost().setAppBase(appBase.getAbsolutePath());
>
>        server.setHostname(host);
>        server.setPort(port);
>
>        final File appDirectory = new File("target/test-classes", "webapps"
> + contextPath);
>        server.addWebapp(null, contextPath, appDirectory.getAbsolutePath());
>    }
>
>    public void start () throws Exception {
>        server.start();
>    }
>
>    public void stop () throws Exception {
>        server.stop();
>    }
>
>    public static void main (final String[] args) {
>        final EmbeddedTomcat container = new EmbeddedTomcat("localhost",
> 8080, "/", "target/classes", "target/test-classes");
>        try {
>            container.start();
>            System.in.read();
>            container.stop();
>        } catch (final Exception problem) {
>            System.exit(100);
>        }
>    }
> }
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>