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.

Recommended Answers

All 22 Replies

When you say it's not working, do you get an error message?

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.:-/

Sorry manisha but I am out of ideas. Maybe someone else can help you?

its ok, thanx a lot for making an effort

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..

ok, if u you know the answer please post here..

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.

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 Manisha,

U can try this out!
String Command = "C:/WINDOWS/system32/cmd.exe";

Try this:

try {
	    Runtime.getRuntime().exec("cmd /c start");
	}
	catch (IOException e){	}

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.

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

How to open cmd at specific location

@Farheen_3: not by reviving dead threads. start a new thread, show what you've tried, and we'll start from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.