Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


LinkBack Search this Thread
Old 08-20-2006, 12:46 AM   #1
LQ Newbie
Registered: Dec 2005
Posts: 9

Rep: Reputation: 0
Java Commands for Command Line

I was wondering if someone could point me in the right direction for methods to be used to export commands to command line in linux. For instance, if I would like to write a GUI application that would run commands for a command line utility. Is there also methods that I could get the output from the command line and interpret them into the GUI's output.
Old 08-20-2006, 01:24 AM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

Here's a skeleton implementation

class MyClass {
    Runtime runEnviron;
    String[] commandArray = new String[2];

    MyClass() {
        runEnviron = Runtime.getRuntime();
        commandArray[0] = "echo";
        commandArray[1] = "hello";
        try {
            Process myProcess = runEnviron.exec(commandArray);
            BufferedReader stdOut = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));
            String outputLine;
            while (((outputLine = stdOut.readLine()) != null)) {
        } catch (IOException e) {
That should run 'echo hello', and print the 'hello' from the echo command.
Instead of the 'System.out.println(outputLine);', you could add the outputLine String to a JTextArea (if it's a Swing GUI).

As with all things Java programming, the Sun docs are pretty good:

Last edited by ilikejam; 08-20-2006 at 01:28 AM.
Old 08-20-2006, 01:43 AM   #3
LQ Newbie
Registered: Dec 2005
Posts: 9

Original Poster
Rep: Reputation: 0
Awesome. I just read this code and did not understand it. Then I reread it and I see the syntax. Pretty much you declare a variable as runtime. I was expecting something more like Runtime enviroment = new Runtime(), or something like that. When I get home after work tonight I am going to play around with this. Thank you for showing me this. My classes taking Java are now starting to pay off where I can create programs I want to create.
Old 08-20-2006, 02:36 AM   #4
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

Yeah, the Runtime syntax is weird, you're basically 'naming' the Runtime instance associated with the application (every running Java app has a single Runtime instance) so that you can use it to get at the exec() function.

Let us know how you get on...

Old 08-20-2006, 02:55 AM   #5
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
This is actually a common pattern in java, the singleton, where a class has only one instance, and cannot be instanciated from an outside class.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Time commands from command line ToothlessRebel Linux - Newbie 1 03-09-2005 10:52 AM
java - how do you input from the command line zeviddalop Programming 4 11-10-2004 11:59 AM
Using FTP to execute command line commands remotely steve_2010 Solaris / OpenSolaris 4 02-13-2004 08:44 AM
Clearly stated driver installation commands do not work from command line in terminal jayelitumbiolo Linux - Newbie 4 10-27-2003 12:52 AM
shell's command line locks after commands like ls tree_buddy Linux - Networking 17 09-15-2003 10:52 AM

All times are GMT -5. The time now is 04:02 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration