Java App: Windows Command Line Example

สำหรับการ run command line ผ่านทาง Java นั่นสามารถใช้ method Runtime.getRuntime().exec() ได้ แต่ว่าจะ run ตรงๆ ไม่ได้ จะต้องส่ง C:\WINDOWS\system32\cmd.exe /y /c ไปก่อน ตัวอย่างข้างล่างเป็นการเขียน utility class เพื่อที่จะนำไปใช้ต่อไปได้

CommandUtility.java


package com.amadmonster.util.command;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

public class CommandUtility {
static Logger logger = Logger.getLogger(CommandUtility.class);
private int status; // return command status, 0 - normal exit

/**
* Run Window command on specific working directory.
* @param command Array of command string and option.
* @param workDir String of working directory.
* @throws CommandUtilityException
*/
public void runCommand(String[] command, String workDir) throws CommandUtilityException {
String[] cmd = { "C:\\WINDOWS\\system32\\cmd.exe", "/y", "/c" };
List cmdList = new ArrayList();
cmdList.addAll(Arrays.asList(cmd));
cmdList.addAll(Arrays.asList(command));

Process p;
try {
p = Runtime.getRuntime().exec(cmdList.toArray(new String[0]), null,
new File(workDir));

logCommandInputStream(p);
logCommandErrorStream(p);
setStatus(p.exitValue());

if (getStatus() != 0) {
throw new CommandUtilityException("Fail to run command.");
}

} catch (IOException e) {
// TODO Auto-generated catch block
throw new CommandUtilityException("Fail to run command.", e);
}

}

private void logCommandInputStream(Process p) throws IOException {
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
logger.info(line);
}

}

private void logCommandErrorStream(Process p) throws IOException, CommandUtilityException {
InputStream is = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
logger.error(line);
}

}

/**
* Run Window command on specific working directory.
* @param command Command string.
* @param workDir String of working directory.
* @throws CommandUtilityException
*/
public void runCommand(String command, String workDir) throws CommandUtilityException {
String[] cmd = { command };
runCommand(cmd, workDir);
}

/**
*
* @return int Status of running command, 0 - normal
*/
public int getStatus() {
return status;
}

/**
*
* @param status Exit command status
*/
private void setStatus(int status) {
this.status = status;
}

}


CommandUtilityException.java

package com.amadmonster.util.command;

public class CommandUtilityException extends Exception {

public CommandUtilityException(String message, Throwable cause) {
super(message, cause);
}

public CommandUtilityException(String message) {
super(message);
}

public CommandUtilityException(Throwable cause) {
super(cause);
}

}


Reference:
When Runtime.exec() won't

No comments:

Post a Comment