Julien
Julien

Reputation: 415

How to run Windows command from Java

I would like execute the following command with Java:

"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root --password=xxxx --routines database > C:\Users\john\Desktop\backup.sql

The command works perfectly when I use the Windows cmd.exe but not with my Java application.

cmd = "\"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\\"mysqldump
-u root --password=xxxx --routines database > C:\\Users\\john\\Desktop\\backup.sql
Runtime runtime = Runtime.getRuntime(); 
process = runtime.exec(cmd);

I got the following error:

"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root --password=xxxx --routines database > C:\Users\john\Desktop\backup.sql java.io.IOException: Cannot run program ""C:\Program": CreateProcess error=5, Access is denied

Do you have any idea?

Thank you.

Upvotes: 0

Views: 1447

Answers (2)

Eagle
Eagle

Reputation: 163

Error 5 is either 1) cannot access the file/folder or 2) don't have permissions.

Looks like the space in your 'Program File' directory name may be causing trouble causing trouble. Check where it says "Cannot run program ""C:\Program". This is obviously not a reference to the command you are trying to run. What I would do is add MySQL to your path and then just call the mysqldump command without the path to it. If you don't want to do this, you can define a system variable and reference it with %MYSQL_HOME%\mysqldump.

Once you have the path resolved, if this still doesn't work, you'll want to look at the permissions for the command. You'll need to make sure that the process has permission to execute the command. I think this in local admin groups or something like that.

Dollars to donuts its your path tho.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76908

You're trying to use a redirect (>) which is a function of the shell.

You don't have a shell, you're exec'ing the mysqldump process directly.

You either need to read the output stream from the process object and write it to a file yourself, or exec the windows shell to execute mysqldump for you along with the redirect.

Upvotes: 3

Related Questions