Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

A prompt command!

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic