Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tomcat无法执行外部java程序_Java_Jsp_Tomcat - Fatal编程技术网

Tomcat无法执行外部java程序

Tomcat无法执行外部java程序,java,jsp,tomcat,Java,Jsp,Tomcat,我对JSP/Tomcat完全是个新手,在很大程度上对Java也是如此。我要做的是——当用户单击按钮/访问URL时,我想启动一个Java程序(它需要一些命令行参数) 我很容易做到 Runtime.exec("C:\\Python27\\python.exe test.py") 或 这个很好用。其中HelloWorld.class只打印“HelloWorld” 然而,当我尝试使用命令行参数的java程序时,GET请求只是挂起,不做任何事情。我不知道要查找什么日志,也不知道这里可能有什么问题。在花

我对JSP/Tomcat完全是个新手,在很大程度上对Java也是如此。我要做的是——当用户单击按钮/访问URL时,我想启动一个Java程序(它需要一些命令行参数)

我很容易做到

Runtime.exec("C:\\Python27\\python.exe test.py") 

这个很好用。其中HelloWorld.class只打印“HelloWorld”

然而,当我尝试使用命令行参数的java程序时,GET请求只是挂起,不做任何事情。我不知道要查找什么日志,也不知道这里可能有什么问题。在花了两天时间尝试各种事情之后,我现在就要放弃了

Runtime.exec("java -cp %CLASSPATH%;C:\\devprojects Run --username Blah --password Foo");
Tomcat最终以什么用户的身份运行这个java程序?我可以成为管理员吗?这是在Windows 2008上,UAC会干扰吗

我不能在这里修改Run.class,我必须按原样使用命令行参数运行它


请给出建议。

一个想法:您依赖于命令行的默认标记化作为一个完整的
字符串,而它并没有像您所期望的那样解析最后一个字符串。相反,您应该使用此方法的形式,在您自己切碎命令行后,使用
字符串[]

或者,它正在等待您的输入,或者等待您读取其输出。这可以解释这个问题。在internet上搜索流和
Runtime.exec()
的危险


请考虑改为
ProcessBuilder

还要记住,您必须确保执行的文件不会“永远”运行,并且 如果需要传递一些参数,可以使用以下方法:

    static String startProcess(String command, String dir) throws IOException {
        StringBuffer ret = new StringBuffer();
        String[] comm = new String[3];
        comm[0] = COMMAND_INTERPRETER[0];
        comm[1] = COMMAND_INTERPRETER[1];
        comm[2] = command;
        long start = System.currentTimeMillis();
        try {
            //Start process
            Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir));
            //Get input and error streams
            BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream());
            BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream());
            boolean end = false;
            while (!end) {
                int c = 0;
                while ((ls_err.available() > 0) && (++c <= 1000)) {
                    ret.append(conv2Html(ls_err.read()));
                }
                c = 0;
                while ((ls_in.available() > 0) && (++c <= 1000)) {
                    ret.append(conv2Html(ls_in.read()));
                }
                try {
                    ls_proc.exitValue();
                    //if the process has not finished, an exception is thrown
                    //else
                    while (ls_err.available() > 0)
                        ret.append(conv2Html(ls_err.read()));
                    while (ls_in.available() > 0)
                        ret.append(conv2Html(ls_in.read()));
                    end = true;
                }
                catch (IllegalThreadStateException ex) {
                    //Process is running
                }
                //The process is not allowed to run longer than given time.
                if (System.currentTimeMillis() - start > MAX_PROCESS_RUNNING_TIME) 
//this is very important
{
                    ls_proc.destroy();
                    end = true;
                    ret.append("!!!! Process has timed out, destroyed !!!!!");
                }
                try {
                    Thread.sleep(50);
                }
                catch (InterruptedException ie) {}
            }
        }
        catch (IOException e) {
            ret.append("Error: " + e);
        }
        return ret.toString();
    }
staticstringstartprocess(String命令,stringdir)抛出IOException{
StringBuffer ret=新的StringBuffer();
字符串[]comm=新字符串[3];
comm[0]=命令_解释器[0];
comm[1]=命令_解释器[1];
comm[2]=命令;
长启动=System.currentTimeMillis();
试一试{
//启动过程
进程ls_proc=Runtime.getRuntime().exec(comm,null,新文件(dir));
//获取输入和错误流
BufferedInputStream ls_in=新的BufferedInputStream(ls_proc.getInputStream());
BufferedInputStream ls_err=新的BufferedInputStream(ls_proc.getErrorStream());
布尔结束=假;
当(!结束){
int c=0;
而((ls_err.available()>0)和(&++c0)和(&++c0)
ret.append(conv2Html(ls_err.read());
while(ls_in.available()>0)
ret.append(conv2Html(ls_in.read());
结束=真;
}
捕获(IllegalThreadStateException ex){
//进程正在运行
}
//进程的运行时间不允许超过给定的时间。
if(System.currentTimeMillis()-start>MAX\u进程\u运行\u时间)
//这是非常重要的
{
ls_proc.destroy();
结束=真;
ret.append(!!!!进程已超时,已销毁!!!);
}
试一试{
睡眠(50);
}
捕获(中断异常)
}
}
捕获(IOE异常){
ret.append(“错误:+e”);
}
返回ret.toString();
}

这是一个非常糟糕的主意。最好创建一个合适的servlet。我同意这是一个糟糕的主意。但这只是原型代码,直到我们雇佣了有相关经验的人。感谢ProcessBuilder和String[]提示。这是一个问题。另一个问题是生成的子进程在ErrorStream上抛出了一些主进程没有排出的内容。再次感谢。
    static String startProcess(String command, String dir) throws IOException {
        StringBuffer ret = new StringBuffer();
        String[] comm = new String[3];
        comm[0] = COMMAND_INTERPRETER[0];
        comm[1] = COMMAND_INTERPRETER[1];
        comm[2] = command;
        long start = System.currentTimeMillis();
        try {
            //Start process
            Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir));
            //Get input and error streams
            BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream());
            BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream());
            boolean end = false;
            while (!end) {
                int c = 0;
                while ((ls_err.available() > 0) && (++c <= 1000)) {
                    ret.append(conv2Html(ls_err.read()));
                }
                c = 0;
                while ((ls_in.available() > 0) && (++c <= 1000)) {
                    ret.append(conv2Html(ls_in.read()));
                }
                try {
                    ls_proc.exitValue();
                    //if the process has not finished, an exception is thrown
                    //else
                    while (ls_err.available() > 0)
                        ret.append(conv2Html(ls_err.read()));
                    while (ls_in.available() > 0)
                        ret.append(conv2Html(ls_in.read()));
                    end = true;
                }
                catch (IllegalThreadStateException ex) {
                    //Process is running
                }
                //The process is not allowed to run longer than given time.
                if (System.currentTimeMillis() - start > MAX_PROCESS_RUNNING_TIME) 
//this is very important
{
                    ls_proc.destroy();
                    end = true;
                    ret.append("!!!! Process has timed out, destroyed !!!!!");
                }
                try {
                    Thread.sleep(50);
                }
                catch (InterruptedException ie) {}
            }
        }
        catch (IOException e) {
            ret.append("Error: " + e);
        }
        return ret.toString();
    }