【问题标题】:Java: Redirecting output of .bat file in other text file using exec() method?Java:使用 exec() 方法在其他文本文件中重定向 .bat 文件的输出?
【发布时间】:2014-12-18 13:37:18
【问题描述】:

Java 对我来说是新的。 我正在使用Runtime.getRuntime.exec(filename.bat) 执行一个批处理文件,这个批处理文件执行一个命令ant encrypt.password -Dvalue=somevalue>log.txt 并将其输出重定向到一个log.txt 文件。 我面临的问题是如果我手动运行批处理文件可以正常工作但是当程序执行它时,它只会创建空白的'log.txt'

mybat.bat批处理文件内容如下:

cd/
c:
cd c:/ant_builds/thinclient
ant encrypt.password -Dvalue=someValue >C:/log.txt

Java代码如下:

Process p=Runtime.getRuntime.exec("C:\mybat.bat");
p.waitFor();

似乎在创建日志文件后,同时命令正在执行,控制从进程中出来。

我在这里阅读了近 50 个主题,但没有得到解决方案。请帮帮我。

【问题讨论】:

    标签: java batch-file cmd command


    【解决方案1】:

    使用ProcessBuilder 创建您的进程并调用redirectOutput(File) 将输出重定向并附加到文件中。

    试试这个代码:

    public class Test {
        ProcessBuilder builder;
        Path log;
    
        public Test() {
            try
            {
                log = Paths.get("C:\\log.txt");
                if (!Files.exists(log))
                {
                    Files.createFile(log);
                }
                builder = new ProcessBuilder("ant", "encrypt.password", "-Dvalue=someValue");
                builder.directory(Paths.get("C:\\ant_builds\\thinclient").toFile());
                builder.redirectOutput(ProcessBuilder.Redirect.appendTo(log.toFile()));
                builder.start();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            new Test();
        }
    }
    

    对于 jdk 1.6 或更低版本,使用以下代码:

    public class Test {
        ProcessBuilder builder;
        Path log;
        Process process;
        BufferedReader br;
        PrintWriter pw;
        Charset charset = Charset.forName("UTF-8");
    
        public Test() {
            try {
                log = new File("C:\\log.txt");
                if (!log.exists()) {
                    log.createNewFile();
                }
                builder = new ProcessBuilder("ant", "encrypt.password","-Dvalue=someValue");
                builder.directory(new File("C:\\ant_builds\\thinclient"));
                builder.redirectErrorStream(true);
                process = builder.start();
                br = new BufferedReader(new InputStreamReader(process.getInputStream(),charset));
                pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(log, true), charset));
    
                (new Thread() {
                    public void run() {
                        try {
                            while (process.isAlive()) {
                                String s = null;
                                while ((s = br.readLine()) != null) {
                                    pw.print(s);
                                    pw.flush();
                                }
                            }
                            br.close();
                            pw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            new Test();
        }
    }   
    

    我不确定 ProcessBuilder 参数的顺序和列表,因此请尝试使用它们以使您的代码正常工作。

    【讨论】:

    • 感谢您的关注!这是我没有尝试过的东西,但是我正在使用 java 1.6,所以路径类不可用请指导我如何将 FilesPath 类替换为 Java 1.6 compatible File Class 谢谢!
    • @Arun 查看我上面更新的代码,并测试提供给 ProcessBuilder 的参数是否正确。
    • 感谢您的努力!!我尝试了上面的代码,但是 ProcessBuilder.Redirect.appendTo(log) 在 jdk 1.6 中不可用,所以我想我必须使用其他东西。 :(
    【解决方案2】:

    您还可以从公共文件中读取命令并将输出和错误重定向到单独的文件。 Redirect.appendTo 是为了避免进程覆盖现有的日志。

    试试这个代码:

    try {
    File commands = new File("D:/Sample/Commands.txt"); 
    File output = new File("D:/Sample/Output.txt"); 
    File errors = new File("D:/Sample/ErrorsLog.txt"); 
    
    ProcessBuilder pb = new ProcessBuilder("cmd");
    
    System.out.println(pb.redirectInput());
    System.out.println(pb.redirectOutput());
    System.out.println(pb.redirectError()); 
    
    pb.redirectInput(commands);
    pb.redirectError(Redirect.appendTo(errors));
    pb.redirectOutput(Redirect.appendTo(output));
    
    pb.redirectInput();
    pb.redirectOutput();
    pb.redirectError(); 
    
    pb.start();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    

    【讨论】:

    • 嗨 Saraboji 方法 pb.redirectInput(),pb.redirectOutput(),pb.redirectError() and Redirect.appendTo() 在 jdk 1.6 中不可用。指导我将此代码转换为与 jdk 1.6 兼容的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2021-12-18
    • 2013-06-08
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多