Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

java - ProcessBuilder cannot run bat file with spaces in path

I have the following code segment to run a bat file:

String workingDir = System.getProperty("user.dir");

ProcessBuilder pb = new ProcessBuilder("cmd", "/c", 
""" + workingDir + File.separator + "midl.bat"");

Process ddsBuildProc = pb.start();

ddsBuildProc.waitFor();

The workingDir includes spaces in the path. Eventhough I use quotes to enclose the workingDir+fileName string, the shell still splits the workingDir and doesn't run the bat file. If a try and copy-paste-execute the bat file path string in the Windows command window manually, it works as expected. What can be the problem here?

Also, please do not close this question as duplicate because I tried all the solutions in the other questions with no success.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • Don't quote commands in a command list, unless the command been executed expects it, this will just stuff things up
  • user.dir is your programs current executing context...so it actually makes no sense to include it, you could just use midl.bat by itself (assuming the command exists within the current execution context)

I wrote a really simple batch file...

@echo off
dir

Which I put in my "C:Program Files" directory, as I need a path with spaces and used....

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RunBatch {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder(
                        "cmd", "/c", "listme.bat"
        );
        pb.directory(new File("C:/Program Files"));
        pb.redirectError();
        try {
            Process process = pb.start();
            InputStreamConsumer.consume(process.getInputStream());
            System.out.println("Exited with " + process.waitFor());
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public static class InputStreamConsumer implements Runnable {

        private InputStream is;

        public InputStreamConsumer(InputStream is) {
            this.is = is;
        }

        public static void consume(InputStream inputStream) {
            new Thread(new InputStreamConsumer(inputStream)).start();
        }

        @Override
        public void run() {
            int in = -1;
            try {
                while ((in = is.read()) != -1) {
                    System.out.print((char) in);
                }
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

}

To run it without any issues...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...