Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2478
  • Last Modified:

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

0
rnicholus
Asked:
rnicholus
  • 6
  • 6
  • 5
2 Solutions
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now