我写了一个java程序,但在这个java程序开始运行时,需要首先启动Tomcat。于是我在程序开始时调用Tomcat的bin目录的批处理命令startup.bat,并且成功的启动了Tomcat。但问题是Tomcat启动时会弹出两个命令提示符窗口,因为我写的这个java程序是个图形用户界面窗口,所以,不能出现命令提示符窗口。不知道有没有办法可以让这两个命令提示符窗口在后台运行?

解决方案 »

  1.   

    别用startup了,试试用catalina run命令;
    大概是这样:String cmd = "cmd /c C:/Tomcat6.0/bin>catalina run";
    Runtime.getRuntime().exec(cmd);
      

  2.   

    public class RunWebApplicationTomcat {    private String path = null;
        private Embedded container = null;
        private Log logger = LogFactory.getLog(getClass());    /**
         * The directory to create the Tomcat server configuration under.
         */
        private String catalinaHome = "tomcat";    /**
         * The port to run the Tomcat server on.
         */
        private int port = 8089;    /**
         * The classes directory for the web application being run.
         */
        private String classesDir = "target/classes";    /**
         * The web resources directory for the web application being run.
         */
        private String webappDir = "mywebapp";    /**
         * Creates a single-webapp configuration to be run in Tomcat on port 8089. If module name does
         * not conform to the 'contextname-webapp' convention, use the two-args constructor.
         * 
         * @param contextName without leading slash, for example, "mywebapp"
         * @throws IOException
         */
        public RunWebApplicationTomcat(String contextName) {
            Assert.isTrue(!contextName.startsWith("/"));
            path = "/" + contextName;
        }    /**
         * Starts the embedded Tomcat server.
         * 
         * @throws LifecycleException
         * @throws MalformedURLException if the server could not be configured
         * @throws LifecycleException if the server could not be started
         * @throws MalformedURLException
         */
        public void run(int port) throws LifecycleException, MalformedURLException {
            this.port = port;
            // create server
            container = new Embedded();
            container.setCatalinaHome(catalinaHome);
            container.setRealm(new MemoryRealm());        // create webapp loader
            WebappLoader loader = new WebappLoader(this.getClass().getClassLoader());        if (classesDir != null) {
                loader.addRepository(new File(classesDir).toURI().toURL().toString());
            }        // create context
            // TODO: Context rootContext = container.createContext(path, webappDir);
            Context rootContext = container.createContext(path, webappDir);
            rootContext.setLoader(loader);
            rootContext.setReloadable(true);        // create host
            // String appBase = new File(catalinaHome, "webapps").getAbsolutePath();
            Host localHost = container.createHost("localHost", new File("target").getAbsolutePath());
            localHost.addChild(rootContext);        // create engine
            Engine engine = container.createEngine();
            engine.setName("localEngine");
            engine.addChild(localHost);
            engine.setDefaultHost(localHost.getName());
            container.addEngine(engine);        // create http connector
            Connector httpConnector = container.createConnector((InetAddress) null, port, false);
            container.addConnector(httpConnector);        container.setAwait(true);        // start server
            container.start();        // add shutdown hook to stop server
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    stopContainer();
                }
            });
        }
        /**
         * Stops the embedded Tomcat server.
         */
        public void stopContainer() {
            try {
                if (container != null) {
                    container.stop();
                }
            } catch (LifecycleException exception) {
                logger.warn("Cannot Stop Tomcat" + exception.getMessage());
            }
        }    public String getPath() {
            return path;
        }    public void setPath(String path) {
            this.path = path;
        }    public static void main(String[] args) throws Exception {
            RunWebApplicationTomcat inst = new RunWebApplicationTomcat("mywebapp");
            inst.run(8089);
        }    public int getPort() {
            return port;
        }}
      

  3.   

    tomcat 的 bin 目录下有个 service.bat 文件,到控制台中执行:service install test 命令的话,会在服务管理器中创建一个名为 Apache Tomcat test 的服务项,以后通过服务管理器启动就不会再有黑窗口了。如果不需要这个服务的话,可以使用 service remove test 将其从服务管理器中删除。
      

  4.   

    我现在用的解压版的,没有tomat6w.exe,也没有tomat.exe,我启动是是单击bin目录下的startup.bat,停止是单击shutdown.bat,运行窗口一闪而过,现我想要运行窗口显示出来,该怎么设置呀?