How to use a bat file to call an exe program in the same folder as it

First option:

If the bat file is in the same folder as an exe program. Then just write the name of the program file.
Two ways of writing. For example, it is the test.exe program,
one type of
test.exe
and two types
of start test.exe

. The first batch process will wait for the test.exe program to finish executing before running the following statement.
The second type will not wait.

This is useful for writing absolute paths

Alternatively:

You can use the relative path in the BAT file to call an EXE program in the same folder as it. The following is a simple example BAT file, assuming your BAT file and EXE file are in the same folder:

@echo off
REM calls example.exe in the same folder
start "" "%~dp0\example.exe"
 

In this example, %~dp0represents the folder path where the BAT file is located. start ""is a command that starts a new window to run the specified command or program. %~dp0\example.exeIndicates the path of the program in the same folder example.exe.

Make sure you example.exereplace with the name of the actual EXE file. This example will be launched in the same folder example.exe.

It should be noted that BAT file execution of EXE files also requires some security and user privacy considerations. Users may see a window flash indicating that the BAT file is launching the EXE file. If you need a smoother experience, you may want to consider other methods, such as packaging the BAT file and EXE file together into a single executable file.

This startup relative path is easier to use

How to open a .bat file with java:

To execute a local BAT file using Java, you can use the Java ProcessBuilderclass. The following is a sample code that shows how to run a BAT file using Java:

import java.io.IOException;

public class RunBatFile {
    public static void main(String[] args) {
        try {
            // 指定要运行的BAT文件路径
            String batFilePath = "C:\\path\\to\\your.bat";

            // 创建一个ProcessBuilder对象
            ProcessBuilder processBuilder = new ProcessBuilder(batFilePath);

            // 启动进程并执行BAT文件
            Process process = processBuilder.start();

            // 等待BAT文件执行完成
            int exitCode = process.waitFor();

            // 输出执行结果
            if (exitCode == 0) {
                System.out.println("BAT文件执行成功。");
            } else {
                System.out.println("BAT文件执行失败。");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Guess you like

Origin blog.csdn.net/s_sos0/article/details/132593313