0

I have a problem, i using a command to winrar in my project. When i try in new project with only 1 class, i have no problem, and the command is executed. But when i try to copy to my project and i call the class from the other class, i use logging for tracing the code, and the code is executed, but nothing happened. Why the code is executed but nothing happened to my program?

this is my simple project for CMD command.

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();
    }
}

and this is when the program is called:

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();
}}

For the listener:

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();
}}

And my thread:

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());

    }

}

To get Total File:

    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;
}

I dont get any error in my project, but my execute method is executed when i give a log for tracing the project doing well. Help me, i almost give up :(

user3505775
  • 339
  • 2
  • 6
  • 20

1 Answers1

0

when you run a process, you need to read it's output and stderr. if it outputs and you are not reading it it will eventually hang when the output buffer fills up.

see this, in particular the example with reading in a different thread.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87