1. Invoke EXE file
1.1 Use the runtime class.
I've been tested.
try {
Runtime RT = Runtime.getruntime ();
rt.exec ("C:\\windows\\notepad.exe");
catch (Throwable t) {
System.out.print (t.getmessage ());
}
This allows you to test the
try {
Runtime rt=runtime.getruntime ();
String file = "E:/test/rem1.exe";
String file = "E:/test/rem1.bat";
Rt.exec ("cmd.exe/c start" + file);
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
The 1.2 bat file differs from a generic EXE file, but can convert a batch bat file to an EXE executable file and then invoke the executable file through Java. The conversion can be aided by Quick Batch file (De) Compiler, which compiles any bat, cmd batch script into an EXE file. Use: (Quick Batch File compiler can be downloaded online)
QUICKBFC filename. bat file name. exe (compile batch command to executable file)
QUICKBFD file name. exe file name. BAT (decompile executable to Batch command)
2. Direct call to BAT file
Batch files are different from ordinary executables, batches are just a series of command scripts, and his runs depend on interpreting his program, and you should create this interpreter.
Tested OK
try {
Runtime rt=runtime.getruntime ();
String file = "E:/test/rem1.exe";
String file = "E:/test/rem1.bat";
Rt.exec ("cmd.exe/c start" + file);
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
The Rem1.bat content in this article is:
@echo off
for%%i in (a b c) do (
: a bottle of beer
echo hero!
echo%%i
echo ^g^g
)
del E:\test\1.txt
pause
Echo Jixu