Larry Price
Larry Price

Reputation: 439

Running .war files with Jetty

I'm struggling with creating the most basic of examples of running a Jetty application and launching a .war package at the same time. Everything I find just says to put the .war in "$JETTY_HOME/webapps", but I'm not sure how to verify what "$JETTY_HOME" is. I'm trying to extend the simple heroku default application found at https://github.com/heroku/java-getting-started.git. My directory structure:

src/
-- main/
---- java/
------ Main.java
target/
-- (lots of stuff in here)
pom.xml
Procfile
webapps/
-- workbench.war

I run my application with java -cp target/classes:target/dependency/* Main.

Main.java is identical to: https://raw.githubusercontent.com/heroku/java-getting-started/master/src/main/java/Main.java.

How can I get this application to run .war files? Whenever I visit localhost:5000/workbench I just see "Hello World" where I should be seeing the Workbench application contained in workbench.war.

Upvotes: 4

Views: 4038

Answers (2)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

If its just a single war, do this.

package org.eclipse.jetty.demo;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.JettyWebXmlConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.TagLibConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

public class EmbedMe
{
    public static void main(String[] args) throws Exception
    {
        int port = 8080;
        Server server = new Server(port);

        String warpath = "webapps/workbench.war";

        WebAppContext context = new WebAppContext();
        context.setResourceBase(warpath);
        context.setConfigurations(new Configuration[]
        { 
            new AnnotationConfiguration(), 
            new WebInfConfiguration(), 
            new WebXmlConfiguration(), 
            new MetaInfConfiguration(), 
            new FragmentConfiguration(),
            new EnvConfiguration(), 
            new PlusConfiguration(), 
            new JettyWebXmlConfiguration() 
        });

        context.setContextPath("/");
        context.setParentLoaderPriority(true);
        server.setHandler(context);
        server.start();
        server.join();
    }
}

Upvotes: 1

outdev
outdev

Reputation: 5502

I guess your are trying to run jetty embeded in your application, and want it to serve a war file. Check this link http://www.eclipse.org/jetty/documentation/current/embedded-examples.html#embedded-one-webapp

Upvotes: 1

Related Questions