{"id":657,"date":"2016-07-23T14:07:00","date_gmt":"2016-07-23T17:07:00","guid":{"rendered":"http:\/\/www.nextframework.org\/site\/?p=657"},"modified":"2016-07-23T23:38:08","modified_gmt":"2016-07-24T02:38:08","slug":"using-embedded-tomcat","status":"publish","type":"post","link":"https:\/\/www.nextframework.org\/site\/657\/tutorial\/using-embedded-tomcat","title":{"rendered":"Using embedded Tomcat"},"content":{"rendered":"<p>By using embedded tomcat, your Next application becomes executable. This way, you don&#8217;t need to deploy an installation of tomcat to use Next.<br \/>\n<!--more--><\/p>\n<p>What you have to do:<\/p>\n<ul>\n<li>Add tomcat libraries to your project<\/li>\n<li>Create a launcher class<\/li>\n<\/ul>\n<p>\n<b>Note:<\/b> You can create a Next application the standard way. Download next zip file; extract and run build-install.xml.<br \/>\n<i>Be sure you have the console opened (in eclise: window &gt; show view &gt; console). You will need to create a folder lib\/src in your project if using next 3.8.1 or older.<\/i>\n<\/p>\n<p><BR><\/p>\n<h3>Add tomcat libraries to your project<\/h3>\n<p><BR><\/p>\n<p>\n1) Open the <strong>ivy.xml<\/strong> file and add tomcat libraries inside <strong>dependencies<\/strong> tag.<\/p>\n<pre style=\"border: 1px solid gray;\">  &lt;dependency org=\"org.apache.tomcat.embed\" name=\"tomcat-embed-core\"         rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat.embed\" name=\"tomcat-embed-logging-juli\" rev=\"8.5.2\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat.embed\" name=\"tomcat-embed-jasper\"       rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat\"       name=\"tomcat-jasper\"             rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat\"       name=\"tomcat-jsp-api\"            rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat\"       name=\"tomcat-jasper-el\"          rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n  &lt;dependency org=\"org.apache.tomcat\"       name=\"tomcat-el-api\"             rev=\"8.5.3\" conf=\"lib\"\/&gt;\r\n\t\r\n  &lt;dependency org=\"org.eclipse.jdt.core.compiler\" name=\"ecj\"                 rev=\"4.5.1\" conf=\"lib\"\/&gt;\r\n<\/pre>\n<p>2) Right click on build.xml and select <strong>Run As &gt; Ant Build &#8230;<\/strong>.<br \/>\n3) Select target <strong>Retrieve Ivy Dependencies<\/strong> (check <em>Hide internal targets<\/em> to filter targets if it is hard to find).<br \/>\n4) Run. This will download the libraries. Refresh (F5) your project and you will see the libraries under lib folder.<br \/>\n5) Select all the libraries and right click &gt; <strong>Build Path &gt; Add to build path<\/strong>.<br \/>\n6) The tomcat libraries are now configured.\n<\/p>\n<p><BR><\/p>\n<h3>Create a launcher class<\/h3>\n<p><BR><\/p>\n<p>\nCreate a class under src folder<\/p>\n<pre style=\"border: 1px solid gray;\">package launch;\r\n\r\nimport java.io.File;\r\n\r\nimport org.apache.catalina.WebResourceRoot;\r\nimport org.apache.catalina.core.StandardContext;\r\nimport org.apache.catalina.startup.Tomcat;\r\nimport org.apache.catalina.webresources.DirResourceSet;\r\nimport org.apache.catalina.webresources.StandardRoot;\r\n\r\npublic class Main {\r\n\r\n\tprivate static final String CLASSES_DIR = \"build\";\r\n\tprivate static final String WEBAPP = \"WebContent\/\";\r\n\tprivate static final String DEFAULT_PORT = \"8080\";\r\n\r\n\tpublic static void main(String[] args) throws Exception {\r\n\r\n        String webappDirLocation = WEBAPP;\r\n        Tomcat tomcat = new Tomcat();\r\n\r\n        \/\/The port that we should run on can be set into an environment variable\r\n        \/\/Look for that variable and default to 8080 if it isn't there.\r\n        String webPort = System.getenv(\"PORT\");\r\n        if(webPort == null || webPort.isEmpty()) {\r\n            webPort = DEFAULT_PORT;\r\n        }\r\n\r\n        tomcat.setPort(Integer.valueOf(webPort));\r\n\r\n        StandardContext ctx = (StandardContext) tomcat.addWebapp(\"\/\", new File(webappDirLocation).getAbsolutePath());\r\n        System.out.println(\"configuring app with basedir: \" + new File(\".\/\" + webappDirLocation).getAbsolutePath());\r\n\r\n        \/\/ Declare an alternative location for your \"WEB-INF\/classes\" dir\r\n        \/\/ Servlet 3.0 annotation will work\r\n        File additionWebInfClasses = new File(CLASSES_DIR);\r\n        WebResourceRoot resources = new StandardRoot(ctx);\r\n        resources.addPreResources(new DirResourceSet(resources, \"\/WEB-INF\/classes\",\r\n                additionWebInfClasses.getAbsolutePath(), \"\/\"));\r\n        ctx.setResources(resources);\r\n\r\n        tomcat.start();\r\n        tomcat.getServer().await();\r\n    }\r\n}\r\n<\/pre>\n<p>The constants CLASSES_DIR and WEBAPP are configured with eclipse defaults. If you are compiling your classes to a different folder, change CLASSES_DIR. If your web files are in a different folder, change WEBAPP.<BR><\/p>\n<h3>Run your application<\/h3>\n<p><BR><br \/>\nYou can now run your application by executing Main class (right click &gt; <strong>Run As &gt; Java Application<\/strong>).<\/p>\n<p>To access your application, go to <strong>localhost:8080<\/strong> in your browser (no application name on path).\n<\/p>\n<p><BR><br \/>\n<em><strong>Note:<\/strong><\/em><\/p>\n<div style=\"left-padding: 10px;\">\nThe dependencies on this tutorial are the minimum requirement to run embedded tomcat. You may need other libraries if you use other resources. If you get a class not found exception, you can check what are the dependencies required. For example you can see that <em>tomcat-jasper<\/em> depends on <em>org.eclipse.jdt.core.compiler . ecj<\/em> in <a href=\"http:\/\/mvnrepository.com\/artifact\/org.apache.tomcat\/tomcat-jasper\/8.5.3\">http:\/\/mvnrepository.com\/artifact\/org.apache.tomcat\/tomcat-jasper\/8.5.3<\/a><\/p>\n<p>Be sure to have the console view opened. Any logs appear on this view. (In eclise select window &gt; show view &gt; console)<\/p>\n<p>It is always recommended that you use a JDK in your IDE. Download and install a jdk. In eclipse, and you can configure it in <em>window > preferences<\/em>. The filter by <em>JREs<\/em>. Add the jdk in <em>Installed JREs<\/em> and also configure <em>Execution Environments<\/em>.\n<\/div>\n<p>Credits to https:\/\/devcenter.heroku.com\/articles\/create-a-java-web-application-using-embedded-tomcat#prerequisites<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By using embedded tomcat, your Next application becomes executable. This way, you don&#8217;t need to deploy an installation of tomcat to use Next.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[36,38,37],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-embedded","tag-executable","tag-tomcat"],"_links":{"self":[{"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/comments?post=657"}],"version-history":[{"count":11,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/posts\/657\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/media?parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/categories?post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nextframework.org\/site\/wp-json\/wp\/v2\/tags?post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}