aspose file tools*
The moose likes Java in General and the fly likes Runtime.exec: how can i use whitespace.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec: how can i use whitespace.." Watch "Runtime.exec: how can i use whitespace.." New topic
Author

Runtime.exec: how can i use whitespace..

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 18, 2001
Posts: 128
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

Joined: Jun 18, 2001
Posts: 128
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

Joined: Nov 22, 2008
Posts: 18944
Thanks for that, Kaspar.
Great help !
kind regards,
Dennis
ken chou
Ranch Hand

Joined: Feb 08, 2001
Posts: 68
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

Joined: Jun 18, 2001
Posts: 128
You are correct...
My fingers are faster than my brain...
/Kaspar
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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

Joined: Nov 22, 2008
Posts: 18944
Sorry, Desai, what exactly do you mean by "writing data in an external application " ? Is that, modifying an existing Excel sheet ?
#! Dennis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.exec: how can i use whitespace..