[Ilugc] Executing Linux Commands with Java GUI

  • From: rajanvn@xxxxxxxxx (Natarajan V)
  • Date: Tue Feb 16 11:31:41 2010

On Tue, Feb 16, 2010 at 8:56 AM, Ravi Jaya <ravi.goglobium@xxxxxxxxx> wrote:

On Tue, Feb 16, 2010 at 8:52 AM, Ramesh Jothimani 
<linuxrockers@xxxxxxxxx>wrote:
I have Googled it already but I was unable to get the proper solution!

No you havent.

Please don't top post.

+1



The idea is simple. Don't look for ready made answers. First look for
how to execute a native process from Java. You will find this:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

so, you can do something like Runtime.getRuntime().exec("command")

Now, you want to pipe the input and output. Lets see how we can do that.
Looking at the method signature of "exec()"

public Process exec(String command)  throws IOException

The method returns an object of the class "Process". What does Process
have? It has a getInputStream() and getOutputStream(). Use these to
read the inputStream which is actually the output of the process and
you can write to the outputstream, which is actually the input to the
process.
READ: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Process.html

I did not understand one part of the question. What does java GUI have
to do with executing the command here? The above mentioned stuff can
simply execute a command. Java GUI has nothing to do with this. you
are welcome to write GUI code using AWT, SWT or SWING, and use the
above classes.


BUT, do you really want to do this? The main purpose of writing a
program in Java is to make it cross platform. The moment you start
using "exec()" with hardcoded Linux commands inside it, you just
defeat the purpose of the Java. You can very well rewrite your code in
C/C++? can't you? Sometimes, you might even be using native commands,
just because you don't know enough of Java? may be?

with regards,
Natarajan.

Other related posts: