0

I'm just trying to open a command prompt window using java program (in eclipse). When i run pgm as below, it's not showing any message

Runtime.getRuntime().exec("cmd");

But when i try to open internet explorer using below line

Runtime.getRuntime().exec("iexplore");

It's throwing below error

Exception in thread "main" java.io.IOException: CreateProcess: iexplore error=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at dev.petrofac.ChangeFilePermission.main(ChangeFilePermission.java:17)
8
  • If it isn't throwing an error, doesn't that mean it's working? Commented Nov 12, 2011 at 14:54
  • It's not opening command prompt window so i guess it's not working Commented Nov 12, 2011 at 14:55
  • try iexplore.exe, see about-java-programming.blogspot.com/2007/02/… Commented Nov 12, 2011 at 14:55
  • yeh i tried iexplore.exe but it's throwing same error. also for cmd i tried C:\WINDOWS\System32\cmd.exe but no use :( Commented Nov 12, 2011 at 14:57
  • just to be sure, you have put double escape characters in string literal of the full command path ? (i.e. `cmd = "C:\\WINDOWS\\...") Commented Nov 12, 2011 at 15:00

2 Answers 2

4

The fact is that you are starting cmd. Just because you can't see it doesn't mean it doesn't start.

If you want to see the output from the application, you need to get the outputstream (see Process for details). If you want to start cmd in a new window you can execute the string "cmd.exe /c start cmd.exe" instead, as in

Runtime.getRuntime().exec("cmd.exe /c start cmd.exe");
Sign up to request clarification or add additional context in comments.

5 Comments

@patric wow..this seems to be working. But still not getting why it's now working when we put just cmd
@Sukumar, if you open a cmd and then type cmd you will see that nothing happens. Same for your program: the java app is run as a console application, so simply running cmd from there without /c start will not produce any effect.
@Sukumar: You can compare this with when you open cmd normally, and in that cmd window type cmd. You don't get a new window, but rather the new cmd is executed within the old cmd. The same thing happens with your exec call, but it's not brought to the foreground. The start argument is the reason why a new window appears, just as when you do it in a cmd window.
Thanks a lot. Also can you pls tell me what is the significance of "/c" in the command
@Sukamar: /C Carries out the command specified by string and then terminates. Type cmd /? in a command prompt to read about more flags.
0

Try checking the file permissions for iexplore.

Allow execute privileges for all processes/users , if not set already.

1 Comment

For iexplore when i give whole path as Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe"); it's working. But still cmd is not working though i give full path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.