Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Awesomeness

macrumors member
Original poster
Feb 12, 2009
73
0
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:
Code:
package programming;

import java.io.*;

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
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // 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.
EDIT:
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...
:mad:
OK!!! I MADE IT A .TXT! IT... WILL... WORK!!!
 

Attachments

  • JTerminal.txt
    8 KB · Views: 55
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.