Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

executing shell script from java

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not a Tomcat question.
Moving to the Java In General (Intermediate) forum.
 
Marshal
Posts: 26750
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I was her plaything! And so was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic