-   Programming (
-   -   java and terminal window commands (

manolakis 02-03-2007 01:37 AM

java and terminal window commands
Hey there
I am interested in creating a java program which uses a print statement to the terminal window which actually is a command.
To be more clear consider the following example.

System.out.println("mkdir aDir");
Somehow like that a new directory should be created.
Is something like that possible?

Thanks for your time

jlliagre 02-03-2007 02:03 AM

It is doable by using the Runtime.exec() method, but it is usually not a good idea to run external programs from Java.
Especially when the job can be done in Java, like in this case (you can create a directory in pure Java), which would make your application more portable.

manolakis 02-03-2007 03:07 PM

Hey again
Many thanks for your reply
I would like once more again your advice. I have a C program which can be executed like:

./program < input > output
As you can see it takes some input from a text file and outputs to another file.
What i want to do is to run the C program then start up Java program which reads the output, determines if it is appropriate and if it's not then it starts up again the C program.
All of that should happen until i get the desired output
The fact is that i dont need to make any change to C or Java files. In other words they dont need to be compiled again

Any ideas?

jlliagre 02-03-2007 03:43 PM

How is the java program reporting the input is not appropriate ?

indienick 02-03-2007 10:27 PM

You will need to spawn the C application from within the Java program, and make use of the java.util.Scanner class (it's the easiest class in the world for obtaining input).

Pretty much, you'll need to spawn the C app. from within the java.util.Splitter() instance, like so:

java.util.Scanner s = new java.util.Scanner(Runtime.exec("application"));
...I'm not sure if that code is correct, but it should come very close - I'm at work right now, and don't have access to a Java compiler.


All times are GMT -5. The time now is 11:28 AM.