Problem with Classpath Jetty Server Classpath

I am trying to deploy a web application on an embedded Jetty server. My application works fine locally on a Windows environment with the code below, but when I deploy it as a JAR file on Linux Server, it looks like my web.xml file was not found. Is there anything I need to change in the Descriptor or ResourceBase fields below before creating the JAR?

static void startJetty() { try { Server server = new Server(9090); WebAppContext context = new WebAppContext(); context.setDescriptor("/WEB-INF/web.xml"); context.setResourceBase("../DemoWithMultiChannels/src/"); context.setContextPath("/"); context.setParentLoaderPriority(true); server.setHandler(context); System.out.println("Starting Server!"); server.start(); 
+6
source share
4 answers

I had the same problem and found a solution:
It worked fine when I ran "java -jar ..." from the terminal, but when I spawned it from another project, web.xml was not raised.

The reason is that the web.xml path was wrong, it was relative to the original project, as a result I do the following:

 context.setDescriptor(Launch.class.getResource("/WEB-INF/web.xml").toString()); 

If you are not using the resource, you are simply reading the regular file inside your src folder, not the one inside the .jar

+6
source

Here's how to do it.

First, in your pom.xml, declare where the webapp folder is located:

  <build> <resources> <resource> <directory>src/main</directory> </resource> </resources> 

Here is the tree of my src / main directory:

 ├── java │  └── com │  └── myco │  └── myapp │  └── worker │  ├── App.java | ... ├── resources │  ├── log4j.properties │  └── version.properties └── webapp ├── index.html ├── index.jsp ├── lib │  ├── inc_meta.jsp │  └── inc_navigation.jsp ├── query.html ├── scripts │  ├── angular.min.js │  └── bootstrap.min.css ├── showresults.jsp ├── status.jsp └── WEB-INF └── web.xml 

Add the Maven Shade plugin to the pom.xml file:

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <finalName>uber-${artifactId}-${version}/finalName> </configuration> </plugin> 

Then run Jetty as follows:

 public static void startJetty() throws Exception { logger.info("starting Jetty..."); Server server = new Server(8080); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/"); /* Important: Use getResource */ String webxmlLocation = App.class.getResource("/webapp/WEB-INF/web.xml").toString(); webAppContext.setDescriptor(webxmlLocation); /* Important: Use getResource */ String resLocation = App.class.getResource("/webapp").toString(); webAppContext.setResourceBase(resLocation); webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext); server.start(); server.join(); } 

An important part is the use of <YourApp>.class.getResource(<your location>) , which will give the path to the files inside the jar. The wrong way would be to do it like this: webContext.setDescriptor("WEB-INF/web.xml"); which gives the path to the file system.

Then create a package

 $mvn clean package 

A uber-jar file is created and contains the webapp directory, which was declared as a resource.

Move the jar anywhere or on the production server and execute it as follows:

 $ java -jar myjettyembededwithwebxmlandhtmljspfile.jar 
+4
source

Deploy the embedded Jetty as follows:

Main class

 public static void main(String[] args) throws Exception { Server server = new Server(8085); WebAppContext webContext = new WebAppContext(); webContext.setDescriptor("WEB-INF/web.xml"); webContext.setResourceBase("src/sim/ai/server/start"); webContext.setServer(server); webContext.setParentLoaderPriority(true); server.setHandler(webContext); server.start(); server.join(); } 

web.xml

 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>sim.ai.server.start</display-name> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>sim.ai.server.start</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> 

Create the WEB_INF folder in the same folder as the jar file; copy web.xml to WEB_INF , for example:

 sim/light.jar sim/WEB-INF/web.xml 
+2
source

All Articles