1

I am new to using AWS SDK on Java. I have created a program to create AWS instances from Java and to check if its running. But I am not able to figure out how to run a bash command on that running instance. Please help!

try
                {
                    String lscmd = "ls";
                    Process p=Runtime.getRuntime().exec(lscmd);
                    p.waitFor();
                    BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line=reader.readLine();
                    while(line!=null)
                    {
                        System.out.println(line);
                        line=reader.readLine();
                    }
                }
                catch(IOException e1) {
                    e1.printStackTrace();
                }
                catch(InterruptedException e2) {
                    System.out.println("Pblm found2.");
                }

But I get an error saying,

java.io.IOException: Cannot run program "ls": CreateProcess error=2, The system cannot     find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at ListInstances.main(ListInstances.java:79)

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 5 more

I am not sure how to connect to the instance I created and run this command on that instance.

1
  • 1
    One solution might be to ssh into it and then run the command using jsch? Commented Feb 8, 2014 at 19:35

1 Answer 1

2

Could it be that the PATH is not defined and it doesn't know where to find ls? Try with the full path (e.g. /bin/ls) to verify.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.