Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only took you 3 years to quote Daconta, then.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sachin Telang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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?

 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It doesn't work is useless.

2) Read the Javaworld article "When Runtime.exec() won't".
 
felipe melo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much it works fine now

Sangaran
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Prabha karan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Ostrander,
Your post was moved to a new topic.
 
kathula kiran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write to the process' output stream. You can wrap that in a PrintStream if you want.
 
ashwin Joshi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far? I'm guessing you're missing some double quotes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic