LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-21-2008, 04:53 AM   #1
maheshkodamati
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Rep: Reputation: 0
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........
 
Old 02-21-2008, 05:26 AM   #2
bartekg
LQ Newbie
 
Registered: Feb 2008
Location: Poland
Distribution: Fedora 8
Posts: 2

Rep: Reputation: 0
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.
 
Old 02-21-2008, 12:52 PM   #3
maheshkodamati
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 02-21-2008, 10:41 PM   #4
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
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.
 
Old 02-22-2008, 02:29 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Another way would be using the Runtime.getInput/Output/ErrorStream() methods.
 
Old 02-24-2008, 11:39 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing konsole from shell script Meriadoc Linux - Newbie 10 07-10-2009 08:08 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 12:33 AM
executing shell script on apache nooodles Linux - General 1 06-29-2004 01:51 AM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM
executing a shell script from C llama_meme Programming 2 04-01-2002 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration