943,771 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 12758
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 22nd, 2008
0

Re: Need help to run Command Prompt from java program

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?th...sageID=4351862
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Oct 23rd, 2008
0

Re: Need help to run Command Prompt from java program

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 ,

Quote ...
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..
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 23rd, 2008
0

Re: Need help to run Command Prompt from java program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manisha is offline Offline
21 posts
since Jun 2005
Oct 23rd, 2008
0

Re: Need help to run Command Prompt from java program

Sorry i dont have any idea about cygwin env


Click to Expand / Collapse  Quote originally posted by babusek ...
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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manisha is offline Offline
21 posts
since Jun 2005
Oct 23rd, 2008
0

Re: Need help to run Command Prompt from java program

ok, if u you know the answer please post here..
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Feb 26th, 2009
0

Re: Need help to run Command Prompt from java program

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

Java Syntax (Toggle Plain Text)
  1. String[] command = { "cmd.exe", "/C", "Start"};
  2. Runtime.getRuntime().exec(command);

Works for me. Hope it helps.
Last edited by peter_budo; Nov 17th, 2011 at 6:17 am. Reason: Adding code tags to old post
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alinutza200483 is offline Offline
1 posts
since Feb 2009
Feb 26th, 2009
0

Re: Need help to run Command Prompt from java program

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.
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Mar 2nd, 2010
0
Re: Need help to run Command Prompt from java program
Hi Manisha,

U can try this out!
String Command = "C:/WINDOWS/system32/cmd.exe";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sramaraj is offline Offline
1 posts
since Mar 2010
Mar 4th, 2010
0
Re: Need help to run Command Prompt from java program
Try this:

JAVA Syntax (Toggle Plain Text)
  1. try {
  2. Runtime.getRuntime().exec("cmd /c start");
  3. }
  4. catch (IOException e){ }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silverastrid is offline Offline
1 posts
since Mar 2010
Apr 2nd, 2010
0

Hi Manisha this might help you...

Click to Expand / Collapse  Quote originally posted by manisha ...
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.
Java Syntax (Toggle Plain Text)
  1. String cmd="copy \"c:\\file.txt\" \"c:\\fil_copy.txt\" ";
  2. String command="cmd /c "+cmd;
  3. Runtime.getRuntime().exec(cmd);

The "/c" specifies the system to consider "cmd" as a command.
Last edited by peter_budo; Nov 17th, 2011 at 6:18 am. Reason: Adding code tags to old post
Reputation Points: 10
Solved Threads: 0
Newbie Poster
papsofts is offline Offline
1 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Basic GUI question
Next Thread in Java Forum Timeline: How does one clone objects with an arraylist of the same object?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC