/*
*/
Package com.***app.mappcore.impl.util;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
/**
* Batch file Execution class .<br>
* @author Mapengfei <br>
* @version 1.0.0 October 26, 2015 <br>
* @see
* @since JDK 1.5.0
*/
public class Batexecutorutil {
/**
* Java process.
*/
public static process process = NULL;
/**
* The open Window mode executes the bat file.
* @param absolute path to the Batfilepath bat file
*/
public static void Execbat (final String Batfilepath) {
if (process! = null) {
Process.destroy ();
}
try {
Process = Runtime.getruntime (). EXEC (Command_k + batfilepath);
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* The open Window mode executes the bat file.
* @param absolute path to the Batfilepath bat file
*/
public static void Execbatnoconsole (final String Batfilepath) {
if (process! = null) {
Process.destroy ();
}
try {
Process = Runtime.getruntime (). EXEC (Command_c + batfilepath);
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Execute command
* @param command
*/
public static void ExecCommand (Final String command) {
if (process! = null) {
Process.destroy ();
}
try {
Process = Runtime.getruntime (). exec (command);
} catch (IOException e) {
E.printstacktrace ();
}
}
/* public static Boolean execcommand (Final String command) {
try {
Process = Runtime.getruntime (). exec (command);
Process.waitfor ();
Process.destroy ();
Process = NULL;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
return true;
}*/
/**
* The batch command to be executed is written to the target bat file
*
* @param command
* @param Batfilepath
*/
public static void Creatbat (Final string command, final string batfilepath) {
try {
File F = new file (Batfilepath);
Defining the Encoding
OutputStreamWriter write = new OutputStreamWriter (new FileOutputStream (f), "GBK");
BufferedWriter writer = new BufferedWriter (write);
Writer.write (command);
Writer.close ();
Write.close ();
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error writing file contents operation");
E.printstacktrace ();
}
}
/**
* Open the Command window.
*/
public static final String Command_k = "cmd/k start";
/**
* Do not open the command window.
*/
public static final String Command_c = "cmd/c";
/**
* @param args
*/
public static void Main (final string[] args) {
String Batfilepath = "F:\\mapp\\installtemple\\test\\test.bat";
String content = "D:\ncd d:\\program Files (x86) \\Inno Setup 5\n compil32/cc f:\\mapp\\installtemple\\test\\template.is S ";
Batexecutorutil.creatbat (content, Batfilepath);
Batexecutorutil.execbatnoconsole (Batfilepath);
}
}
Java Create and Execute bat