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.
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" );
You must see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
if you encounter blocking problems
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() });
ASKER
CEHJ, the solution on your first post doesn't work. It throws the same error.
ASKER
This below also doesn't work.
Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "-d", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePa th() });
It still gives the same error: ERROR:mysqldump: Couldn't find table: ">"
Process proc = shell.exec(new String[] {"mysqldump", "-uusername", "-pmypassword", "-d", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePa
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() });
ASKER
CEHJ,
It still doesn't work.
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.
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").getAbsolutePa th() });
Process proc = shell.exec(new String[] {"bash", "-c", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePa
ASKER
CEHJ,
It still says: ERROR:mysqldump: Couldn't find table: ">"
objects, let me try your solution.
It still says: ERROR:mysqldump: Couldn't find table: ">"
objects, let me try your solution.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
objects,
I tried:
Process proc = shell.exec(new String[] {"bash", "-c", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePa th() });
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.crea te(Native Method)
at java.lang.ProcessImpl.<ini t>(Process Impl.java: 81)
at java.lang.ProcessImpl.star t(ProcessI mpl.java:3 0)
at java.lang.ProcessBuilder.s tart(Proce ssBuilder. java:451)
at java.lang.Runtime.exec(Run time.java: 591)
at java.lang.Runtime.exec(Run time.java: 464)
at testing.main(testing.java: 52)
I tried:
Process proc = shell.exec(new String[] {"bash", "-c", "mysqldump", "-uusername", "-pmypassword", "startdb", "markets", ">", new File(".", "mkts.dump").getAbsolutePa
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.crea
at java.lang.ProcessImpl.<ini
at java.lang.ProcessImpl.star
at java.lang.ProcessBuilder.s
at java.lang.Runtime.exec(Run
at java.lang.Runtime.exec(Run
at testing.main(testing.java:
my preference would be the second suggestion
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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
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
ASKER
Thanks for the solution! :)
Open in new window