0
votes

I am getting error while executing sh file by using java code.From terminal, it is working fine

sudo: no tty present and no askpass program specified

My Code :-

package com.test;

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class TestScript {

        int iExitValue;
        String sCommandString;

        public void runScript(String command){
            sCommandString = command;
            CommandLine oCmdLine = CommandLine.parse(sCommandString);
            DefaultExecutor oDefaultExecutor = new DefaultExecutor();
            oDefaultExecutor.setExitValue(0);
            try {
                iExitValue = oDefaultExecutor.execute(oCmdLine);
            } catch (ExecuteException e) {
                System.err.println("Execution failed.");
                e.printStackTrace();
            } catch (IOException e) {
                System.err.println("permission denied.");
                e.printStackTrace();
            }
        }

        public static void main(String args[]){
            TestScript testScript = new TestScript();
            testScript.runScript("sh /home/FTP-SCP-Project/shellscript.sh");
        }
    }

sh file :-

sudo scp -i /home/FTP-SCP-Project/src/lib/demo.pem -r /home/FTP-SCP-Project/test-output3 [email protected]:/var/www/html/projects/demo_reports/reporttest112

I have tried all other java code also but they are giving error while I have giving them all permision like chmod 777 or 755 or 600

    String Pemfilepath="/home/shubham/Experiment_zone/FTP-SCP-Project/src/lib/cuelogic.pem";
    String targetFolder = "/home/shubham/Experiment_zone/FTP-SCP-Project/test-output3";
    String[] command = { "sudo scp -i "+Pemfilepath+" -r "+targetFolder+" "+"[email protected]:/var/www/html/projects/kumo_reports/reporttest1"};   
//    String command = "ls";
    System.out.println(command[0]);
    Process process = Runtime.getRuntime().exec(new String[] { "sudo ", "scp ", "-i " ,Pemfilepath," ", "-r"," ",targetFolder," ","[email protected]:/var/www/html/projects/demo_reports/reporttest1" });                 
    BufferedReader reader = new BufferedReader(new InputStreamReader(         
        process.getInputStream()));                                          
    String s;                                                                
    while ((s = reader.readLine()) != null) {                                
      System.out.println("Script output: " + s);                             
    }  

But they are also giving error as below :-

Exception in thread "main" java.io.IOException: Cannot run program "sudo ": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) at java.lang.Runtime.exec(Runtime.java:617) at java.lang.Runtime.exec(Runtime.java:485) at com.test.shelllocallaunch.main(shelllocallaunch.java:14) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:187) at java.lang.ProcessImpl.start(ProcessImpl.java:130) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 3 more

I have observe that if the command is small like ls everything is fine but my above command is not executing while it is working fine from terminal. Please help if I missing anything

1

1 Answers

0
votes

"echo "+pass+" | "+"sudo -S Worked for me

My full code seems like below :-

String pass = "\"Yourpassword\"";
                out.println("echo "+pass+" | "+"sudo -S scp -i "+Pemfilepath+" -r "+targetFolder+" "+"[email protected]:/var/www/html/projects/demoproject");

Hope it will help you :)