Link to home
Start Free TrialLog in
Avatar of rnicholus
rnicholus

asked on

Run "mysqldump" command from Java program

I'm trying to run "mysqldump" command from my Java program (please see code snippet).
But it gives me the following error:
ERROR:mysqldump: Couldn't find table: ">"

Can someone please advise?
Thanks in advance for the help.

Runtime shell = Runtime.getRuntime();
Process proc = shell.exec("mysqldump -uusername -pmypassword -d startdb markets > " + currentDir+ "\\\\mkts.dump" );

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That should probably be (on Windows)
Process proc = shell.exec("mysqldump -uusername -pmypassword -d startdb markets > " + currentDir+ "\\mkts.dump" );

Open in new window

You must see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
if you encounter blocking problems
Better would be something like:
Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "-d", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

Open in new window

Avatar of rnicholus
rnicholus

ASKER

CEHJ, the solution on your first post doesn't work. It throws the same error.
This below also doesn't work.

Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "-d", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

It still gives the same error: ERROR:mysqldump: Couldn't find table: ">"
The switch is wrong i think. Try
Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

Open in new window

CEHJ,

It still doesn't work.
What do you mean by 'doesn't work' - what's the error now?
you can't use redirection like that. If you want to redirect then you need to use a shell.
or read the output into a file yourself.

try:

Process proc = shell.exec(new String[] {"bash", "-c", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

CEHJ,
It still says: ERROR:mysqldump: Couldn't find table: ">"

objects, let me try your solution.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
objects,

I tried:
Process proc = shell.exec(new String[] {"bash", "-c", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

It gives me the following error:
java.io.IOException: CreateProcess: bash -c mysqldump -uusername -ppassword startdb markets > C:\myfolder\mkts.dump error=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
    at java.lang.Runtime.exec(Runtime.java:591)
    at java.lang.Runtime.exec(Runtime.java:464)
    at testing.main(testing.java:52)
my preference would be the second suggestion

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> java.io.IOException: CreateProcess: bash -c mysqldump -uusername -ppassword startdb markets > C:\myfolder\mkts.dump error=2

if you are on windows then you would of course use cmd shell instead of bash.
Another reason why my second suggestion is better as it is less platform dependent

Thanks for the solution! :)