• Post Reply Bookmark Topic Watch Topic
  • New Topic

command line jazz

 
Jason Crest
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any EASY way to do command line stuff in java?
we are trying to create directories and do stuff like "cat hello.txt > blah.txt".
unfortunatly we've been using Runtime.exec().....silly us
any ideas my fellow buckaroos!?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that depends on what command line stuff you are trying to do. Runtime.exec() is the way to execute command line commands, no way around it.
So are you wanting to know if there is a way in Java to write code that does the same thing as a command, or a different way to call commands from within Java?
 
Jason Crest
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no problem when i do stuff like mkdir and such, but when i start trying to direct output to a file using ">" or i try to pipe something, then it doesnt work. i read something about Runtime.exec() and delimiters and cant recognize ">" yadda yadda yadda, not too sure about it, haha. so im kinda stuck
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said "cat" so I'm thinking you're on UNIX. Good, because I don't know precisely the answer on Windows. I'm sure it's similar.
On UNIX systems, things like ">" are handled by the shell. So to use them while executing a proccess from Java, you have to execute a shell, and get the shell, in turn, to execute the command. So, for example,
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be two other solutions:
a) Use Files and FileOutputStream or FileWriter.
Though there is need to write more than one line of code, you have a better control in error-situations (write permissions, disk full, ...), which would lead to much more code, if using 'Runtime.exec'.
And you don't need different code for different platforms (unix:cat, dos:type, mac: ?)
b) For small programs, you may use System.out.println ("foo"), and call the program with:
java Foo > foo.log
 
Jason Crest
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks! worked great
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what worked great?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!