1

So I am still learning programming, I am creating a simple application that can backup a database but the problem is when I click the button for backup, nothing happens, it does not even display the "can't create backup". I am using xampp, in case that is relevant. I have zero idea as to why is it is not working, and I am really curios what is the reason behind it, any help will be greatly appreciated.

...
String path = null;
String filename;

//choose where to backup

 private void jButtonLocationActionPerformed(java.awt.event.ActionEvent evt)   {                                         
    JFileChooser fc = new JFileChooser();
    fc.showOpenDialog(this);
    String date = new SimpleDateFormat("MM-dd-yyy").format(new Date());

    try {
        File f = fc.getSelectedFile();
        path = f.getAbsolutePath();
        path = path.replace('\\', '/');
        path = path+"_"+date+".sql";
        jTextField1.setText(path);

    } catch (Exception e) {
        e.printStackTrace();
    }
} 

//backup
private void jButtonBackUpActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Process p = null;


    try{
        Runtime runtime = Runtime.getRuntime();

        p=runtime.exec("C:/xampp/mysq/bin/mysqldump -u root --add-drop-database -B capstone -r "+path);

        int processComplete = p.waitFor();
        if (processComplete==0) {
            jLabel1.setText("Backup Created Success!");
        } else {
            jLabel1.setText("Can't create backup.");
        }
    } catch (Exception e) {

    }


} 
G. Tepait
  • 11
  • 3

3 Answers3

0

You use a try-catch block in the jButtonBackUpActionPerformed, but the catch statement is empty. Therefore, if an exception is raised for whatever reason, no file would be written and you would get no output. You can try to use e.printStackTrace() like in the catch statement of the other button for debugging.

Stan
  • 21
  • 4
0

I found the underlying problem, thanks to stan. It was a typo problem, instead of "mysql", I have put "mysq" thank you guys!

  java.io.IOException: Cannot run program "C:/xampp/mysq/bin/mysqldump.exe": CreateProcess error=2, The system cannot find the file specified
G. Tepait
  • 11
  • 3
0

this will run any shell script on Linux server. Test it on windows ... shoud work too

 public static int executeExternalScript(String path) throws InterruptedException, IOException {

  ProcessBuilder procBuilder = new ProcessBuilder(path); 
  procBuilder.redirectErrorStream(true);
  Process process = procBuilder.start();
  BufferedReader brStdout = new BufferedReader(new InputStreamReader(process.getInputStream()));

  String line = null;
  while((line = brStdout.readLine()) != null) {   logger.info(line);   }
      int exitVal = process.waitFor();
      brStdout.close();
      return exitVal;}