2

I have a Spring+Hibernate application, which I compile to *.war file and deploy it to Tomcat. This works for me as developer, but:

Is there a way to run that application in some user's computer, that has Java installed, but not tomcat installed?

I would even accept the solution, which uses somekinda package that actually runs the servlet container and deploys the application to user's computer, but I don't want that user must install container and configure it etc etc.

Any suggestions?

EDIT:

Basically I want user to run my web application from an executable, without having to install tomcat or other tools.

Jaanus
  • 16,161
  • 49
  • 147
  • 202
  • `war=web archive` though you can always develop desktop application using hibernate and spring as both do not require container always – Umesh Awasthi Sep 13 '12 at 15:52
  • @UmeshAwasthi I don't have to use *.war , but I want to keep my source code as it is, using spring+hibernate and annotations and stuff – Jaanus Sep 13 '12 at 15:52
  • To be clear: what do you want to run on the user's computer, the web app, or some variation of your code that runs from a `main()` method (and presumably doesn't need to respond to HTTP requests)? – matt b Sep 13 '12 at 16:06
  • @mattb I mean that, I am developing my application using all web technologies.spring web, mvc etc etc, and myself running it via Tomcat, but I want to give user just an executable thing, that he can run, without installing tomcat or other tools, hope this explains – Jaanus Sep 13 '12 at 17:17
  • Just curious, what's the db you plan to use? – vector Sep 13 '12 at 17:53
  • @vector : i would be using external database solution, postgres possibly, so my application from users desktop connects to database that is located somewhere else, so all applications use same database – Jaanus Sep 13 '12 at 17:57

4 Answers4

4

You have a few options:

  1. if you are distributing the source code to the user, and they have maven installed, you can just run mvn jetty:run or mvn tomcat:run to build the application locally and run it within a servlet container started by the Maven plugin.

  2. You can embed Tomcat or embed Jetty in your application, so that running a main() method in your app launches a servlet container listening on a certain port and runs your application. This makes it possible to package your entire application as a single .jar file and have it be run with java -jar your.jar.

Community
  • 1
  • 1
matt b
  • 138,234
  • 66
  • 282
  • 345
  • @Jaanus in that case, embedding Tomcat in your application sounds like the way to go. What this does is that instead of just distributing your application as a `.war` file to be run inside a Tomcat installation, you include the necessary Tomcat setup/config *in your code*. – matt b Sep 13 '12 at 18:03
  • Is it difficult to configure that solution, when running standard Spring application : http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/ , any example about it?, I found some embed tomcat tutorials before, but they seem like alot of code editing and not simple at all.. – Jaanus Sep 13 '12 at 18:22
  • I linked to two tutorials in my answer, both of which look dead simple. A quick google search turns up a bunch more tutorials, and it also looks like embedding has become even easier in Tomcat 7. See http://arhipov.blogspot.com/2011/03/embedded-tomcat-minimal-version.html and https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat (skip everything related to Heroku). Of course with Spring MVC, what you want to configure the embedded server to do is call DispatcherServlet. – matt b Sep 13 '12 at 18:49
3

The Winstone servlet container allows for embedding the war-file inside the winstone jar, resulting in a single jar deployment which can be run either with "java -jar foo.jar" or as a clickable jar.

Jenkins/Hudson uses this. We've used it with some classpath trickery to use an exploded war.

See http://winstone.sourceforge.net/#embedding for details.

This is most likely the most elegant way to do this at the moment.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

You can certainly run a spring +hibernate application from command line, using the ClassPathXmlApplicationContext to load the spring configuration file in your main method to initialize the spring container and rest of the wiring.

However, to run a web application written using servlets or similar paradigms that use Java Servlet Specification, then you need a servlet container like Tomcat, AFAIK.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • This does not run a servlet container, and the webapp would be somewhat useless without one. – matt b Sep 13 '12 at 15:58
  • Not sure why you are referring to "servlet container". I am referring to "Spring container" that contains all the beans instantiated. – Vikdor Sep 13 '12 at 16:00
  • I'm assuming from the OP's initial question that the application is a web application and needs to run in a servlet container for it to be meaningfully run. It's not clear if he/she is attempting to run the webapp without requiring user's to understand how to install a servlet container, or if the code that is currently packaged as a `war` also contains a `main()` entry point that would launch a regular, non-web application. – matt b Sep 13 '12 at 16:02
  • From his response to the comment on the question, I understood that OP wants to run it from command line, but I may be wrong! – Vikdor Sep 13 '12 at 16:04
0

Not a direct example, but potentially useful example of something similar: http://www.zimbra.com/products/desktop.html and http://www.zimbra.com/products/zimbra-open-source.html

vector
  • 7,334
  • 8
  • 52
  • 80