Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

shell and getRuntime

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to initiate a shell (ksh) and the pass commands to that shell as needed. For example, I want to do something like:

1. /bin/ksh
2. run db2 connect user joe using password
3. run a db2 command in the same /bin/ksh shell

I tried the following, but it does not work:



Is there some way to just run something to make /bin/ksh and then be able to pass commands to that shell? How do people do multiple commands to the same shell? If I simply run another Exec command to do the db2 list tables command the db2 connection is gone--in that, it does not survive pass the initial conneciton, which is why I am now trying to chain a command within the ksh shell that created the db2 connection.

Thanks,

David
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A fairly common solution to this is to have the Java program create a shell script containing the commands that you want to run and then have the Runtime execute that script.
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. However, I don't want to create a script that could get left over accidently or on purpose because the password would be in it. Is it not feasible to create a shell and then pass commands into it? p = getRuntime("/bin/ksh"); p.exec("ls");
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be possible to start the shell as a Runtime process and then pass commands to it through the process's OutputStream, which would (in theory) emulate what happens as a user types in the console window. I've never tried that, and that approach would depend on whether or not the spawned shell process automatically dies if no command is passed to it.
 
Marshal
Posts: 65035
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People are forever quoting this article. It explains you have to call Runtime.exec(), divert the two Streams associated with the called process, and make sure to keep the Streams empty otherwise you are liable to get deadlock.
I had a similar problem a few weeks ago calling gcc from a shell, and this thread shows how I sorted it out.

I hope that helps you a bit.
 
Danger, 10,000 volts, very electic .... tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!