This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Runtime getRuntime() exec(String command)  - How does this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime getRuntime() exec(String command)  - How does this work?" Watch "Runtime getRuntime() exec(String command)  - How does this work?" New topic
Author

Runtime getRuntime() exec(String command) - How does this work?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Originally posted by Ulf Dittmer:


I didn't? Not on 4/21/2006?
Yes, you did. I missed it.

I am very sorry.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
The nerve! To miss a two year old post that was only 29 posts into the discussion!

(That was irony, obviously - no need to be sorry.)


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
It only took you 3 years to quote Daconta, then.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
Sachin Telang
Greenhorn

Joined: Nov 02, 2007
Posts: 19
@Campbell Ritchie...

I said it is informative because whatever information provide here is new to me. May be I am not expert as you are?



Thanks<br />Sachin Telang<br />IBM Websphere Portal Application Developer Certified, Exam -399.
Sun Certified Java Programmer 1.5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
If you read Ulf Dittmer's posting on 10th July, you find that my comment about not informative was a mistake. Sorry.

The link to Daconta's article is very useful; please make sure to read it. There are different ways to handle the text outputs; I have tried putting them into List<String>s and printing the contents of the Lists, which worked well, only had to be followed by a 1-second delay with Thread.sleep().
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Hi Members,


I have to restore my mysql database using java application.

I have to run mysql command line syntax from java runtime.exec(String command).

I tried to do so--->

Runtime.getRuntime().exec("cmd /c mysql --host localhost --port 3306 -u root -p sasa attestdb < c:/backup.sql");

If i run this, im getting the 'mysql version and its option details displayed in the console'... instead my database is not restored/updated.

If we use cmd or cmd.exe in the exec() method, it will work only in windows, but for me it should work both in linux and windows..

Please help me int this...

Looking forward for your reply...


regards,
Prabhu.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Hi Members,

I have to execute mysql command line syntax from java runtime, that should work in both windows and linux machine. I tried using the Runtime.getRuntime().exec("cmd /c mysql -u username -p pass databasename < c:backup.sql");

But this is not restoring my database, instead im getting the mysql version and options information in the console trace.

Please help me to figure out this issue...

Some times it just hangs...

Regards,
Prabhu.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
I would suggest you start a new thread on that subject on the JDBC forum. As it says in this FAQ, you are less likely to get useful answers on a 5-year old thread, and your query may actually be database-related.
Ahmed Zed
Greenhorn

Joined: Jan 22, 2010
Posts: 1
I know this threat is a bit too old, but maybe someone can help me out.
Well, trying to create something that kills a specific running application on any computer on the network at a certain time (Wordpad.exe for example). Trying to do this with a Java Applet but it doesn't seem to work at all, no matter what I do.



Could someone help me out please? Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

1) It doesn't work is useless.

2) Read the Javaworld article "When Runtime.exec() won't".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
felipe melo
Greenhorn

Joined: May 04, 2010
Posts: 1
Hi everybody, even if this is an old thread, I really hope for some help...
I have a problem with my code:


The output, when I execute my code, is:


How can I solve this problem? Is there a way to allow long file names?
Thank you in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

How long is your name? Anyway, that error=36 indicates that it isn't caused by any Java code but by the actual process.
Sangaran Nagendran
Greenhorn

Joined: Dec 05, 2011
Posts: 2
Hi All,

I am trying to execute the following pice of code

Process pjavade = Runtime.getRuntime().exec("cmd /C java -version");
BufferedReader stdJavadeInput = new BufferedReader(new
InputStreamReader(pjavade.getInputStream()));
System.out.println("Here is the standard output of the Java Default:\n");
while ((striterate = stdJavadeInput.readLine()) != null) {
System.out.println("Inside While loop");
System.out.println(striterate);
}

but i couldn't see the out put in " System.out.println(striterate); but if i manually run java -vesion in command window i can see my java version..
please let me know what went wrong....

Thanks
Sangaran
"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Welcome to the Ranch!

If you execute "java -version" the output is not sent to standard output (stdout), but to the standard error stream (stderr). This means that you shouldn't read from getInputStream() but from getErrorStream(). In fact, you usually should read from both; read http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html for more information.
Sangaran Nagendran
Greenhorn

Joined: Dec 05, 2011
Posts: 2
Thank you so much it works fine now

Sangaran
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
Prabha karan
Greenhorn

Joined: Dec 09, 2011
Posts: 2
Hello,

i am planning to execute a dos commands in the cmd window via Java code.
I saw all of your examples and i tried to do some code...

find my code snippet

try {
Process p = Runtime.getRuntime().exec(new String[]{"cmd,"/C","ls-l","C:\mydir\"});
/*
-----
getting the Process P data and printing in the cmd window
-----
*/
}
catch (IOException e)
{
e.printStackTrace();
}

It works fine for "ls -l" but not working for "dir" command
"If the path is not the current workig directory"

eg:
exec(new String[]{"cmd,"/C","ls-l"})

Both the commands are working fine if the command runs for the current directory.

I dont know why this problem comes

I have one more doubt also in that code if i use "/c" only it executes the command otherwise it only invokes the cmd window what is the
exact meaning of the "/C

Thanks in advance!!

Thanks
Karan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

/C means that the next part is the command that should be executed inside the command window.

But are you sure that the command is "ls-l"? Should it not be "ls -l", with a space? The former is a call to an application called "ls-l". The second one is a call to an application called "ls" with argument "-l". The space is really important here.
Prabha karan
Greenhorn

Joined: Dec 09, 2011
Posts: 2
You are correct that is my typo mistake "ls -l".
Thanks for the meaning of "/C"

Do you have any idea why the "dir" command is not working only "ls -l" works..

Regards
Prabahkaran
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Ralph Ostrander,
Your post was moved to a new topic.
kathula kiran
Greenhorn

Joined: Oct 06, 2013
Posts: 1
Michael Morris wrote:
This just runs the dir command, captures its ouput and copies it to the programs stdout. Not very exciting but it shows the basic parts to use Runtime.exec(). You can also open up the process' output and error streams.





can you please give me the code for giving input to it also?
i want to execute other java programs using command prompt and get its output to my screen. please respond as soon as possible
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Write to the process' output stream. You can wrap that in a PrintStream if you want.
ashwin Joshi
Greenhorn

Joined: Jun 18, 2014
Posts: 1

I want to execute a command in my java program using below function.

Process p = Runtime.getRuntime().exec();

My command om cmd prompt looks something like

C:\Program Files\IBM\MQSI\9.0.0.0>mqsicacheadmin IB9NODE -c clearGrid -m myMapName

Please do tell correct format of writing above command inside exec() block am getting some error when tried.

Thanks in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

What do you have so far? I'm guessing you're missing some double quotes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime getRuntime() exec(String command) - How does this work?
 
Similar Threads
Fetching code from Serena [Version control] using ANT tasks
the exec command is not able to deal with space some time
Run Oracle Script from within JSP
Regular Expressions, Where are they?
Open browser/web page from a swing app