• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

shell and getRuntime

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic