• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Originally posted by Ulf Dittmer:


I didn't? Not on 4/21/2006?

Yes, you did. I missed it.

I am very sorry.

 
Rancher
Posts: 43081
77
  • Number of slices to send:
    Optional 'thank-you' note:
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
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
It only took you 3 years to quote Daconta, then.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Number of slices to send:
    Optional 'thank-you' note:
 
Greenhorn
Posts: 19
  • Number of slices to send:
    Optional 'thank-you' note:
@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
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
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().
 
Ranch Hand
Posts: 147
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Greenhorn
Posts: 1
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Sheriff
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
1) It doesn't work is useless.

2) Read the Javaworld article "When Runtime.exec() won't".
 
Greenhorn
Posts: 1
  • Number of slices to send:
    Optional 'thank-you' note:
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
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
How long is your name? Anyway, that error=36 indicates that it isn't caused by any Java code but by the actual process.
 
Greenhorn
Posts: 2
  • Number of slices to send:
    Optional 'thank-you' note:
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
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
Thank you so much:) it works fine now

Sangaran
 
Rob Spoor
Sheriff
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
You're welcome.
 
Greenhorn
Posts: 2
  • Number of slices to send:
    Optional 'thank-you' note:
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
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
/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
  • Number of slices to send:
    Optional 'thank-you' note:
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
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Ralph Ostrander,
Your post was moved to a new topic.
 
Greenhorn
Posts: 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
Write to the process' output stream. You can wrap that in a PrintStream if you want.
 
Greenhorn
Posts: 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
Posts: 22784
131
  • Number of slices to send:
    Optional 'thank-you' note:
What do you have so far? I'm guessing you're missing some double quotes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |