嵌入式(embed)Tomcat

6 篇文章 0 订阅

嵌入式(embed)Tomcat的使用

本来想用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中。

按这上面的说法抄一个吧,代码很简单:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) throws Exception {  
  4.   
  5.         String webappDirLocation = "src/main/webapp/";  
  6.         Tomcat tomcat = new Tomcat();  
  7.   
  8.         //The port that we should run on can be set into an environment variable  
  9.         //Look for that variable and default to 8080 if it isn't there.  
  10.         String webPort = System.getenv("PORT");  
  11.         if(webPort == null || webPort.isEmpty()) {  
  12.             webPort = "8080";  
  13.         }  
  14.   
  15.         tomcat.setPort(Integer.valueOf(webPort));  
  16.   
  17.         tomcat.addWebapp("/"new File(webappDirLocation).getAbsolutePath());  
  18.         System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());  
  19.   
  20.         tomcat.start();  
  21.         tomcat.getServer().await();  
  22.     }  
  23. }  
 按照该文章的步骤完全可以得出正确结果,但是他的方法生成了一个很复杂的bat文件,搞半天不还是搞出了一个和tomcat一模一样的东西来。但是如果直接run这个main方法,会报下面的错误:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. org.apache.jasper.JasperException: java.lang.IllegalStateException: No Java compiler available  
  2.     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)  
  3.     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:391)  
  4.     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)  
  5.     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)  
  6.     javax.servlet.http.HttpServlet.service(HttpServlet.java:727)  
  7.     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)  

追踪源码,发现是在load类org.apache.jasper.compiler.JDTCompiler的时候出了问题。但是这个类确实已经存在于编译路径中了,咋整?直接在main方法中在启动tomcat之前load一把试试:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Class.forName("org.apache.jasper.compiler.JDTCompiler");  
结果:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.compiler.env.INameEnvironment  
  2.     at java.net.URLClassLoader$1.run(Unknown Source)  
  3.     at java.net.URLClassLoader$1.run(Unknown Source)  
  4.     at java.security.AccessController.doPrivileged(Native Method)  
  5.     at java.net.URLClassLoader.findClass(Unknown Source)  
  6.     at java.lang.ClassLoader.loadClass(Unknown Source)  
  7.     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)  
  8.     at java.lang.ClassLoader.loadClass(Unknown Source)  
  9.     ... 3 more  

发现,其实是没有上面的这个类。从网上找到,并加入到编译路径下就ok了:


接下来,把webapp换成真实项目的路径,该项目中使用了spring、Hibernate等工具:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) throws Exception {  
  4.   
  5.         String webappDirLocation = "src/main/webapp/";  
  6.         Tomcat tomcat = new Tomcat();  
  7.   
  8.         //The port that we should run on can be set into an environment variable  
  9.         //Look for that variable and default to 8080 if it isn't there.  
  10.         String webPort = System.getenv("PORT");  
  11.         if(webPort == null || webPort.isEmpty()) {  
  12.             webPort = "8080";  
  13.         }  
  14.   
  15.         tomcat.setPort(Integer.valueOf(webPort));  
  16.   
  17. //      tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());  
  18.         tomcat.addWebapp("/nea""D:\\apache-tomcat-7.0.34\\webapps\\nea");  
  19.         System.out.println("configuring app with basedir: D:\\apache-tomcat-7.0.34\\webapps\\nea");  
  20.   
  21.         tomcat.start();  
  22.         tomcat.getServer().await();  
  23.     }  
  24. }  
重新启动,发现spring mvc 的路径映射、Hibernate数据库初始化等动作正常执行了。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值