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

rnicholusAsked:
Who is Participating?
 
objectsCommented:
or:

Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "startdb", "markets" });
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("mkts.dump")));
InputStream in = proc.getInputStream();
// read bytes from in into out
http://helpdesk.objects.com.au/java/how-do-i-copy-one-stream-to-another-using-java

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

Open in new window

0
 
CEHJCommented:
You must see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
if you encounter blocking problems
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CEHJCommented:
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

0
 
rnicholusAuthor Commented:
CEHJ, the solution on your first post doesn't work. It throws the same error.
0
 
rnicholusAuthor Commented:
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: ">"
0
 
CEHJCommented:
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

0
 
rnicholusAuthor Commented:
CEHJ,

It still doesn't work.
0
 
CEHJCommented:
What do you mean by 'doesn't work' - what's the error now?
0
 
objectsCommented:
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.

0
 
objectsCommented:
try:

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

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

objects, let me try your solution.
0
 
rnicholusAuthor Commented:
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)
0
 
objectsCommented:
my preference would be the second suggestion

0
 
CEHJCommented:
Try
Process proc = shell.exec(new String[] {"cmd.exe", "/C", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePath() });

Open in new window

0
 
objectsCommented:
> 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

0
 
rnicholusAuthor Commented:
Thanks for the solution! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.