0

I'm running below code on windows using java and want to run same code on CENT OS machine using java but not getting any fruitful. Please let me know what changes are required to solve this issue.

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

public class CommandPrompt {

    public static void main(String[] argv) throws Exception {
        try {
            Process child = Runtime.getRuntime().exec(commands);

            child = Runtime.getRuntime().exec("cmd /c \"\" aws cp E:\\rock.jpg s3://bucket");

            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));

            InputStream in = child.getInputStream();

            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            in.close();
        }

    }
}

Now I want run above code on cent os (unix) but not getting any fruitful.

4
  • The cmd thing only works with Windows/DOS . Commented Oct 24, 2018 at 12:44
  • what is the subtitute for cmd in centos? Commented Oct 24, 2018 at 12:56
  • Detailing the error you get is helpful for troubleshooting. Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example Commented Oct 24, 2018 at 12:59
  • I figured that my answer was a bit off, I just adapted it. Let me know if this works for you now. Commented Mar 23, 2019 at 6:47

1 Answer 1

1

Here:

child = Runtime.getRuntime().exec("cmd /c \"\" aws cp E:\\rock.jpg s3://bucket");

You are invoking the Windows cmd shell to invoke the aws binary to copy a local file to s3.

Please note: it is A) a windows specific command, and B) using windows specific file system details.

The short answer: don't expect that to work on any other operating system then. Unless you remove the part to use "cmd".

For a Linux system, you have to make sure some aws tools is in your path and to use a Linux like file path.

But the real solution is to turn to https://aws.amazon.com/sdk-for-java/ and to not call some aws binary using the command line but to use Java interfaces to do these tasks directly in Java code!

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

2 Comments

what is the subtitute for cmd in centos?
It would be a shell; e.g. "sh" or "bash". But for this particular example, you shouldn't need a shell.

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.