• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

command line jazz

 
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!?
 
Chicken Farmer ()
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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,
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what worked great?
 
crispy bacon. crispy tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!