本来想用InstallAnyWhere来制作一个安装文件,里面包含一个tomcat,结果发现InstallAnyWhere一时半会儿可能学不下来。我的目的是想用Java SWT做一个壳子,内嵌一个浏览器,这个浏览器访问tomcat应用,让整个程序看起来像是一个CS架构的。我需要的功能倒是不复杂,想想能不能自己实现一个tomcat。Tomcat有没有提供这样的功能呢,不小心搜索了一下“嵌入式Tomcat”,还真有,并且在Tomcat5的时候就已经支持了:https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat#create-your-pom-xml
他用的maven,直接到官网上下载embed版的压缩包下来解压放到自己项目的lib目录中,并添加到buildpath中。
按这上面的说法抄一个吧,代码很简单:
public class Main { public static void main(String[] args) throws Exception { String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); //The port that we should run on can be set into an environment variable //Look for that variable and default to 8080 if it isn't there. String webPort = System.getenv("PORT"); if(webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath()); tomcat.start(); tomcat.getServer().await(); } }按照该文章的步骤完全可以得出正确结果,但是他的方法生成了一个很复杂的bat文件,搞半天不还是搞出了一个和tomcat一模一样的东西来。但是如果直接run这个main方法,会报下面的错误:
org.apache.jasper.JasperException: java.lang.IllegalStateException: No Java compiler available org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:391) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Class.forName("org.apache.jasper.compiler.JDTCompiler");结果:
Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.compiler.env.INameEnvironment at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 3 more
发现,其实是没有上面的这个类。从网上找到,并加入到编译路径下就ok了:
接下来,把webapp换成真实项目的路径,该项目中使用了spring、Hibernate等工具:
public class Main { public static void main(String[] args) throws Exception { String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); //The port that we should run on can be set into an environment variable //Look for that variable and default to 8080 if it isn't there. String webPort = System.getenv("PORT"); if(webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); // tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); tomcat.addWebapp("/nea", "D:\\apache-tomcat-7.0.34\\webapps\\nea"); System.out.println("configuring app with basedir: D:\\apache-tomcat-7.0.34\\webapps\\nea"); tomcat.start(); tomcat.getServer().await(); } }重新启动,发现spring mvc 的路径映射、Hibernate数据库初始化等动作正常执行了。