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

    Lightbulb help with Runtime.exec()

    hi to all,

    need some help regarding runtime.exec. i got two questions:

    1st Q:
    when i used these code it works fine
    Java Code:
    Process p = Runtime.getRuntime().exec("cmd /c start c:\\some.exe");
    but when i tried a variation like these,
    Java Code:
    Runtime p = Runtime.getRuntime();
    String[]commands={"cmd /c start c:\\some.exe"};	
    Process x = p.exec(commands);
    it compiles but when i run it error occurs "Cannot run program cmd /c start c:\\some.exe...The system cannot find the file specified etc etc..."

    i cant figure whats wrong with the 2nd variation :confused:

    2nd Q:
    i was able to call an external application using runtime.exec, when some.exe is called it will open a new command console with options like:

    1 - Enter new Name
    2 - Input text file where to save
    3 - Exit

    Enter choice:_

    i was wondering how to do when my java calls the some.exe (pops up the new console with those options) my java will automatically close the old console (the console where i run my java so only the new console remains) then automatically chooses the option 1 (Enter new Name) without having me to type 1 and then press enter in the new console. :rolleyes:

    thanks in advance :D

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Java Code:
    Runtime p = Runtime.getRuntime();
    String commands={"cmd /c start c:\\some.exe"};	
    Process x = p.exec(commands)

  3. #3
    Lanfear is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    tried your code
    Java Code:
    String commands={"cmd /c start c:\\some.exe"};
    but error: it says { illegal initializer
    so i removed the { }
    Java Code:
    String commands="cmd /c start c:\\some.exe";
    and it works, but i saw lots of example code using array to store multiple strings for the runtine exec to run like these one:
    Java Code:
    String[]callAndArgs= {"D:\\applns\\Office2000\\Office\\Msaccess.exe","D:\\docs\\db7.mdb","/cmd","report1"};

    how about my second question? is it possible for java to do that? :confused:

    thanks for the answer though :)

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Yes sure remove curly brackets, i didn't pay attention.
    And for your second question:
    Java Code:
    // In case developer wants to execute a command with more than 
    // one argument, it is necessary to use the overload that requires 
    // the command and its arguments to be supplied in an array:
    
    try {
        // Execute a command with an argument that contains a space
        String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
        
        commands = new String[]{"grep", "hello world", 
                "c:\\Documents and Settings\\f.txt"};
                
        Process child = Runtime.getRuntime().exec(commands);
        
    } catch (IOException e) {
    }

  5. #5
    Lanfear is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    tried it

    Java Code:
     try
          {
    	Runtime p = Runtime.getRuntime();
    	String[] commands= {"cmd /c start c:\\some.exe", "cmd /c start c:\\newsome.exe"};
    	commands = new String[] {"cmd /c start c:\\some.exe", "cmd /c start c:\\newsome.exe"}; 
    	Process q = p.exec(commands);
           }
          catch(IOException e)
          {
             System.err.println("Error on exec() method");
             e.printStackTrace();
          }
    it compiles but error is Cannot run program "cmd /c start c:\\some.exe".. The system cannot find the file specified

    same error as before :(

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Yes, "Daughter of the Night" (and if you're not a Robert Jordan fan, forget that). If you are going to use the array form of the runtime exec method then it goes like this
    Java Code:
    String commands= { "cmd", "/c", "start", "c:\\some.exe" };
    Read the following:

    When Runtime.exec() won't - JavaWorld

    thoroughly and completely, and implement the suggestions given there.

    And actually read it, don't just skim through it (like nearly every other poster that asks these questions) for something that you believe pertains to your problem here, or you'll miss 90 - 100% of the benefit of the article (and there is a large benefit).

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

    Default

    i like dragonlance and forgotten realms more than WoT, but i like the character Lanfear :D

    back to topic:

    i tried what you said:
    Java Code:
    String commands= { "cmd", "/c", "start", "c:\\some.exe" };
    error is on the curly brackets: illegal initializer for java.lang.String

    will read the guide you provided after i posted this reply ;)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    First of all you have to remove curly braces from the following line, as masijade says.

    Java Code:
    String commands= { "cmd", "/c", "start", "c:\\some.exe" };
    Then you have to read more about arguments in runtime() use here. You must define the correct *.exe and the path that executable application is located. Since some.exe is not found in the defined path you get the error.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Lanfear View Post
    i like dragonlance and forgotten realms more than WoT, but i like the character Lanfear :D

    back to topic:

    i tried what you said:
    Java Code:
    String commands= { "cmd", "/c", "start", "c:\\some.exe" };
    error is on the curly brackets: illegal initializer for java.lang.String

    will read the guide you provided after i posted this reply ;)
    Typo
    Java Code:
    String[] commands= { "cmd", "/c", "start", "c:\\some.exe" };
    Are you sure you're ready for any of this if you weren't able to figure out that it was declared a String, but defined as a String array, and so should be declared as a String[] ?

    Come on, I don't mean to be mean, but really. If that one was too, hard you should be going here:

    The Java™ Tutorials

    And working through all of those tutorials from top to bottom starting with

    Trail: Getting Started (The Java™ Tutorials)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Definitely, our thread starter must work on with that tutorial. masijade gives all the information you want. If you cannot figure them out so far, better to deal with Java basis.

  11. #11
    Lanfear is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    didnt mean to be rude or ungrateful but i just tried what masijade said on his post, he said it was suppose to be in that form:

    Quote Originally Posted by masijade View Post
    If you are going to use the array form of the runtime exec method then it goes like this
    Java Code:
    String commands= { "cmd", "/c", "start", "c:\\some.exe" };
    if you look on the 3rd post on this thread serjant also suggested that same form, but i said it resulted in an error so i removed the curly brackets for it to work. and like what i said on my first post i can make runtime work using the simpler form like
    Java Code:
    Process p = Runtime.getRuntime().exec("cmd /c start c:\\some.exe");
    im just trying to use string array on runtime but somehow it just wont work. :(

    but using this form, it works fine:

    Java Code:
           Runtime p = Runtime.getRuntime();
    	String[] commands = new String[4]; 
    	commands[0] = "cmd";
    	commands[1] = "/c";
    	commands[2] = "start";
    	commands[3] = "c:\\some.exe" ;
    		
    	Process q = p.exec(commands);
    Last edited by Lanfear; 12-15-2008 at 11:36 AM.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    And have you tried the "corrected" version in my last post?

    If you are "trying the array version" as you say, then you should know that an array needs to be declared as an array (i.e. String[] and not String).

    If you have tried the "corrected" version, then describe "won't work".

    Also, did you thoroughly read the link from my first post in this thread, noting that I explicitly said not to simply skim through it (somehow I doubt it).

  13. #13
    Lanfear is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    when i said that what you suggested: String commands= { "cmd", "/c", "start", "c:\\some.exe" }; doesnt work it was before your post with correction of the typo.

    sorry i forgot to indicate in my last post that your corrected version works.
    and yes i read the tutorial you provided, i even compile and run the examples given there.

  14. #14
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Have you seen the fourth post?

    Java Code:
    // In case developer wants to execute a command with more than 
    // one argument, it is necessary to use the overload that requires 
    // the command and its arguments to be supplied in an array:
    
    try {
        // Execute a command with an argument that contains a space
        String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
        
        commands = new String[]{"grep", "hello world", 
                "c:\\Documents and Settings\\f.txt"};
                
        Process child = Runtime.getRuntime().exec(commands);
        
    } catch (IOException e) {
    }

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    We both have, but I posted "again" because it wasn't the same command being used, so the OP didn't understand (seemingly).

    Of course, I must say that I don't understand why you defined the array twice. ;-)

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Lanfear View Post
    i even compile and run the examples given there.
    I do need to comment though, that that is a simple cut-n-paste.

    Do you actually understand them?

  17. #17
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    No,i just wanted to show how to execute the same command on two different OSes,The first array is for Linux and the second one is for MS

  18. #18
    mark24 is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default new in java

    hello friends

    i am a beginner in java(CUI), so i am facing some problems in it. so

    can you guys provide me the basic tutorial for java(CUI),through which i can get

    help,i am in search of simple and at beginner point of views tutorial with

    examples, as i am studing java by myself without any tutor so thats why i am in

    search of basic tutorials you can connect me at my email

    (mark_gagan@yahoo.co.uk)

    thank you

  19. #19
    Lanfear is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    thansk to positive replies and criticism my first problem is solved :D and thanks for the guide provided by masijade, very helpful.

    but im still trying to solve my second problem:
    the scenerio is
    i use runtime to spawn an external program
    a new console-type windows open for the external program
    then the external program displays possible options and "Enter Choice: _ "
    it only accepts 1 or 2 or 3 or 4 or X (for exit) as inputs

    im trying to do is when i run my java it will spawn the external program
    and it will also automatically choose options i indicated in my java.
    example is if i want to choose option 2 then X (exit), my java will spawn
    the external program then chooses option 2 then chooses option X to exit the external program.

    any help or suggestion is very much appreciated :)

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2008, 06:56 AM
  2. runtime.exec stale process
    By karine in forum Advanced Java
    Replies: 6
    Last Post: 10-06-2008, 05:40 PM
  3. Tomcat and Runtime.exec
    By karine in forum Introductions
    Replies: 1
    Last Post: 10-03-2008, 08:25 AM
  4. Runtime.exec()
    By hknyo in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:40 AM
  5. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 01:35 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
  •