Results 1 to 7 of 7
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default help with "Runtime.getRuntime().exec(this.command);"

    Hi everyone

    I use Runtime to run unix command.
    it work fine for some simple command as pwd or ls
    but several other simple command fail - such as "cd .."

    More importent - I want to run tcsh ( or C-shell) scripts , but it fail
    hwat I'm doing (inside the class).. for example

    this.commnd="tcsh MyScript.tcsh"
    Runtime.getRuntime().exec(this.command);

    Thanks

  2. #2
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default Further detail

    ok . the tcsh is this

    I make the script because if I use the unix "cd" function - it fail. I though that inside shell it might work

    #!/bin/tcsh -f
    #
    # test.tcsh : test case , somwthing simple - print currect direcrot, got to parent dir , print it and return to original dir
    #
    # Created by Itai Peer on Sun Dec 28 15:20:25 IST 2008
    #

    set org_dir=`pwd`
    pwd
    echo "-----------------"
    cd ..
    pwd
    echo "-----------------"
    cd org_dir
    pwd
    echo "-----------------"

  3. #3
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default Oh, what?

    Sorry, I can't understand you. First of all: your class compiles, right? So there is no error. I think that your program executes correctly, but it has NO PLACES to show the output of the echo command. In other words, when you execute an environment command this way
    myRuntime.exec("myunix command -h");
    the JVM starts a new child process and executes your command. But, TO SEE IT WORKING, it must have a place to display the result and, in this case, it doesn't have such a place. Your command does get executed, but you simply don't see its output. I hope I've guessed your problem. I think that it's slightly more difficult to see the visual output of a unix command. You'd better use a java one...

    A trick... Avoid all those "this.<member>", they are unnecessary. You can use them in case of name collisions, but not there. There is no need for them. ;-)

    A lil curiosity... Which unix commands do you happen to call from your java programs? Do you really need them? Keep in mind this is a non-portable practice. You should avoid it. If you need cd, pwd, ls, echo... there are standard java classes for do that, and in a much clearer way

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default my class

    Thanks for the reply

    I have output and error streams , I build this classi to make it easy .
    anyway - the output and the error goes to string vector and then to string array. I dont quite sure how to handle the catch things , but it is not critical now.
    it work for "ls" but not for "cd"


    Here is the classs: ( without some getter and setter..and only one clonstructor )

    public class RunCommand2 {
    // argument
    private String command;
    private int isDone; // caled it - 1 , didnt called it - 0
    private int isFail; // after called - 1 fail, 0 succecc
    private String outStd [];
    private String errStd [];
    public IOException exception;
    // constructor
    public RunCommand2 (String command1,int doit, boolean printOut, boolean printErr)
    {
    this.isDone =0; this.isFail=0; this.command=command1;
    if (doit!=0)
    this.perform(printOut,printErr);
    }
    // ****************************************
    // the method pefrorm - call the unix function

    public int perform()
    {
    String s=null;
    try {

    Process p = Runtime.getRuntime().exec(this.command);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    // read the output from the command
    Vector <String> temp_out=new Vector<String>();
    while ((s = stdInput.readLine()) != null) {
    temp_out.add(s);
    }
    if (temp_out.size()>0){
    this.outStd= new String[temp_out.size()];
    temp_out.copyInto(this.outStd);
    }
    // read any errors from the attempted command
    temp_out.clear();

    while ((s = stdError.readLine()) != null) {
    temp_out.add(s);
    }
    if (temp_out.size()>0){
    this.errStd= new String[temp_out.size()];
    temp_out.copyInto(this.errStd);
    }



    this.isDone=1;
    this.isFail=0;
    } // end of try
    catch (IOException e) {
    this.exception=e;
    System.out.println("exception happened - here's what I know: ");
    e.printStackTrace();

    this.isDone=1;
    this.isFail=1;
    return 0;
    }
    return 1;
    }

    public int perform(boolean printOut, boolean printErr)
    {

    int return1=this.perform();

    if (return1>0&&printOut)
    this.printOut();
    if (return1>0&&printErr)
    this.printErr();
    return return1;
    }

  5. #5
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default a courtesy

    Please post another time
    1. Avoid all those this.<member>. It's inelegant and boring. You are already referencing to your class, don't worry! ;-)

    2. Use the CODE button to format the text into some readable fashion. Paste your source code, select it, then click on the 3rd button from the right (I don't know how it's called, maybe ?star? mah...)

    Then someone will help you
    Bye

  6. #6
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    This is from official Sun API for Process class:
    The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
    The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
    And this is your class, which I modified to focus on your problem. It compiles, but still doesn't do what you want:
    Java Code:
    import java.io.*;
    import java.util.Vector;
    
    public class RunCommand2 {
    	private String command;
    	private String[] stdOut;
    	private String[] stdErr;
    	
    	
    	public RunCommand2 (String command) {
    		this.command = command;//Here "this" is useful
    	}
    	
    	// the method call the unix command
    	public int perform() {
    		try {
    			Process p = Runtime.getRuntime().exec(command);
    			BufferedReader stdInput = new BufferedReader(
    					new InputStreamReader(p.getInputStream()) );
    			BufferedReader stdError = new BufferedReader(
    					new InputStreamReader(p.getErrorStream()) );
    			
    			// read the output of the unix command
    			Vector<String> temp_out = new Vector<String>();
    			
    			String s = "";
    			while ((s = stdInput.readLine()) != null) {
    				temp_out.add(s);
    			}
    			if ( temp_out.size() > 0){
    				stdOut = new String[temp_out.size()];
    				temp_out.copyInto(stdOut);
    			}
    			
    			// read any errors from the attempted command
    			temp_out.clear();
    			while ((s = stdError.readLine()) != null) {
    				temp_out.add(s);
    			}
    			if ( temp_out.size() > 0){
    				stdErr= new String[temp_out.size()];
    				temp_out.copyInto(stdErr);
    			}
    			
    			//Normal execution ended
    			return 1;
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    			return 0;
    		}
    	}
    	
    	public static void main(String... cmds) {
    		//First we check if it's invoked correctly
    		if (cmds.length < 1){
    			System.out.println("Usage: java RunCommand2 " +
                                    "<your_unix_command> <separated> <by_spaces>");
    			System.exit(1);
    		}
    		
    		for (String cmdName : cmds) {
    			System.out.println("Executing " + cmdName + "...");
    			RunCommand2 myCmd = new RunCommand2(cmdName);
    			myCmd.perform();
    			System.out.println();
    		}
    		
    		
    	}
    }
    Last edited by raffaele181188; 12-29-2008 at 01:02 PM.

  7. #7
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default "cd" function does not work because it doesnt have terminal?

    The created subprocess does not have its own terminal or console
    I guess that is the reason function like "cd .." does not work , right ?

    Thanks

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Regarding "install" command
    By Kavitha Kamath in forum New To Java
    Replies: 1
    Last Post: 11-21-2008, 05:30 AM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. Replies: 0
    Last Post: 09-18-2008, 05:07 PM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •