java - the code isn't working.it run infinitely? -


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);     }          } 

the code isn't working. run infinitely? how can run mysqldump on java?

since path command want run contains spaces, need quotes around it:

string dumpcommand = "\"c:\\program files\\mysql\\mysql server 5.0\\bin\\mysqldump\""+ " -u " + user +" -p" + " "+ database +" > "+path;          

edit

a processbuilder make job easier:

// step 1: set 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 process process proc = pb.start(); 

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


Comments