Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-21-2008, 04:53 AM
|
#1
|
LQ Newbie
Registered: Feb 2008
Posts: 5
Rep:
|
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........
|
|
|
02-21-2008, 05:26 AM
|
#2
|
LQ Newbie
Registered: Feb 2008
Location: Poland
Distribution: Fedora 8
Posts: 2
Rep:
|
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");
Last edited by bartekg; 02-21-2008 at 05:29 AM.
|
|
|
02-21-2008, 12:52 PM
|
#3
|
LQ Newbie
Registered: Feb 2008
Posts: 5
Original Poster
Rep:
|
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
|
|
|
02-21-2008, 10:41 PM
|
#4
|
Member
Registered: Feb 2008
Posts: 67
Rep:
|
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.
|
|
|
02-22-2008, 02:29 AM
|
#5
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
|
Another way would be using the Runtime.getInput/Output/ErrorStream() methods.
|
|
|
02-24-2008, 11:39 AM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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 07:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|