Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec: how can i use whitespace..

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
I need to tell my Runtime environment to run "ping 192.168.0.4".
The method "exec(String s)" tokenizes the argument, shredding whenever whitespace occurs and sending out the tokens as directions to the runtime.
My workaround is to put "ping.." into a batchfile and run this one instead. This works. I do have to create this call dynamically, though, so there should be a better solution.
Anyone can help me ?
thanx 4 that,
Dennis
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!
Try to use the exec() method that takes a string array instead. Tokenize your commands and put each one of them into the cmdarray. This should work, I think!
//Kaspar
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works for me... :
import java.io.*;
public class T {
public static void main(String[] a) throws IOException {
String[] s = {"ping", "192.168.0.4"};
Process p = Runtime.getRuntime().exec("ping 192.168.0.4");

InputStreamReader in = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(in);
int c;
while ((c = br.read()) != -1)
System.out.print((char) c);
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that, Kaspar.
Great help !
kind regards,
Dennis
 
ken chou
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Kaspar:
In your code, I don't see the reason why you have:
String[] s = {"ping", "192.168.0.4"};

I guess you are trying to have:
String[] s = {"ping", "192.168.0.4"};
Process p = Runtime.getRuntime().exec(s);
Instead of:
String[] s = {"ping", "192.168.0.4"};
Process p = Runtime.getRuntime().exec("ping 192.168.0.4");
Right?

 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct...
My fingers are faster than my brain...
/Kaspar
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I had used the Runtime.exec() method to open an Excel spread-sheet.I was looking at a way to write some data in an external application like Excel.How do I do it?
-- Sandeep
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Desai, what exactly do you mean by "writing data in an external application " ? Is that, modifying an existing Excel sheet ?
#! Dennis
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic