Java programs call bat files and exe files

Source: Internet
Author: User
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



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.