java 调用shell 生成文件的问题,好困惑,弄了很多天了

public Callback exeShell(final Map header, final Map map){
Callback rb= new CallbackImpl();

         String shellCommand = "python3 /home/vat/gfjj_report/makeexecl/report.py";
         logger.info("shellCommand:"+shellCommand);
          //String[] cmd = {"/bin/sh", "-c", shellCommand};
          String result = " ";
          BufferedReader br = null ;
          BufferedInputStream buis = null ;
          Process p;
            try {
                p = Runtime.getRuntime().exec(shellCommand);
                if(p.waitFor() != 0 ){
                    result += "没有进程号";
                }
                buis = new BufferedInputStream(p.getInputStream());
                br = new BufferedReader(new InputStreamReader(buis));
                String lineStr ;
                while ((lineStr = br.readLine()) != null){
                    result += lineStr;
                }
            } catch (Exception e) {
                e.printStackTrace();
                rb.setErrorCode("-1");
                rb.setErrorMessage("生成失败,错误信息:" + e.getMessage());
                return rb;
            }finally{
                if(br != null){
                    try {
                        br.close();
                        buis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                logger.info("ShellUtil.ExeShell =>"+ result);
            }
        return rb;
    }

其中类是这样写的,我有几个疑问。点击生成时不成功。
java.io.IOException: Cannot run program "python3": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at com.gingkoo.cust.server.GfjjCommonService.exeShell(GfjjCommonService.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.gingkoo.gui.data.server.command.executer.CommandExecutor.execute(CommandExecutor.java:136)
at com.gingkoo.gui.data.server.GUIGWTServer.updateData(GUIGWTServer.java:1888)
at com.gingkoo.gui.data.server.GUIGWTServer.updateData(GUIGWTServer.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1097)
at com.gingkoo.gal.web.filter.LoggerContextFilter.doFilter(l:211)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.io.IOException: CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 41 more
报以上的错,我的脚本是:python3 /home/vat/gfjj_report/makeexecl/report.py

我怀疑是不是因为本地测试的原因,因为调用的是远程的shell.在网上找了很多答案,发现都没有解决好。

2个回答

我在执行的脚本前加了cmd.exe /C ,后变成这样cmd.exe /C python3 /home/vat/gfjj_report/makeexecl/report.py。这样没有报错。有哪个亲兄弟知道
cmd.exe /C 是否与调动远程指令有关系。虽然没有报错,可是不明白,就是乱来啊!

没有人回答啊,哈哈。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问