调用jee时,bash脚本不起作用

时间:2016-07-26 09:56:03

标签: linux bash java-ee eclipse-jee

我有一个需要参数的脚本bash。

当我直接从putty调用它时,它可以工作,我从我的jee程序中做它不起作用并且没有向我显示任何错误。

这是我的java代码:

String[] cmdArray = {"sudo", "ssh", "-tt", "root@89.40.112.248", "/root/dve", "-l", "89.40.112.120,89.40.112.248", "you.mp4", "-s",".teeeest.avi" };

List<ObjectNode> listFileNode = new ArrayList<ObjectNode>(); 

try{
        Runtime rt = Runtime.getRuntime();
        ProcessBuilder pb = new ProcessBuilder(cmdArray);
        Process proc = pb.start();     // Start the process.
        System.out.println("Script executing");
        rc= proc.waitFor();    // Wait for the process to finish.
        System.out.printf("Script executed successfully in ", rc);

 InputStream stderr = proc.getErrorStream();
                    InputStreamReader isr = new InputStreamReader(stderr);
                    BufferedReader br = new BufferedReader(isr);

                    String line = null;
                    System.out.println("<ERROR___EXEC>");
                    while ( (line = br.readLine()) != null){

                        System.out.println(line);
                        node.put("line",line );
                        listFileNode.add(node);
                      }
                    System.out.println("</ERROR___EXEC>");
                    int exitVal = proc.waitFor();
                    System.out.println("Process exitValue: " + exitVal);


                    InputStream processInputStream =proc.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(processInputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    System.out.println("<RESULTAT___EXEC>"); 
                    while ( (line = bufferedReader.readLine()) != null){

                        System.out.println(line);
                        node.put("lineR",line );
                        listFileNode.add(node);

                  } 
                    System.out.println("</RESULTAT___EXEC>");  

            }catch (Throwable t)
              {
                t.printStackTrace();
              }

请帮助我,我被困在这一周,我找不到解决方案

1 个答案:

答案 0 :(得分:0)

您不能在java代码中使用sudo命令。如果您需要运行root特权应用程序,则需要在运行Java代码之前切换到root。

如果您想从Java代码中执行此操作,则需要调用setuid。但我不知道你可以从java代码中调用setuid。为此,您可能需要一个C / C ++包装器来切换到root。有关编写包装器的信息,请参阅:http://unix.stackexchange.com