java方法将脚本命令写入bat文件去执行

请问如何在java方法里根据动态提供的参数,将动态参数加入到脚本命令,去cmd窗口自动执行呢?求回复

“Devil组”引证GPT后的撰写:
创建要执行的脚本命令字符串,该字符串包含需要执行的命令和动态提供的参数。例如,假设要执行的命令是“dir”,动态提供的参数是目录路径,那么可以创建如下字符串:

String command = "cmd /c dir " + path;

其中,“cmd /c”是在cmd窗口中执行命令的参数,“dir”是要执行的命令,path是动态提供的参数。

使用Java的Runtime类或ProcessBuilder类执行创建的命令字符串。例如,使用Runtime类可以这样实现:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);

使用ProcessBuilder类可以这样实现:


ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
Process process = builder.start();

在执行命令后,可以通过Process对象获取命令执行结果,例如:

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

上述代码会将命令执行结果输出到控制台。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 请看👉 :java如何向bat文件中传带有等号(=)等特殊字符的参数,并执行
  • 除此之外, 这篇博客: java实现服务器的在线升级更新中的 4.java调用bat并执行bat文件 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    java调用bat文件,及相关的服务器更新如下,这里更新的服务名为Global_info,注意自己服务所在的盘符路径

    package com.nlx.controller;
    
    import org.cola.model.Result;
    import org.cola.utils.ResultGenerator;
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
    
    import java.io.*;
    import java.util.Iterator;
    
    /**
     * @ClassName: OnlineUpdate
     * @Author:Cola.Niu
     * @date:2020/2/12 16:05
     * @Version:v1.0
     * @description:
     */
    @RestController
    @RequestMapping("/version")
    public class OnlineUpdate {
        @PostMapping("/update")
        public Result update(StandardMultipartHttpServletRequest request){
            //1.实现文件上传
                // 设置更新包安装路径
            String path = "D:\\service\\global_info";
                //开始上传
            try {
                Iterator<String> iterator = request.getFileNames();
                while (iterator.hasNext()) {
                    MultipartFile file = request.getFile(iterator.next());
                    FileCopyUtils.copy(file.getBytes(),new File(path+"//version.zip"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //2.执行解压bat操作,解压完后删除上传的文件
                //bat文件中的cmd命令
            String cmd = "cd /d %~dp0\n" +
                    "set current_path=%cd%\n" +
                    "7z.exe x version.zip -y\n"+
                    "del version.zip";
                //bat文件路径
            String batPath = path+"\\version.bat";
            FileWriter fw = null;
            try {
                // 生成bat文件
                fw = new FileWriter(batPath);
                fw.write(cmd);
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
                //执行
            executeBat(batPath);
            //3.执行更新bat
            executeBat(path+"\\update.bat");
            return ResultGenerator.returnSuccess();
        }
    
        public void executeBat(String batPath){
            try {
                // 运行bat文件
                Process process = Runtime.getRuntime().exec(batPath);
                /*InputStream in = process.getInputStream();
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();*/
                process.waitFor();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^