Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Executing a UNIX shell script using JAVA  RSS feed

 
jkpriyatcs jkpriyatcs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to execute a shell script that takes in 3 CLI arguments.
My program is
import java.io.*;


public class ExecDemo
{
public static void main(String args[])
{
Runtime r= Runtime.getRuntime();//reference to runtime java environment
Process P=null;
try
{
System.out.println("Inside PGM");
System.out.println("/wls_domains/neo21/met_nbt/metDomain/shell/Automate1.sh "+'"'+"<TaskIdentifier>47</TaskIdentifier>"+'"' +" 10 150");
String strShellName="/wls_domains/neo21/met_nbt/metDomain/shell/Automate1.sh "+'"'+"<TaskIdentifier>47</TaskIdentifier>"+'"' +" 10 150";
String[] cmd={"/bin/sh",strShellName};
System.out.println("shell script loaded");
P=r.exec(cmd);
//int retval=P.waitFor();
// System.out.println("retval:"+retval);
StringBuffer strBuf=new StringBuffer();
String strLine="";
BufferedReader outCommand = new BufferedReader(new
InputStreamReader(P.getInputStream()));
while((strLine=outCommand.readLine())!=null)
{
strBuf.append(strLine);

}
// System.out.println("strCmpleteOp"+strBuf);

}
catch(Exception e)
{

e.printStackTrace();
}
}
}

I find the shell script is loaded but iam not able to view the output of the script through my program.

Can anyone help?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

There are a number of pitfalls one must be aware of when using Runtime.exec. This article is very helpful in avoiding them.
 
Paul St. Martin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to read the document mentioned by Ulf Dittmer; and with shells, be sure to exit the shell:

Runtime rtime = Runtime.getRuntime();

//start unix shell
Process child = rtime.exec("/bin/sh");
//or "/bin/sh set -t" to auto-exit after 1 command

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

//run perl command
outCommand.write(cmd + "\n");
outCommand.flush();

//remove a file
outCommand.write("rm -f " + file + "\n");
outCommand.flush();

//rename a file
outCommand.write("mv " + fileNew + " " + file + "\n");
outCommand.flush();

//exit the unix shell
outCommand.write("exit" + "\n");
outCommand.flush();

int wait = child.waitFor();
System.out.println("exit code: " + wait);


- Paul
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem... except that I am trying
to run linux command line program and have java feed it the inputs
automatically, via java.

[jay] MyCProgram
MyCProgram> set x=1
x is now equal to 1
MyCProgram> x + 2
3


So is there any way to do this from java ? That is, to run a command line
program and set its inputs iteratively ?

I would need to "grab" the process, and send it input twice, before exiting.

Is this even possible in Java ?

Ive been working for 10 hours on this problem so I really need some help.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec returns a Process object, through which you can get access to the input, output and error streams of the newly created process.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does, but can you give me an example ?
Ive tried everything and cant seem to create
an interactive dialog with a unix program.

How can you validate that when youre writing to a processes output stream
youre commands are in fact arriving to it ?

I tried

process.getOutputStream().write("next command");
process.getOutputStream().flush();

but it seems to stall...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a shot in the dark, but are you sending a line terminator, i.e. CR+LF, after the command? Because without it, the command is not complete and will not execute (it's like entering it on the command line, but not hitting return).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!