-3
String run="c:\\Program Files\DOSBox-0.74\dosbox.exe dosbox -c mount c c:\games";

The word c c:\games gets removed.

Please advise how do I prevent this? Should I use a literal to insert the spaces in the command?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Mayukh Nair
  • 623
  • 1
  • 6
  • 26
  • Well the `String` is wrong to start with...all the `\` should be escaped. How are you executing the command? – MadProgrammer Aug 07 '14 at 03:57
  • @MadProgrammer `Process process = Runtime.getRuntime().exec(run);` – Mayukh Nair Aug 07 '14 at 07:09
  • Use `ProcessBuilder`, something like `ProcessBuilder pb = new ProcessBuilder("c:\\Program Files\\DOSBox-0.74\\dosbox.exe", "dosbox", "-c", "mount", "c", "c:\\games");` Spaces in commands cause no end of issues when executed in the manner you are trying, better to separate them into individual elements, this way you can control who the underlying process interrupts them – MadProgrammer Aug 07 '14 at 07:11
  • @MadProgrammer No, it ain't exactly wrong because the other commands are executed by DOSBox as desired. – Mayukh Nair Aug 07 '14 at 07:12
  • You have a choice then. You can continue doing what you're doing, which sometimes works or try something else...you choose... – MadProgrammer Aug 07 '14 at 07:13
  • No no, I ain't saying that I am right or your method is wrong. I am not in front of my workstation right now so I can't respond. And I do believe your commands are right. Your second comment came just a second before I pressed `Add Comment`. – Mayukh Nair Aug 07 '14 at 07:16
  • Yeah, love the placement of the enter key sometimes :P – MadProgrammer Aug 07 '14 at 07:17
  • Don't take it personally, just neck deep in documentation and going cross eyed ;) – MadProgrammer Aug 07 '14 at 07:18
  • Roger ;) Will try this command and get back to you. – Mayukh Nair Aug 07 '14 at 07:19
  • Actually the spaces are causing the problem. The space after Program is confusing the cmd to term the next token after space as parameter. So to solve that either use the ProcessBuilder and pass all the parameters as String array or if you still want to stick with the current code then write it as: String run="\"c:\\Program Files\DOSBox-0.74\dosbox.exe\" dosbox -c mount c c:\games";. So basically i am taking the argument where there are spaces inside the quotes. – Tech Enthusiast Aug 07 '14 at 07:28
  • No, it's not working. Neither does ProcessBuilder. – Mayukh Nair Aug 12 '14 at 03:15
  • @MadProgrammer Can you tell me the complete code you are using to make it work using ProcessBuilder? – Mayukh Nair Aug 12 '14 at 03:15
  • Does the command line you posted actually work (at the command line)? – MadProgrammer Aug 12 '14 at 05:05
  • Yeah. Only thing is, any word inserted after a space vanishes. Apart from that, everything else works. – Mayukh Nair Aug 14 '14 at 16:21

1 Answers1

0

A little bit of experimentation...

public class Test {

    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("C:\\Program Files (x86)\\DOSBox-0.74\\DOSBox.exe", "dosbox", "-c", "mount c c:\\games");
            pb.redirectError();
            Process p = pb.start();
            new Thread(new InputStreamConsumer(p.getInputStream())).start();
            System.out.println("Have exited with " + p.waitFor());
        } catch (IOException | InterruptedException exp) {
            exp.printStackTrace();
        }
    }

    public static class InputStreamConsumer implements Runnable {

        private InputStream is;

        public InputStreamConsumer(InputStream inputStream) {
            is = inputStream;
        }

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

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366