• Post Reply Bookmark Topic Watch Topic
  • New Topic

Append a message to a file  RSS feed

 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 22712
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Yes, that worked.
Thanks for the tip.
Sashi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!