Java Terminal Help!

Discussion in 'Mac Programming' started by Awesomeness, Mar 20, 2009.

  1. Awesomeness macrumors member

    Feb 12, 2009
    Okay, I have officially put myself to the test. I am making a TERMINAL EMULATOR IN JAVA! :D I've made the GUI, the mac menu, actionlisteners... everything... it's just the terminal unix part isn't goin' so well.

    Here's my code for the command runner so far:
    package programming;
    public class Unix {
        public static String command(String cmd) {
            String s = null;
            String text = "";
            try {
    	    // run the Unix "ps -ef" command
                Process p = Runtime.getRuntime().exec(cmd);
                BufferedReader stdInput = new BufferedReader(new
                BufferedReader stdError = new BufferedReader(new
                // read the output from the command
                while ((s = stdInput.readLine()) != null) {
                    text = text + s;
                // read any errors from the attempted command
                while ((s = stdError.readLine()) != null) {
                    text = text + s;
            catch (Exception e) {
            return text;
    It works when I do stuff like ls, echo, and cd, but for unknown commands it doesn't say anything, other stuff it messes up, etc. For example, when I type in python, it freezes up.
    PS: My dad fixed the computer somehow. If you're wondering what I'm talking about, look at my recent threads.

    Anyways, how can I improve this code? I want to make it so that I can give it any command and it will return what it would spit back out at you in a real terminal. I want it to be fully functional! :D

    Note, to see my program, change the extension of the .zip to a .jar, and open it. You can see how the terminal behaves.
    Nooo! the jar didn't wooork!!! I'll upload a new one soon.

    Ok, got the new jar. It works. I didn't set the main() in the old one.

    AHHH! It auto-decompresses it! Darn... I'll try again...
    OK!!! I MADE IT A .TXT! IT... WILL... WORK!!!

    Attached Files:

Share This Page