简体   繁体   中英

Java Runtime exec() with mysqldump

I'm a little curious about the behaviour of Runtime's exec() method when I run mysqldump. I'm running the following command:

mysqldump --user=root --hex-blob [database name] -r [path to sql file]

What I'm wondering is, where does Runtime search for the program mysqldump.exe?

I see that some people supply the whole file path to mysqldump.exe when executing it using Runtime. Why is this?

The reason why I'm curious is because I have two scenarios:

  1. On one windows machine, if I open run and type "cmd" it will open a command window with the default location C:/. Running the mysqldump command on this machine works.
  2. On another windows machine, if I open run and type "cmd" it will open a command window with the default location H:/. Running the mysqldump command on this machine fails. Java's Runtime cannot find the file mysqldump.exe.

Is it possible that the two windows machines have different default drives and if I don't supply the full path to mysqldump.exe, the system will look in the default driver?

Thanks in advance!

As mentioned in the documentation :

Starting an operating system process is highly system-dependent . Among the many things that can go wrong are:

  • The operating system program file was not found.
  • Access to the program file was denied
  • The working directory does not exist.

What I would suggest is starting with a ProcessBuilder , something like:

 ProcessBuilder pb = new ProcessBuilder("mysqldump ...");
 Map<String, String> env = pb.environment();
 env.put("PATH", env.get("PATH") + ";Path/to/mysqldump");
 try {
    Process process = pb.start();
    //some code
 } catch(IOException e){
 }

This way you ensure the environment variable is correctly set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM