简体   繁体   English

无法使用Java Servlet在cmd中打开或执行命令

[英]Can't open or execute command in cmd using java servlet

Want to execute echot.BAT file using java servlet. 想要使用Java Servlet执行echot.BAT文件。 i have used following code. 我用下面的代码。

Process croppingProcess =  Runtime.getRuntime()
                .exec("C:Windows/system32/cmd /c echot.BAT", null, new File("C:restapi_bat/echot"));
                int processOutput = croppingProcess.waitFor();
                if(processOutput == 0){
                    success = true;
            //do your work here
        }

but getting the error 但是得到错误

"C:Windows/system32/cmd" (in directory "C:restapi_bat\\echot"): CreateProcess error=267, The directory name is invalid “ C:Windows / system32 / cmd”(在目录“ C:restapi_bat \\ echot”中):CreateProcess错误= 267,目录名称无效

file echot.BAT in C:\\restapi_bat C:\\ restapi_bat中的文件echot.BAT
Note: using tomcat 7 注意:使用tomcat 7

Instead of using C:Windows/system32/cmd you should use C:\\\\Windows\\\\system32\\\\cmd, because Windows separates with \\. 不应使用C:Windows / system32 / cmd,而应使用C:\\\\ Windows \\\\ system32 \\\\ cmd,因为Windows用\\分隔。 The / Separator is in Linux. /分隔符在Linux中。 And you have to Escape the \\, so use \\\\. 而且您必须转义\\,所以请使用\\\\。

Using escape and Start, bat executed the solution as follow 使用Escape和Start,bat按照以下方式执行了解决方案

Process croppingProcess =  Runtime.getRuntime()
                .exec("C:\\Windows\\System32\\cmd.exe /C Start C:\\restapi_bat\\echot ");
                int processOutput = croppingProcess.waitFor();
                if(processOutput == 0){
                    success = true;
            //do your work here
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM