Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run shell command from java

Tags:

java

exec

I am working on an application an have an issue about running shell command from java application. here is the code:

public String execRuntime(String cmd) {

        Process proc = null;
        int inBuffer, errBuffer;
        int result = 0;
        StringBuffer outputReport = new StringBuffer();
        StringBuffer errorBuffer = new StringBuffer();

        try {
            proc = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            return "";
        }
        try {
            response.status = 1;
            result = proc.waitFor();
        } catch (InterruptedException e) {
            return "";
        }
        if (proc != null && null != proc.getInputStream()) {
            InputStream is = proc.getInputStream();
            InputStream es = proc.getErrorStream();
            OutputStream os = proc.getOutputStream();

            try {
                while ((inBuffer = is.read()) != -1) {
                    outputReport.append((char) inBuffer);
                }

                while ((errBuffer = es.read()) != -1) {
                    errorBuffer.append((char) errBuffer);
                }

            } catch (IOException e) {
                return "";
            }
            try {
                is.close();
                is = null;
                es.close();
                es = null;
                os.close();
                os = null;
            } catch (IOException e) {
                return "";
            }

            proc.destroy();
            proc = null;
        }


        if (errorBuffer.length() > 0) {
            logger
                    .error("could not finish execution because of error(s).");
            logger.error("*** Error : " + errorBuffer.toString());
            return "";
        }


        return outputReport.toString();
    }

but when i try to exec command like :

/export/home/test/myapp -T "some argument"

myapp reads "some argument" as two seperated arguments.but I want to read "some argument" as only a argument. when i directly run this command from terminal, it executed successfully. I tried '"some argument"' ,""some argument"" , "some\ argument" but did not work for me. how can i read this argument as one argument.

like image 344
masay Avatar asked Mar 17 '10 06:03

masay


People also ask

Can we run shell script from Java?

As we've seen in this quick tutorial, we can execute a shell command in Java in two distinct ways. Generally, if you're planning to customize the execution of the spawned process, for example, to change its working directory, you should consider using a ProcessBuilder. As always, you'll find the sources on GitHub.

How do I run a terminal command in Java?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

Can we run Linux command in Java?

You can use java. lang. Runtime. exec to run simple code.


1 Answers

I recall that the an overload of exec method provides a parameter for the arguments seperately. You need to use that

Yup. Here is it

public Process exec(String[] cmdarray)
             throws IOException

Just make the command line and all arguments Seperate elements of the String array

like image 129
Midhat Avatar answered Sep 21 '22 05:09

Midhat