• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

A prompt command!

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
How to "call" a command like "net send host_name message" from a java application?!
Which class i must use?!
Thanks!
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Look at the java.lang.Runtime class, more specifically the exec methods.
/Peter
 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter!!

It's simple... let me show for the others buddies:
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!