954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help to run Command Prompt from java program

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.

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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 :((

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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?

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

java.io.IOException: Cannot run program "start": CreateProcess error=2,
manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

its ok, thanx a lot for making an effort

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 

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

babusek
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

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

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

manisha
Newbie Poster
21 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

babusek
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

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.

alinutza200483
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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.

PhiberOptik
Junior Poster
164 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

Hi Manisha,

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

sramaraj
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Try this:

try {
	    Runtime.getRuntime().exec("cmd /c start");
	}
	catch (IOException e){	}
silverastrid
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

papsofts
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You