Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Runtime Problem

 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Am I missing something, the commands in the array dont appear to get processed at all
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a guess at what might be wrong; I haven't tried it. I'm thinking it may be that Windows doesn't like the "\n" on the end of your command string. Newline in Windows is "\r\n".

To fix, either just try that as the rest of your code is tied to Windows anyway or, in the interest of portability , use bw.newLine() or swap your BufferedWriter for a PrintWriter and use println().

Jules
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bizzare - none of those worked Does this happen in Unix too? I've got this working before now and it seems to have magically stopped!!

Tom
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't "cmd /c somecommand" tell cmd.exe to execute "somecommand" and then exit? Maybe you want to save "dir c:\\software" for the "commands" array.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay it does appear to run a command and the exit, hence why it stops early - but what should be there then?
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm ive just tried it on a linux machine and im getting the same problems
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. If you don't flush the bw stream, the commands may still be in the buffer.
2. You really need to set up a separate Thread to read the output - if the process output doesn't get consumed, it may just sit there.
Bill
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Hill:
okay it does appear to run a command and the exit, hence why it stops early - but what should be there then?


Just "cmd.exe".

Regarding doing something similar on Unix -- if you mean running sh -c "some command" and then trying to send more commands: yep, same problem, same reason.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
side note: why aren't you using Java to perform what you are attempting?

Your system commands look like a broken approach.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because I wanted to start with easy commnads I know the results of rather than the actuall commands that will be run
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting : on windows /k is required to keep the shell open to write commands too. In unix the first command has to be a shell command such a ksh and proceeding commands are written to it and then the new shell dies after leaving.

But:

doing: String[] commands = {"ksh","klog username","password"};
Hangs!
I assumed it might be that a new terminal is created for the password prompt but then: echo password | klog username wouldnt work:
and java doesnt like: commands ={"ksh","echo password | klog user"};
for the same reason - could it be that Java cannot cope with prompts that do not display the shell's input?
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it might have something to do with Bill's comment about consuming the process's output streams. See this Javaworld article for more detail.

Jules
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic