Java – start the embedded jetty server for jar files
What I want to do is build an executable jar file containing my project I have included its dependencies next to it and in the jar file, so my directory list looks like this:
I know and am sure that my problem does not come from dependencies, because my application runs normally until the moment I start jetty embedded
The code I use to start jetty is as follows:
public class ServerExecutionGoal implements ExecutionGoal {
private final static Logger logger = Logger.getLogger(ServerExecutionGoal.class);
private WebAppContext getWebAppContext() throws IOException {
WebAppContext context = new WebAppContext();
System.out.println("context = " + context);
context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm());
context.setContextPath("/");
context.setLogger(new StdErrLog());
return context;
}
@Override
public void execute(Map<String,Object> stringObjectMap) throws ExecutionTargetFailureException {
logger.info("Instantiating target server ...");
final Server server = new Server(8089);
final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
try {
handlerCollection.setHandlers(new Handler[]{new RequestLogHandler(),getWebAppContext()});
} catch (IOException e) {
throw new ExecutionTargetFailureException("Could not create web application context",e);
}
server.setHandler(handlerCollection);
try {
logger.info("Starting server on port 8089 ...");
server.start();
server.join();
logger.info("Server started. Waiting for requests.");
} catch (Exception e) {
throw new ExecutionTargetFailureException("Failed to properly start the web server",e);
}
}
public static void main(String[] args) throws ExecutionTargetFailureException {
new ServerExecutionGoal().execute(null);
}
}
I can verify that the "webapp" folder is correctly relocated to / webapp in my jar and run the code through my IDE (IntelliJ idea 11) context setResourceBase(new PathMatchingResourcePatternResolver(). getResource(“classpath:/ webapp”) ). getURL(). Toexternalform()) is effectively mapped to related resources In addition, I can prove that it solves problems similar to:
jar:file:~/Projects/Java/web-app/out/web-app.jar!/webapp
And accessible (I've read it)
However, when I start the main method of the application and jetty starts, on http: / / localhost: 8089, I get the following:
What's wrong with me?
I know by setting "resourcebase" to "." The address "http: / / localhost: 8089" will act as the interface for the "~ / projects / Java / web app / out /" location, where I can see the list of all jar files, including "web app. Jar". Click I provide to download it
I have read the following questions and the answer does not apply:
>Embedded jetty application not working from jar: class. Getprotectiondomain() gets NullPointerException Getcodesource() resolves to null. > Embedded jetty webappcontext filepermission issue: not only is this not answered, but the situation obviously doesn't apply because I don't have a license question (the fact that I can get a file list should prove this). > Embedding jetty server problems: there is no answer, nor does it apply, because I don't have any dependency problems (as I pointed out in the comments above)
I think I should somehow enable the jetty access / webapp folder, which is located in my Src / main / resources / directory and bundled into my web application Should I abandon the bundled web application and deploy the exploded context path somewhere that jetty can access (this is not desirable at all because it causes a lot of problems for me and my customers)
Solution
I'm not sure if jetty's webappcontext can be used with pathmatching resourcepatternresolver!
In one of my projects, I wrote my own javax servlet. Filter to solve this problem. It loads the requested resource through class #getresourceasstream Class #getresourceasstream reads resources from inside jar and resource path (such as Maven)
I hope this tip will help you. Cheers, Tiro
