java - 当我通过网络调用 exec 并持续一段时间后,它失败了

标签 java batch-file process mysql

我有一个用 java 和 MySQL 开发的应用程序。我通过 LAN 使用它,并在装有 Windows 7 professional x64 的 PC 上托管该应用程序。此电脑用作服务器,不用于任何其他目的。我已公开共享应用程序类文件所在的文件夹,其他 PC 可以使用以下批处理脚本连接到该应用程序:

net use v: \\192.168.0.44\MyAppName
v:
start "" /b javaw -cp .;v:\bin;v:\lib\swt.jar;v:\lib\mysql-connector-java-5.0.8-bin.jar;v:\lib\jdatepicker-1.3.2.jar;v:\lib\commons-io-2.4.jar;v:\lib\jfreechart-1.0.17.jar;v:\lib\jcommon-1.0.21.jar;v:\lib\forms-1.3.0.jar;v:\lib\activation-1.1.jar;v:\lib\commons-email-1.3.2.jar;v:\lib\mail.jar main.Main

所有 PC 均使用 JAVA 7,并运行 Windows 7、8 和 Mac(均为 64 位)。网络电缆是巨型的。

该应用程序的所有部分都工作正常,只有备份会出现问题。我为此使用了以下 java 代码:

public static boolean getDatabaseBackUp() {
    boolean isSucess = false;
    File dbBackUpFile = new File("db.sql");

    File mysqlDir = new File("\\\\192.168.0.44\\xampp\\mysql");
    File mysqlBinDir = new File(mysqlDir, "bin");
    File mysqlumpFile = new File(mysqlBinDir, "mysqldump.exe");
    String dbUser = "myUserName";
    String dbPass = "myPassword";
    String dbName = "myDbName";
    String executeCmd = "";
    executeCmd = mysqlumpFile + " " + dbName + " -u " + dbUser
            + " --password=" + dbPass + " " + " -r \""
            + dbBackUpFile.getAbsolutePath() + "\"";
    System.out.println(executeCmd);
    try {
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("Process is completed");
            isSucess = true;
        } else {
            System.out.println("Process is NOT completed");
            isSucess = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        isSucess = false;
    }
    return isSucess;
} // getDatabaseBackUp ()

上面的代码过去可以在网络上的所有 PC 上正常工作,包括服务器,当数据库很小时,我的意思是大约 3 MB,但现在它已增长到 335 MB,并且备份仅当您在服务器 PC 上打开应用程序时才起作用,当通过网络从任何其他 PC 打开该应用程序时网络,getDatabaseBackUp() 返回 false

如果我使用 -debug -verbose 运行它,输出为:

\\192.168.0.44\xampp\mysql\bin\mysqldump.exe myApplication -u myUserName --password=myPassword  -r "\\192.168.0.44\myApplication\assets\db_backups\2014_06_10_16_28_13.sql"
[Loaded java.lang.ProcessBuilder from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.Process from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.ProcessImpl from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.ProcessImpl$2 from C:\Program Files\Java\jre7\lib\rt.jar]
Process is NOT completed    

如果我在服务器上运行mysqldump ... --verobse ...,我得到:

C:\Users\myPcUserName>\\192.168.0.44\xampp\mysql\bin\mysqldump.exe myApplicationName -u myUserName --password=myPassword  --verbose  -r "\\192.168.0.44\myApplicationName\assets\db_backups\2014_06_11_09_59_56.sql

