Results 1 to 20 of 20
  1. #1
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Launching an exe

    Hi All,

    I am trying to launch an exe from JAVA using
    Process p = Runtime.getRuntime().exec(exeLocation);

    This launches the exe but the problem is when I double click on the exe, it opens a command prompt as a console, which gets minimized on the taskbar.

    I am not able to get this command prompt when launching the exe from the code.

    Please help.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    What is the executable that you are launching?

    Can you provide some more code? The details provided by you are not sufficient.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Do you want to retrieve the output from the exe or communicate in some way? Get the InputStream/OutputStream from the Process and read/write as needed.

  4. #4
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    The exe that i am trying to launch is an application made in Visual Studio & the command prompt that it shows on double click is nothing but a console.

    However, I am unable to see this console/command prompt on launching the exe through the java code.

    Code snippet :

    String s;
    Process p = Runtime.getRuntime().exec(exelocation);
    BufferedReader inputReader =new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = inputReader.readLine()) != null) {
    System.out.println(s);
    }
    input.close();

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by kanupriya View Post
    Process p = Runtime.getRuntime().exec(exelocation);

    What is the exelocation above? Are you providing the correct full path, including the extension?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Yes, I am providing the correct path with the extension. That's why I am able to launch the exe.

    The problem is not with launching the exe, it is about showing the console (a command prompt) attached with it. Currently, it is not showing.

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by kanupriya View Post
    Yes, I am providing the correct path with the extension. That's why I am able to launch the exe.

    The problem is not with launching the exe, it is about showing the console (a command prompt) attached with it. Currently, it is not showing.
    What usually happens when you manually open your exe file? Does it opens that console in the first place, which you are talking about? If yes, then it should happen the same way through java.

    Do you need to click somewhere or pass any commands to have that console appear? If that's the case then you have to make sure that you are passing all those supplementary things through your code as well.

    It would be nice if you can put your whole code instead of bunches.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    What usually happens when you manually open your exe file? Does it opens that console in the first place, which you are talking about? If yes, then it should happen the same way through java.

    Do you need to click somewhere or pass any commands to have that console appear? If that's the case then you have to make sure that you are passing all those supplementary things through your code as well.

    It would be nice if you can put your whole code instead of bunches.

    Goldest
    When I manually open the exe (double clicking on the shortcut), the console opens.

    public class Process {
    public static void main(String args[]) {
    String s;
    Process p = Runtime.getRuntime().exec(exelocation);
    BufferedReader inputReader =new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = inputReader.readLine()) != null) {
    System.out.println(s);
    }
    inputReader.close();
    }
    }


    On running the main class, the application launches w/o the console.

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by kanupriya View Post
    public class Process {
    This seems more of a naming conflict. We already have the Process class in the java.lang package. This might spoil the things.

    Can you simply change your public class name to something else than Process ? Make it Process1 or something and re-run the program.

    See what happens?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    This seems more of a naming conflict. We already have the Process class in the java.lang package. This might spoil the things.

    Can you simply change your public class name to something else than Process ? Make it Process1 or something and re-run the program.

    See what happens?

    Goldest
    No change. It still doesnot show a console.

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by kanupriya View Post
    No change. It still doesnot show a console.
    Can you post the full new code? Because even in your previous code, you are neither handling the exceptions nor throwing them from your main method.

    The compiler should ask you to handle the exception that might arise when you try to use the readLine().

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  13. #13
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    public class TestExe {
    public static void main(String args[]) throws InterruptedException,IOException {
    String s;
    Process p = Runtime.getRuntime().exec(exelocation);
    BufferedReader inputReader =new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = inputReader.readLine()) != null) {
    System.out.println(s);
    }
    inputReader.close();
    }
    }

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I am assuming that you have used the import statements correctly. In the same code given by you, I just placed the path to one of my executable, like,
    Java Code:
    Process p = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/iexplore.exe");

    When I ran this, it just worked fine. It opened the internet explorer for me. I wonder how you are getting issues in launching your exe file.

    What is the path you are providing for "exelocation"? Could you please post it here? In fact your code never declared this String or passed it to the exec method. From where are you passing this?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  15. #15
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    I do not have a problem in launching the exe.

    The problem is that the application that I am launching has a console which doesn't show when I execute/launch it using :

    Runtime.getRuntime().exec(<some exe location>);

  16. #16
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    It shouldn't happen basically. If this is the only exe file that is responsible for launching your application, then it should come along with all the features.

    Make sure that your application is not having any inter dependencies other than this executable.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    When the OS starts a process is attaches three streams to it: stdin, stdout and stderr. The default streams form a 'console' or 'terminal'. When no streams are supplied the OS uses the default Streams. The ProcessBuilder supplies those three Streams because the parent process must be able to read from the process and write to it; that's why no console starts up.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The ProcessBuilder supplies those three Streams because the parent process must be able to read from the process and write to it; that's why no console starts up.
    How can I get the console then? Is there a way to start it?

    Thanks
    Kanupriya

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kanupriya View Post
    How can I get the console then? Is there a way to start it?
    Sure, just start it but you won't see its window because the Streams are supplied by your Java program. On Unix/Linux one can start a shell in 'interactive' mode by supplying a flag. I don't know if that feature exists in MS Windows. Consult the (online) manual for the cmd.exe program.

    kind regards,

    Jos

    ps. or even nicer: write your own terminal program. It can be done without too many problems.
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    kanupriya is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Hi everyone,

    I just came to know that the exe, I'm trying to execute, is a console application.
    Do I need to pass any arguments to Runtime.getRuntime().exec for the console to show??

Similar Threads

  1. launching applet via jsp
    By rafzio4 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 08:35 AM
  2. launching .rm file outside java
    By nonboyx in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-28-2009, 07:40 PM
  3. Launching Eclipse Preferences from button
    By sarcasteak in forum Eclipse
    Replies: 3
    Last Post: 08-04-2009, 10:10 PM
  4. Launching applications without an IDE
    By this_is_phil in forum New To Java
    Replies: 6
    Last Post: 05-24-2008, 07:54 AM
  5. Launching Applet from a JSP page
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-31-2008, 12:53 PM

Tags for this Thread

Posting Permissions

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