Home arrow static arrow Java Programming [Archive] - Execute Linux command from Java
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Execute Linux command from Java
This topic has 4 replies on 1 page.

Posts:278
Registered: 9/1/04
Execute Linux command from Java  
May 21, 2004 12:21 AM



 
Hi!

In Java, if I have to execute any commands in Windows family OS, then I have to use following code.

...Runtime.exec("command.com /C dir");...

Now in a same manner if I want to execute any Linux command in Linux family OS, how can I achieve that?

Regards,
Chetan Parekh
 

Posts:4,689
Registered: 6/6/03
Re: Execute Linux command from Java  
May 21, 2004 12:27 AM (reply 1 of 4)



 
If it a shell script, meant for, say, "sh", then exec this:
{"sh" , "-c" , "myscript", "arg0," , "arg1" }
 

Posts:278
Registered: 9/1/04
Re: Execute Linux command from Java  
May 21, 2004 12:36 AM (reply 2 of 4)



 
Actully I want to take backup using MySQLDump.exe file in Linux. It is already there in my Classpath.
 

Posts:407
Registered: 11.11.03
Re: Execute Linux command from Java  
May 21, 2004 3:28 AM (reply 3 of 4)



 
Actully I want to take backup using MySQLDump.exe file
in Linux. It is already there in my Classpath.

I doubt that you can find an exe-file that runs on a linux box -
unless you renamed a simple shell script (or so) to .exe, that is ;)

Anyway - the way to execute "something" in a linux box is very similar to executing it in windows - you simply don't use cmd.exe and stuff, but something like
//weird shell magick;)String[] commandline={"/bin/sh", "-c", " ls -la ", " 2&>1 ", "/tmp/listing.txt" }; Runtime.getRuntime().exec(commandline);


-T-
 

Posts:3
Registered: 6/18/03
Re: Execute Linux command from Java  
Jun 23, 2004 9:55 AM (reply 4 of 4)



 
I am glad that I found your post. We do very much the same thing with our web application; that is we invoke the mysqldump program via Runtime.exec(...). This works great on our deployments to Windows, however, I have had no success on my Linux test machine. If it is not too much to ask, would you be able to maybe throw me a hint or quick example code fragment showing your dump command?
By the way, I tried (without success) using tomvollerthun's reply:

//weird shell magick;
String[] commandline={"/bin/sh", "-c", " ls -la ", " 2&>1 ", "/tmp/listing.txt" };

Thanks ahead of time.
--Dan
 
This topic has 4 replies on 1 page.