-- Connecting to localhost...
-- Retrieving table structure for table batch_fault_count...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table batch_insert_time_check...
-- It's a view, create dummy table for view
-- Retrieving table structure for table batch_status_logs...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table batch_types...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table batch_username_for_pq_check...
-- It's a view, create dummy table for view
-- Retrieving table structure for table batches...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table batches_price_list...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table dblock...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table digitally_verified_file_count...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table dvds...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table dvds_batches...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table expense_types...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table expenses...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table files...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table keys_values...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table mailing_list...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table qc_dblock...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table scanners_dirs...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table settings...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table user_check_ins...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table user_groups...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table users...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table users_salary...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving view structure for table batch_fault_count...
-- It's base table, skipped
-- Retrieving view structure for table batch_insert_time_check...
-- Retrieving view structure for table batch_status_logs...
-- It's base table, skipped
-- Retrieving view structure for table batch_types...
-- It's base table, skipped
-- Retrieving view structure for table batch_username_for_pq_check...
-- Retrieving view structure for table batches...
-- It's base table, skipped
-- Retrieving view structure for table batches_price_list...
-- It's base table, skipped
-- Retrieving view structure for table dblock...
-- It's base table, skipped
-- Retrieving view structure for table digitally_verified_file_count...
-- It's base table, skipped
-- Retrieving view structure for table dvds...
-- It's base table, skipped
-- Retrieving view structure for table dvds_batches...
-- It's base table, skipped
-- Retrieving view structure for table expense_types...
-- It's base table, skipped
-- Retrieving view structure for table expenses...
-- It's base table, skipped
-- Retrieving view structure for table files...
-- It's base table, skipped
-- Retrieving view structure for table keys_values...
-- It's base table, skipped
-- Retrieving view structure for table mailing_list...
-- It's base table, skipped
-- Retrieving view structure for table qc_dblock...
-- It's base table, skipped
-- Retrieving view structure for table scanners_dirs...
-- It's base table, skipped
-- Retrieving view structure for table settings...
-- It's base table, skipped
-- Retrieving view structure for table user_check_ins...
-- It's base table, skipped
-- Retrieving view structure for table user_groups...
-- It's base table, skipped
-- Retrieving view structure for table users...
-- It's base table, skipped
-- Retrieving view structure for table users_salary...
-- It's base table, skipped
-- Disconnecting from localhost...

C:\Users\myPcUserName>

但是,如果我在其他电脑上运行mysqldump ... --verobse ...,我会得到:

C:\Users\myPcUserName>\\192.168.0.44\xampp\mysql\bin\mysqldump.exe myApplicationName -u myUserName --password=myPassword  --verbose  -r "\\192.168.0.44\myApplicationName\assets\db_backups\2014_06_11_09_59_56.sql
-- Connecting to localhost...
mysqldump: Got error: 1049: Unknown database 'myApplicationName' when selecting the database

谁能告诉我问题是什么以及我如何解决它?

最佳答案

问题出在主机上。我没有在用于生成cmd命令的java代码中指定主机。我需要在 cmd 命令中使用 -h 192.168.0.44,因此 java 代码片段应如下所示:

File dbBackUpFile = new File("db.sql");

File mysqlDir = new File("\\\\192.168.0.44\\xampp\\mysql");
File mysqlBinDir = new File(mysqlDir, "bin");
File mysqlumpFile = new File(mysqlBinDir, "mysqldump.exe");
String dbUser = "myUserName";
String dbPass = "myPassword";
String dbName = "myDbName";
String dbIp = "192.168.0.44";
    String executeCmd = "";
    executeCmd = mysqlumpFile + " " + dbName + " -u " + dbUser + " -h " + dbIp
            + " --password=" + dbPass + " " + "  -r \""
            + dbBackUpFile.getAbsolutePath() + "\"";

关于java - 当我通过网络调用 exec 并持续一段时间后,它失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24138607/

相关文章:

java - 有效阻塞查询的建议

windows - 导出区域设置供以后导入

Java 进程 getInputStream 与 getOutputStream

multithreading - 线程内存布局

PHP - 子进程未正确退出

Java - ArrayList - 从数组的数组中获取值

java - NoViableAltException : Unexpected token in @NamedQuery

java - 获取文件过早结束异常

windows - 当批处理系列中没有负面导出时,TFS 构建流程中的批处理如何以 -1 退出

windows - 调用其他批处理文件后批处理中断