tomcat想在 startup.bat 点击是先加载一个自定义的jar,在bat里改如何写?
可以使用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();
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。