Hi,

I want to start the command prompt window from my java program.
Tried using Runtime but its not working.These are my codes :

String command = "cmd.exe";
			try {
				child = Runtime.getRuntime().exec(command);
			} 
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

Actually, its working when i change the value of command, like ita working when i put "notepad.exe". May be i m using a wrong command for cmd prompt. If anyone can figure it out, then i will be really thankful.

Edited 5 Years Ago by peter_budo: Adding code tags to old post

no i am not getting any error message. All other parts of the program are working well.Just at the end the command prompt is supposed to open.but it isnt doing so :((

Not sure if this will work, but the command for opening the cmd shell in a batch file is simply cmd. Maybe try changing the line

String command = "cmd.exe";

to simply

String command = "cmd";

and see what happens?

EDIT: Actually, if that doesn't work, try "start" instead of "cmd" as that is the command to open a new CMD shell from a running CMD shell.

It might also be worthwhile pointing out that this code will only work on windows operating systems. Really you should probably have this in an if-statement to ensure running this code only in a windows environment. An else statement might allow you to open a different shell in Linux, Mac, other environments?

Thanks,
Actually i did try "cmd" , and it was all the same. But i m trying again , will tell u the result.:|

hey dear, its not working.:(
i tried using "start" but its showing error

java.io.IOException: Cannot run program "start": CreateProcess error=2,

I am using Windows. But i am doing everything in Eclipse.Hope that doesnt make any difference.:-/

I don't know the exact solution. I may try once I go home. Just few suggestions:
1. Since Java 1.5 a new class ProcessBuilder has been introduced to replace exec(). So might be a good idea to try with ProcessBuilder and see if it works.
2. A work around can be to take user input, execute it, and display it. Might be a lot of work. You may find this useful: http://forums.sun.com/thread.jspa?threadID=762618&messageID=4351862

Hi manisha ,

I tried the same above but it's not opening the command prompt ,
it's compiled successfully , but it is hanging after that ,

sekharbabuk@sekharbabuk /cygdrive/c/j2sdk1.4.2_16/bin
$ javac RunUnixCommand.java

sekharbabuk@sekharbabuk /cygdrive/c/j2sdk1.4.2_16/bin
$ java RunUnixCommand
-----------###------------------------------------------
 Command = cmd.exe
--------------------------------------------------------
 File Found : Microsoft Windows XP [Version 5.1.2600]
 File Found : (C) Copyright 1985-2001 Microsoft Corp.
 File Found :

i tried this using cygwin environment in windows operating system..

thanks Orko,
but ProcessBuilder does the same. My problem is that in the end i need a command prompt window to open.Its working when i change the command from "cmd" to"notepad.exe". Its working then.I am really unable to figure out the problem.Anyways thanx for making an effort to help

Sorry i dont have any idea about cygwin env

Hi manisha ,

I tried the same above but it's not opening the command prompt ,
it's compiled successfully , but it is hanging after that ,


i tried this using cygwin environment in windows operating system..

I think it's "a bit" :P late, but... for anyone still "listening"...

String[] command = { "cmd.exe", "/C", "Start"};
Runtime.getRuntime().exec(command);

Works for me. Hope it helps.

Edited 5 Years Ago by peter_budo: Adding code tags to old post

Hi Manisha,
I tried the exact same thing you are attempting to do. I found that it is possible but very difficult (I never did it) I found the best way is to just launch cmd with arguments.

Hi,

I want to start the command prompt window from my java program.
Tried using Runtime but its not working.These are my codes :


String command = "cmd.exe";
try {
child = Runtime.getRuntime().exec(command);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Actually, its working when i change the value of command, like ita working when i put "notepad.exe". May be i m using a wrong command for cmd prompt. If anyone can figure it out, then i will be really thankful.

You will have to use the following code as the cmd line.

String cmd="copy \"c:\\file.txt\" \"c:\\fil_copy.txt\" ";
String command="cmd /c "+cmd;
Runtime.getRuntime().exec(cmd);

The "/c" specifies the system to consider "cmd" as a command.

Edited 5 Years Ago by peter_budo: Adding code tags to old post

Runtime runP = 	Runtime.getRuntime();
Process process =	runP.exec("cmd /c start");

Edited 5 Years Ago by peter_budo: Adding code tags to old post

This article has been dead for over six months. Start a new discussion instead.