#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Buenos Aires, Argentina
    Posts
    64
    Rep Power
    2

    Executing Commands from Java (Mac Os X / Unix)


    Good day.

    Well, I'm stuck in the following code:

    Code:
    try {
    if (opersis==1) {
    String [] clearwin = {"sh", "-c", "clear"};
    Runtime.getRuntime().exec(clearwin);
    } else if (opersis==2) {
    //ejecutar un clear
    	String [] clearwin = {"cmd", "-c", "cls"};
    	Runtime.getRuntime().exec(clearwin);
    } 
    else { 
    System.out.println("No ha seleccionado un sistema operativo correctamente. El programa NO PUEDE continuar.");
    System.exit(1);
    }
    } catch (IOException ioe) { 
         System.out.println (ioe);
    }
    What I want:
    As I enter Opensis "1", it should execute the "clear" command AND OBVIOUSLY CLEAR the current console I'm using to run my java snippet. (This is just a snippet, not the whole program).

    What Happens:
    As I enter Opensis "1", it does executes the command (I tried changing it to non-existing commands and it does complain throwing me the exception right in my face... and I can even hear it saying 'In yer face... Problem?').
    As I said, it does run the command, but nothing happens. Seems to be running on background (In another snippet, I ran an installation and, in fact it was completed, but no output was shown, in that case, that's what I wanted).
    It should simply, clear the screen, but seems to invoke the command, and (?)...
    Also, man clear clearly tells "ignores every argument provided". Nothing more to do, I think.

    What's my equipment:
    Mac Mini PPC G4 10.5.8 Leopard.
    Using SH. Frontend: iTerm.
    Java Version: java version "1.5.0_30"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_30-b03-389-9M3425)
    Java HotSpot(TM) Client VM (build 1.5.0_30-161, mixed mode, sharing)
    Updated to last possible version of every software (Using update manager).

    Probably I'm missing something myself in my code, but maybe this data would come in handy if something further is causing me to get lost...

    Well, hope someone could help me. Sorry if I provided few or too much data, this is the first time I make a question here.

    Regards.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Take a look at your code:

    java Code:
    String [] clearwin = {"sh", "-c", "clear"};


    What do you think the first thing that the "exec" does? It spawns a brand new shell (sh). Everything you're doing is operating in there, not your current shell.

    Why do you run the command in "sh" and not just a single "clear" as the command?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Buenos Aires, Argentina
    Posts
    64
    Rep Power
    2
    Originally Posted by stdunbar
    Take a look at your code:

    java Code:
    String [] clearwin = {"sh", "-c", "clear"};


    What do you think the first thing that the "exec" does? It spawns a brand new shell (sh). Everything you're doing is operating in there, not your current shell.

    Why do you run the command in "sh" and not just a single "clear" as the command?
    Hi stdunbar! First let me thank you for taking the time for helping me out of this problem.

    I use it because, if not used it throw an exception and says "clear cannot be found". While I was reading on another forum for a solution, on an old thread someone was having a similar issue with that exception (If not wrong, IO.IoException, I think) and one of the users explained him as follows:

    "The command you're trying to invoke it's not an executable but a command from the shell, if you look at /usr/bin/ you won't find any exec with the name of that command, so tell your shell to open and then execute that command". He used "ksh", but didn't worked for me. So I turned it to "sh".

    I know, probably quite wrong, but it got the code to work around.

    If I use it alone, I get the exception. Any ideas what could I use instead?

    Again, thanks for your help.
    I would like you to know that I'm not asking for a simple answer without even bothering myself on finding the answer based upon research first. I tried, but can't find this thing exactly.

    Regards and thanks for your time.

    -Mauro.

IMN logo majestic logo threadwatch logo seochat tools logo