Galaxin
Galaxin

Reputation: 484

Unable to run a shell script from java

I have a shell script that has a single command to generate a file with the required information in it after sucessful creation of the script.

Now when Iam executing that command directly or executing the script itself from the cmd line the file is getting generated.

But when iam trying to execute the same script from the java code using Runtime.getRuntime().exec(cmd) the file is not getting generated.The traces just before and after this line are displayed as expected indicated that the execution of the script is not throwing any exceptions.

Where am I going wrong or what am I missing on? Plz help!!!

Example this is the code iam tryng to execute

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Scripttest {
public static void main(String args[])
{
        try{
String cmd="ls|grep sys";
        Process p=Runtime.getRuntime().exec(cmd);
        System.out.println("done");
        final BufferedReader input = new BufferedReader(new InputStreamReader( p.getInputStream()));
        String line = null;
        while ((line = input.readLine()) != null)
         System.out.println("proc: " + line);
}
        catch(Exception e)
        {
                System.out.println("Exception is"+e);
        }
}
}

and the output iam getting is this

java Scripttest
Exception isjava.io.IOException: Cannot run program "ls|grep": error=2, No such file or directory

The cmd is not getting executed but not sure abt the mistake why it is not.

Upvotes: 0

Views: 1523

Answers (2)

Suniel
Suniel

Reputation: 1499

keep the command ls|grep sys in an array because It consists of two primary commands ls and grep.

Or

The best idea is to use shell script in linux and batch file in windows. create a shell script or batch file as per your requirement with the commands and just execute the shell script or batch file from java code.

Upvotes: 0

pizza
pizza

Reputation: 7640

Here is an example to do it somewhat like using pipes, the script is embedded in java in this example.

import java.io.*;
class junk {
 public static void main (String args[]) {
  try {
   String line; String script;
   OutputStream stdin = null; InputStream stderr = null; InputStream stdout = null;
   Process p = Runtime.getRuntime ().exec ("/bin/bash");
   stdin = p.getOutputStream (); stderr = p.getErrorStream (); stdout = p.getInputStream ();
   script = "a=$(cat <<'@@@'\n" +
"ICAgICBfCiAgICB8IHwgX18gX19fICAgX19fXyBfICAgIF9fXyBfXyBfIF8gX18gICAgXyBfXyBfICAg" +
"XyBfIF9fCiBfICB8IHwvIF9gIFwgXCAvIC8gX2AgfCAgLyBfXy8gX2AgfCAnXyBcICB8ICdfX3wgfCB8" +
"IHwgJ18gXAp8IHxffCB8IChffCB8XCBWIC8gKF98IHwgfCAoX3wgKF98IHwgfCB8IHwgfCB8ICB8IHxf" +
"fCB8IHwgfCB8CiBcX19fLyBcX18sX3wgXF8vIFxfXyxffCAgXF9fX1xfXyxffF98IHxffCB8X3wgICBc" +
"X18sX3xffCB8X3wKICAgICAgICAgICAgICAgICAgICBfICAgICAgICAgICAgICAgXwogICAgICAgICAg" +
"ICAgICAgICAgfCB8X18gICBfXyBfIF9fX3wgfF9fCiAgICAgICAgICAgICAgICAgICB8ICdfIFwgLyBf" +
"YCAvIF9ffCAnXyBcCiAgICAgICAgICAgICAgICAgICB8IHxfKSB8IChffCBcX18gXCB8IHwgfAogICAg" +
"ICAgICAgICAgICAgICAgfF8uX18vIFxfXyxffF9fXy9ffCB8X3wK" +
"\n" +
"@@@)\n" +
"st=0\n" +
"for (( i=0; i<\"${#a}\"; i++ ))\n" +
"do\n" +
" x=${a:$i:1}\n" +
" in=$(($(expr index \\\n" +
" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' \"\\\\$x\")-1))\n" +
" if [ $in -ge 0 ]; then case $st in\n" +
"  0 ) out=$(($in<<2)); st=3;;\n" +
"  1 ) out=$(($out|$in)); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ; st=0 ;;\n" +
"  2 ) out=$(($out+($in>>2))); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
"    st=0; out=$(($in<<6)); st=1;;\n" +
"  * ) out=$(($out+($in>>4))); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
"    st=0; out=$(($in<<4)); st=2;;\n" +
"  esac fi\n" +
"done\n";
   stdin.write (script.getBytes ());
   stdin.close ();
   BufferedReader br = new BufferedReader (new InputStreamReader (stdout));
   while ((line = br.readLine ()) != null) { System.out.println(line); }
   br.close ();
   br = new BufferedReader (new InputStreamReader (stderr));
   while ((line = br.readLine ()) != null) { System.out.println ("2>" + line); }
   br.close ();
   p.waitFor ();
   System.out.println ("exit code " + p.exitValue ());
  }
  catch (IOException e) { e.printStackTrace (); }
  catch (java.lang.InterruptedException e) { e.printStackTrace (); }
 }
}

Upvotes: 2

Related Questions