-1

i have a script bash which requires parameters.

when i call it directly from putty, it works, hen i do from my jee program it does not work and does not show me any errors.

This is my java code:

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();
              }

please help me i am stuck on this from a week and i don't find solution

1 Answers1

0

You can't use sudo command in the java code. If you need to run root privileged application you need to switch to root before running your java code.

If you want to do it from your java code, you need to call setuid. But I don't know you can call setuid from a java code. For that purpose you may need a C/C++ wrapper for switching to root. Refer:http://unix.stackexchange.com question for writing wrapper.

Community
  • 1
  • 1
Jobin
  • 6,506
  • 5
  • 24
  • 26