aspose file tools*
The moose likes Java in General and the fly likes executing shell script from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "executing shell script from java" Watch "executing shell script from java" New topic
Author

executing shell script from java

Chethan Sharma
Ranch Hand

Joined: Mar 22, 2006
Posts: 59
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


chaithu
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is not a Tomcat question.
Moving to the Java In General (Intermediate) forum.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18881
    
    8

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

Joined: Mar 22, 2006
Posts: 59
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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chethan Sharma
Ranch Hand

Joined: Mar 22, 2006
Posts: 59
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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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

Joined: Mar 22, 2006
Posts: 59
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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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

Joined: Mar 22, 2006
Posts: 59
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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40


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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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

Joined: Mar 22, 2006
Posts: 59
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

Joined: Mar 22, 2006
Posts: 59
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

Joined: Jul 26, 2002
Posts: 5
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

Joined: Mar 22, 2000
Posts: 12821
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: executing shell script from java