Hi there, How to "call" a command like "net send host_name message" from a java application?! Which class i must use?! Thanks!
--BJCK--
Peter Kristensson
Ranch Hand
Joined: Jul 02, 2001
Posts: 118
posted
0
Hi. Look at the java.lang.Runtime class, more specifically the exec methods. /Peter
Bruno Frascino
Ranch Hand
Joined: Jul 22, 2003
Posts: 55
posted
0
Thanks Peter!! It's simple... let me show for the others buddies:
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
Originally posted by BJack: It's simple... let me show for the others buddies:
There is no need to catch NullPointerException here. I presume that its inclusion was prompted by the JDK javadoc, but careful reading of that javadoc reveals that NullPointerException cannot occur in the example, because exec() is being passed a literal String, which cannot be null. In fact, it is very rarely appropriate to catch NullPointerException, because it indicates a programming error, rather than a user error or system fault. Second, and much more subtle, there are problems with Runtime.exec() for any command that might produce a significant amount of output on standard output or standard error streams. If the buffers for these streams fill up, the process that has been run with exec() will hang forever. Therefore, you often need to ensure that these buffers are drained, while the process is running; you may need additional thread(s) to do this. It's a real pain, but Sun refuses to acknowledge that it's a problem.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Dmitry Melnik
Ranch Hand
Joined: Dec 18, 2003
Posts: 328
posted
0
This piece of code handles the console output generated by the executed command, and waits for it to complete // =========== Process p; try { p = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader (new InputStreamReader(p.getInputStream ())); String s; while ((s = br.readLine ())!= null){ System.out.println (s); } p.waitFor(); } catch (Exception ex) { System.out.println( "Failed to execute the script (" + cmd + ") :\n" + ex.getMessage()); }
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
BJack, Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious. Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name. You can change your name: here. Thanks, Cindy
"JavaRanch, where the deer and the Certified play" - David O'Meara