进程无法找到正确的文件位置

时间:2016-03-16 06:55:33

标签: java c++ mysql netbeans processbuilder

我创建了C ++程序,负责在MySQL中备份和恢复数据库。这些C ++程序都使用两个文件夹,即readerCodesavesreaderCode包含包含mysql命令的文本文件(或脚本)。 saves文件夹是备份存储的位置。我将程序和文件夹存储在C:\AwesomeDatabase\ext

当我通过cmd运行程序时,命令成功运行。它在保存文件夹中生成.sql文件。但是当我使用流程构建器运行我的Java程序时,java程序可以运行可执行程序(用于备份和恢复),但是C ++程序找不到要查找的任务的文件。提前谢谢!

来自 Process.java

的代码片段
private void menuItemBackupActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
    try {
        String cmd [] = {"\"C:\\AwesomeDatabase\\ext\\DBBackup.exe\"", "-d"};
        ProcessBuilder builder = new ProcessBuilder ("\"C:\\AwesomeDatabase\\ext\\DBBackup.exe\"", "-d");
        builder.redirectErrorStream (true);
        Process p = builder.start();
        java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    } catch (java.io.IOException ioe) {
        System.err.println ("Fatal Error! Back up file not found!\n" + ioe);
    }

}

是的,所以当点击menuItemBackup时,java会启动一个进程。所以这是我的 DBBackup.cpp

#include <iostream>
#include <cstdlib>
#include <cstring>

#include "backup_head.h"

using namespace std;

//void secureBackup (void);
void normalBackup (void);

int 
main (int argc, char *argv[]) 
{

    if (argc > 1) {
        if (strcmp (argv [1], "-s") == false) {
            //secureBackup();
        } else if (strcmp (argv [1], "-d") == false) {
            normalBackup();
        } else {
            cout << "Invalid Command Line Arguments!" << endl;
            Sleep (500);
        }
    } else {
        cout << "Invalid Process Spawn!" << endl;
        Sleep (500);
    }

    return (0);
}

void 
normalBackup (void) 
{

    /* Fetch Location Variables */
    Program_Path        progpath    =   "";
    Change_Directory    cd          =   "CD";

    /* Back Up Variables */
    Program_Name    progdump    =   "mysqldump";
    Database_Name   dbname      =   "sampledb";
    Directory_Name  dirName     =   "saves";
    Dump_Name       dumpName    =   "BackUpNow";
    Username        username    =   "root";
    Password        password    =   "root";

    /* Delete Old Back Up */
    Command         delCommand  =   "DEL";
    File_Name       file        =   "DBBackUp";
    File_Directory  fileDir     =   "saves";

    /* Commands */
    _getcwd (progpath, sizeof (progpath));
    _chdir (progpath);
    SetCurrentDirectory (progpath);
    Command redir = cd + " " + "\"" + string(progpath) + "\"";
    Command backup = progdump + " " + "-u " + username + " " + "-p" + password + " " + dbname + " > " + dirName + "/" + dumpName + ".sql";
    Command delOld = delCommand + " \"" + fileDir + "/" + file + ".sql\"";

    cout << "Back Up process is on-going..." << endl << "Please wait for a moment..." << endl;
    Sleep (500);
    system (redir.c_str());
    system (delOld.c_str());
    system (backup.c_str());
    cout << "Back Up Completed Successfully!";
    Sleep (500);

    return;
}

Program_Pathchar [99]的类型定义。剩下的“怪异”&#39;数据类型只是字符串(我使用typedef来识别应该在特定用途中使用哪个变量)。我的java程序将在NetBeans Projects文件夹上的Documents文件夹上运行。当我在NetBeans中运行我的项目时,这将是输出:

Back Up process is on-going... 
Please wait for a moment... 
The system cannot find the path specified. 
The system cannot find the path specified. 
Back Up Completed Successfully!

0 个答案:

没有答案