• Post Reply Bookmark Topic Watch Topic
  • New Topic

Edit method to accept parameters  RSS feed

 
paul a carron
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm getting back into Java after some years away but must admit I'm struggling a little.

I've written the below method to stop a service:



I'd now like to edit it so that I could break up the string array and pass the action(stop in the example above) and the service name as parameters. Can this be done easily and if so how?

Thanks
Paul
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the easiest way would be to use the var arg syntax, since you aren't sure exactly how many arguments you need to pass:

Then you could call it like this:

One other thing: you probably need to wait for the process to finish before reading any output:  Not necessary.  Read Carey's post below.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not entirely clear on what you're asking. The command array is already "broken up". If you are wanting to populate part of it with a parameter you could do it something like this:

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:One other thing: you probably need to wait for the process to finish before reading any output:

Wouldn't readLine() on an open inputstream block until either, there is more data, or the inputStream has closed? If it were me I'd like to see all of the cmd output as it's happening and not wait for it to be completed.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what do you know.  I just tested it with a process that produces output before it terminates, and you can read the STDOUT and STDERR before the process is finished.  TIL something new.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't worry about struggling with that sort of code. Processes are notoriously difficult to write and ProcessBuilder only makes them slightly easier. Find the old article “When Runtime.exec() won't” by Michael Daconta and read that carefully. Remember that Daconta wrote before ProcessBuilder was introduced, so read about that too.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!