This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executing shell script from java

 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

import java.util.*;
import java.io.*;

public class Test
{
public static void main(String [] args)
{
try
{
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec("/bin/bash");
BufferedWriter outCommand = new BufferedWriter(new
OutputStreamWriter(child.getOutputStream()));
outCommand.write("/users/test/aaa.sh");
outCommand.flush();
int exitVal = child.waitFor();
System.out.println("ExitValue: " + exitVal);
}catch (Exception e)
{
e.printStackTrace();
}
}
}

i did chmod 777 for aaa.sh

The program is hanging with out any output.


Please let me know.

Thanks in Advance

Rams
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a Tomcat question.
Moving to the Java In General (Intermediate) forum.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The shell command never finishes because it is writing something to stdout. When the buffer fills up, it waits for your program to read that data. But your program doesn't read the data, it waits for the shell command to finish. Deadlock. Read from child.inputStream() -- which is the shell's stdout -- or child.errorStream() -- which is stderr.
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
The shell command never finishes because it is writing something to stdout. When the buffer fills up, it waits for your program to read that data. But your program doesn't read the data, it waits for the shell command to finish. Deadlock. Read from child.inputStream() -- which is the shell's stdout -- or child.errorStream() -- which is stderr.


String file_location = "/users/temp/rams";
String file_location1 = "/users/temp/Test.java";
String a_mib_name = "Test269.java";
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec("/bin/bash");
child.getOutputStream().write("cd " + file_location);
System.out.println("cd " + file_location);
child.getOutputStream().write("ln -s " + file_location1 + " " + a_mib_name);
child.getOutputStream().flush();
System.out.println("inside script");

cd and softlink commands are not working...
pls let me know how to handle this scenario.

Thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really answering your question... just pointing out that there are other exec() methods which does exactly what you want, and easier to use.



Henry
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Not really answering your question... just pointing out that there are other exec() methods which does exactly what you want, and easier to use.



Henry


public class Test2
{
public static void main(String [] args)
{
try
{

String file_location = "/users/temp/rag/";
String file_location1 = "/users/temp/Test.java";
String a_mib_name = "Test269.java";
String cmd[] = new String[] {"/bin/bash", "cd ", file_location ," ln ", " -s ", file_location1, " ", a_mib_name};
for(int i=0;i<cmd.length;i++)
{
System.out.println("....."+cmd[i]+".....");
}
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec(cmd);
}catch (Exception e)
{
e.printStackTrace();
}
}
}


still facing the same problem.
cmds are not working....
pls let me know..
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a valid bash command...



Why did you decide not to do it the way I showed you?

Henry
[ May 07, 2006: Message edited by: Henry Wong ]
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
This is not a valid bash command...



Why did you decide not to do it the way I showed you?

Henry

[ May 07, 2006: Message edited by: Henry Wong ]


i am getting this error

Wrong number of arguments in method.
Process child = rtime.exec(cmd, null, file_location);
^
1 error

String file_location = "/users/temp/rag";
String file_location1 = "/users/temp/Test.java";
String a_mib_name = "Test269.java";
String cmd[] = new String[] {"/bin/bash", "-c", "ln ", "-s ", file_location1, " ", a_mib_name};
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec(cmd, null, file_location);



one more doubt here where we are changing dir to file_location?


Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the example that I showed you... You still have differences.

one more doubt here where we are changing dir to file_location?


The version of exec() that I am using is specifying a current working directory. One of the operations prior to executing the command is changing to the current working directory.

Henry
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Take a look at the example that I showed you... You still have differences.



The version of exec() that I am using is specifying a current working directory. One of the operations prior to executing the command is changing to the current working directory.

Henry


File file_location = new File("/users/temp/rag");
String file_location1 = "/users/temp/Test.java";
String a_mib_name = "Test269.java";
String cmd[] = new String[] {"/bin/bash", "-c", "ln", "-s", file_location1, a_mib_name};
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec(cmd, null, file_location);

Wrong number of arguments in method.
Process child = rtime.exec(cmd, null, file_location);
^
1 error

After ln and -s cmd is any space required ?

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wrong number of arguments in method.
Process child = rtime.exec(cmd, null, file_location);
^
1 error


It compiles fine on my end.


After ln and -s cmd is any space required ?


No... It is a parameter list. Those parameters are not supposed to have spaces.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possible issue... the version of exec() which takes a current working directory was added in Java 1.3.

What version of Java are you using?

Henry
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Possible issue... the version of exec() which takes a current working directory was added in Java 1.3.

What version of Java are you using?

Henry


I am using the below mentioned version.
java version "1.2.2"
Solaris VM (build Solaris_JDK_1.2.2_10, native threads, sunwjit)

let me know how to resolve this one in this version.

Thanks
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RamaShankar Miryala:


I am using the below mentioned version.
java version "1.2.2"
Solaris VM (build Solaris_JDK_1.2.2_10, native threads, sunwjit)

let me know how to resolve this one in this version.

Thanks


It compiles fine on java version "1.4.2_09" but softlink is not created.
No luck.pls let me know where i am doing not correct.

Thanks
 
MK Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
The shell command never finishes because it is writing something to stdout. When the buffer fills up, it waits for your program to read that data. But your program doesn't read the data, it waits for the shell command to finish. Deadlock. Read from child.inputStream() -- which is the shell's stdout -- or child.errorStream() -- which is stderr.


Does it mean I should keep reading the error and inputStream one command after another? Or just read at the end of all commands?

Do I have to call child.waitFor anymore?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean I should keep reading the error and inputStream one command after another? Or just read at the end of all commands?

People typically start separate Threads to read the std err and std out streams independently - thats the safest thing to do.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic