Java Create and Execute bat

Source: Internet
Author: User

/*

*/
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.