LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   executing shell script from java application (https://www.linuxquestions.org/questions/programming-9/executing-shell-script-from-java-application-622724/)

maheshkodamati 02-21-2008 04:53 AM

executing shell script from java application
 
hi,i want to know is there any way of executing a shell script from my java application? please help me........

bartekg 02-21-2008 05:26 AM

read about method exec() at java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

quick example how to execute 'ls' command

Runtime.getRuntime().exec("ls -l");

maheshkodamati 02-21-2008 12:52 PM

Thank u bartekg.......
i have read the description given for runtime and i've tried executing the program as below:
Public class ExecCMD{
psvm(S a[]){
try{
Runtime r=Runtime.getRuntime();
Process p=r.exec("sh xyz.sh");
}catch(Exception e){
.......
}
}
}
when i run this code i didn't get the o/p as the shell should print a string.So,i have changed my shell to redirect the o/p to a file
i.e
xyz.sh: echo "abcdef" > abc.txt
so after running the above java program i got a file named abc.txt which contains the string abcdef.this shows that my shell is running.
But what my problem is, i must give input from an interface created by java
program to a shell script.(dynamically)
i' m very thankful to u
plz reply me soon

JWPurple 02-21-2008 10:41 PM

One way to do it is to create a file-system pipe (man mkfifo). Have the script read from it, and have the java program write to it.

jlliagre 02-22-2008 02:29 AM

Another way would be using the Runtime.getInput/Output/ErrorStream() methods.

Tinkster 02-24-2008 11:39 AM

Moved: This thread is more suitable in <PORGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 06:51 AM.