Execute a Linux konsole command from Java

Hy,I am having problems when trying to execute a command for konsole from java.

This is a part of my code

try{

Runtime.getRuntime().exec("konsole");

}

catch (IOException ex){ex.printStackTrace();}

this code opens the Linux konsole ; is the a method to append a command for Linux from java?

Thanks

[545 byte] By [Vladislava] at [2007-11-27 5:08:34]
# 1
Obligatory "When Runtime.exec() wont" link: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htmlOn a sidenote, I believe there is a "-e" or "-x" switch for konsole to execute a command.
kevjavaa at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 2
I couldn't find anything that could help me.Can u be more specific ?
Vladislava at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 3
http://huygens.ca.infn.it/cgi-bin/man/man2html?konsole+1Check out the "-e" switch, and use all the knowledge you gained from the JavaWorld article to help you execute a konsole -e "ls ~/Desktop" or whatever.
kevjavaa at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 4
> Can u be more specific ?You first.All you've said is that you're "having problems."
jverda at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 5

OK

this is what I am trying to do : by pressing a button in a JFrame I want to start Snort (the exact command is snort -c /etc/snort/snort.conf )

What I want is the following: I press that button and the Konsole in Linux opens and that command to be executed

MY code is the following

try { Runtime.getRuntime().exec(" konsole -e snort -c /etc/snort/snort.conf")}

{catch(IOException ex) ex.printStackTrace()}

the command is executed , the Konsole window appears only for a second and closes , I NEED the window to stay open

Any suggestion is welcomed

Thanks

Vladislava at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 6

> OK

> this is what I am trying to do : by pressing a button

> in a JFrame I want to start Snort (the exact command

> is snort -c /etc/snort/snort.conf )

> What I want is the following: I press that button

> and the Konsole in Linux opens and that command to

> be executed

I would just use the Process.getInputStream() method to retrieve what Snort said and display that in a JTextArea or something... why bother launching it in a terminal?

I don't know if Snort is a daemon or what... if it's a daemon, you're not going to get much output from it... it'll fork itself into the background and exit.

If it isn't a daemon, then you can either just redirect the output to a file, a la:

konsole snort /etc/snort/snort.conf > /tmp/snort.out

and do a "tail" sort of monitoring on the file. You could also just keep that process up in a different thread, and use a timer to periodically update the output into a JTextArea (this would also allow you to be notified when the process ended). Finally, you could wrap your command in a script and execute it that way.

kevjavaa at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 7
Can u tell me more or give an example how to use Process.getInputStream() ?
Vladislava at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 8
Process proc = rt.exec("javac");OutputStream out = proc.getOutputStream();InputStream in = proc.getInputStream(); http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Process.html
kevjavaa at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 9
And that OutpusStream gets the info displayed by executing that command , and I cand put it to be displayed in a JTextArea ?
Vladislava at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 10
> And that OutpusStream gets the info displayed by> executing that command , and I cand put it to be> displayed in a JTextArea ?Yeah, dude. Give it a shot.
kevjavaa at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...
# 11
ok, thanks a lot I will try it
Vladislava at 2007-7-12 10:28:03 > top of Java-index,Java Essentials,Java Programming...