我有一个问题,我在我的项目中使用命令来 winrar。当我在只有 1 个类的新项目中尝试时,我没有问题,并且命令被执行。但是当我尝试复制到我的项目并从另一个类调用该类时,我使用日志记录来跟踪代码,并且代码被执行,但什么也没发生。为什么执行代码但我的程序没有任何反应?
这是我的 CMD 命令的简单项目。
public static void execute(String flagname, String password) {
String command = "cmd.exe "
+ "/C"
+ " cd C:\\Program Files\\WinRAR"
+" && rar a -n E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\RUN\\testing.rar "
+ "E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\"+ flagname + " -ptesting"
+ " && rar a -n E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\RUN\\LOCK\\testing.rar "
+ "E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\" + flagname + " -ptesting";
try {
Runtime.getRuntime().exec(command);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
这是程序被调用的时候:
public class ListenerSpoolPfile {
public void run() throws Exception {
boolean runForever = true;
String path = PropertiesUtil.getInstance().getPathProp()
.getProperty("WATCH_FOLDER");
int mask = JNotify.FILE_CREATED;
boolean watchSubtree = true;
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listen());
while (runForever) {
}
boolean res = JNotify.removeWatch(watchID);
if (!res) {
}
}
public static void main(String[] args) throws FileNotFoundException,
IOException, Exception {
org.apache.log4j.Logger log = LogFile
.getLogger(ListenerSpoolPfile.class);
log.info("Starting listener...");
new ListenerSpoolPfile().run();
}}
对于听众:
class Listen implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
}
public void fileModified(int wd, String rootPath, String name) {
}
public void fileDeleted(int wd, String rootPath, String name) {
}
public void fileCreated(int wd, String rootPath, String name) {
new ListenerThread(name).start();
}}
我的线程:
class ListenerThread extends Thread {
static org.apache.log4j.Logger logger = LogFile
.getLogger(ListenerThread.class);
String flagname;
ListenerThread(String flagname) {
this.flagname = flagname;
}
public void run() {
String lockPath = PropertiesUtil.getInstance().getPathProp()
.getProperty("SHELL_CMD_LOCK");
String runPath = PropertiesUtil.getInstance().getPathProp()
.getProperty("SHELL_CMD_RUN");
int maxShellCommand = Integer.parseInt(PropertiesUtil.getInstance()
.getPathProp().getProperty("SHELL_CMD_MAX"));
int sleepTime = Integer.parseInt(PropertiesUtil.getInstance()
.getPathProp().getProperty("SLEEP"));
File lockFile = new File("");
File runFile = new File("");
boolean runForever = true;
if (getTotalFile(lockPath) >= maxShellCommand) {
while (runForever) {
try {
Thread.sleep(sleepTime);
if (getTotalFile(lockPath) < maxShellCommand) {
runForever = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
String tampung = null;
try {
logger.info("flag name : " + flagname);
tampung = IoFile.readFile(flagname);
logger.info("tampung value : " + tampung);
CommandCMD.execute(flagname, tampung);
lockFile = new File(PropertiesUtil.getInstance().getPathProp()
.getProperty("SHELL_CMD_LOCK")
+ flagname);
if (lockFile.exists()) {
lockFile.delete();
} else {
IoFile.writeFile(lockPath, tampung, flagname);
}
runFile = new File(PropertiesUtil.getInstance().getPathProp()
.getProperty("SHELL_CMD_RUN")
+ flagname);
if (runFile.exists()) {
runFile.delete();
} else {
logger.info("Nama File : " + flagname + " isinya adalah = "
+ tampung);
IoFile.writeFile(runPath, tampung, flagname);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
要获得总文件:
private int getTotalFile(String dirPath) {
int count = 0;
File f = new File(dirPath);
File[] files = f.listFiles();
if (files != null)
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
count++;
getTotalFile(file.getAbsolutePath());
} else {
count++;
}
}
return count;
}
我的项目中没有任何错误,但是当我提供日志以跟踪项目运行良好时,我的执行方法被执行。帮帮我,我几乎放弃了:(