我创建了C ++程序,负责在MySQL中备份和恢复数据库。这些C ++程序都使用两个文件夹,即readerCode
和saves
。 readerCode
包含包含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_Path
是char [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!