-3
String dumpCommand = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump" + " -u " + user + " -p" + " " + database + " > " + path;         
Runtime rt = Runtime.getRuntime();
File test = new File(path);
PrintStream ps;
try{
    Process child = rt.exec(dumpCommand);
    System.out.println("Child" + child);
    ps = new PrintStream(test);
    InputStream in = child.getInputStream();
    int ch;

    while ((ch = in.read()) != -1) {
        ps.write(ch);
        System.out.write(ch);
    }         
}

コードが機能していません。それは無限に実行されますか?Java で mysqldump を実行するにはどうすればよいですか?

4

2 に答える 2

2

Since the path to the command you want to run contains spaces, you need quotes around it:

String dumpCommand = "\"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump\""+ " -u " + user +" -p" + " "+ database +" > "+path;         

EDIT

A ProcessBuilder will make this job easier:

// Step 1: set up the command line
ProcessBuilder pb = new ProcessBuilder(
    "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump",
    "-u",
    user,
    "-p"
    database);

// Step 2: redirect output
File test = new File(path);
pb.redirectOutput(test);

// Step 3: start the process
Process proc = pb.start();

The ProcessBuilder class has many other features (consult the documentation), including the ability to define the environment variables for the subprocess, setting the current directory, and redirect error output. The latter might be extremely useful in diagnosing problems if things go wrong.

于 2013-07-15T05:27:59.207 に答える
0

または、 String.trim() メソッドを使用してスペースを除外できます。

于 2013-07-15T05:52:05.860 に答える