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!?
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?
posted 15 years ago
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
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,
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