开发者社区> 问答> 正文

tomcat 启动时如何加载jar代码?

tomcat想在 startup.bat 点击是先加载一个自定义的jar,在bat里改如何写?

展开
收起
落地花开啦 2016-05-30 11:45:15 3637 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    可以使用api控制tomcat启动,然后在里面定制一些自己的逻辑,下面是代码,可以参考参考

    import java.io.File;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
      
    import org.apache.catalina.Context;
    import org.apache.catalina.Engine;
    import org.apache.catalina.Host;
    import org.apache.catalina.startup.Embedded;
      
    public class TomcatService {
    private static Embedded tomcat;
     
     public static void main(String[] args) {
    // set the path for tomcat embed
    String path = "F:/myDaRepository/apache-tomcat-embed";
    Embedded tc = createTomcat(path);
    try {
    tc.start();
    System.out.println("Tomcat Server start over");
    //   tc.stop();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      
    private static Embedded createTomcat(String path) {
    // create Tomcat Server Instance
    tomcat = new Embedded();
    // set the tomcat home
    tomcat.setCatalinaHome(path);
    // create the Tomcat engine
    Engine engine = tomcat.createEngine();
    engine.setName("MyTestServer");
    // create the host
    Host host = tomcat.createHost("localhost", tomcat.getCatalinaHome()
    + "/webapps");
    // put the host into engine
    engine.addChild(host);
    engine.setDefaultHost(host.getName());
     
     String javayouPath = host.getAppBase() + "/CloudscapeDemo";
    if (!new File(javayouPath).exists()) {
    System.err.println("Please test if the javayou exists");
    return null;
    }
    System.out.println("javayouPath"+javayouPath);
    // create the context, and add it to the host
    Context ctxtJavayou = tomcat
    .createContext("/CloudscapeDemo", javayouPath);
    host.addChild(ctxtJavayou);
     
     tomcat.addEngine(engine);
    try {
    // put the Connector to Tomcat,listen the local visit from 127.0.0.1
    tomcat.addConnector(tomcat.createConnector(InetAddress
    .getByName("127.0.0.1"), 8080, false));
    } catch (UnknownHostException e) {
    System.err
    .println("can not bind tomcat Server to the localhost 127.0.0.1:8080;test the host is free");
    e.printStackTrace();
    tomcat = null;
    }
    return tomcat;
    }
     
    public void shutdown() throws Exception {
    tomcat.stop();
    }
    }
    2019-07-17 19:20:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Apache Tomcat 的云原生演进 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载