• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Append a message to a file

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm trying to insert a message into a log file on a Solaris 10 machine via a Java program.

Basically I need to run an echo command and append the output into the file:

echo "Test message" >> /my/output/file

Here's my effort so far:

public class Test{
public static void main(String args[]){

try{
Process p = Runtime.getRuntime().exec(" echo \"TeSt MeSsAgE.\" >>/var/log/cisco" );

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

It compiles and runs silently but doesn't do what I want.

Any pointers in the right direction?

Thanks,
Sashi
 
Sheriff
Posts: 27465
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the Runtime.exec() method doesn't start a shell, it just runs an executable. You need a shell to support redirection. But instead of trying to fix that code to start a shell and do the redirection, which is still nontrivial, why don't you just use a FileWriter to append text to a file?
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might work, but it's a log file that's being written to by another process.
I'll give it a shot and post the results.
Thanks for your input,
Sashi
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that worked.
Thanks for the tip.
Sashi
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic