2

I am creating a little program to take away some of my work by using a GUI. What I have right now is a little program with a button "Start" which starts a CMD-based application. What I need to do is enter a certain command in that CMD window. I want to add a button which fills out this command for me.

Is this even possible?

P.S.: CMD is Windows's Command Prompt.

Thank you all.

1
  • I don't believe you can control a CMD window from a Java application. However you can create a window which looks like a CMD window which works any way you want. Commented Jun 6, 2011 at 9:21

2 Answers 2

1

This works if it is an external command:

String command = "cmd";   //Replace with your command
Runtime.getRuntime().exec(command);

If it is an internal command then that will not work, but there is a workaround: create a batch file with the commands in it, and put a String with the path to the batch file as an argument.

Runtime.getRuntime().exec("Temp.bat");

Or you can write the batch file programmatically using this method:

private void batch(String commands){
  try{
    String filename = "Temp.bat";
    File f = new File(filename);
    PrintWriter writer = new PrintWriter(f);
    writer.print(commands);
    writer.close();
    Runtime.getRuntime().exec(filename);

    long x = getTime + 3000;
    do{
      //Wait
    }while(getTime < x);

    f.delete();
  }catch(Exception ex){
    ex.printStackTrace();
  }
}

private long getTime(){
  SimpleDateFormat datef = new SimpleDateFormat("yyyyDDDHHmmssSSS");
  Date date = new Date();
  return Long.parseLong("" + datef.format(date));
}
Sign up to request clarification or add additional context in comments.

Comments

0

It depends on how your CMD-based application receives its input.

If it receives input from the input stream of the CMD window, this article explains how to get a reference to the input stream and send messages to it: Java exec - execute system processes with Java ProcessBuilder and Process.

If your CMD window is running a command that doesn't take input, and you want to execute another command, then I think you would have to run your second command in a separate CMD process.

4 Comments

The CMD get its input from a C++ application. The application is closed-source so I cannot change anything in there.
Does the C++ application read its input from the CMD command line? If so, then the Java Process Builder method should work just the same.
The C++ (DLL file) opens a CMD window to give the output. So the DLL sends its output to the CMD window. I want to grab the content of that window. It would be easy if I could modify the DLL by saving the contents to a text file every X seconds and let Java read that text file. Hmmm
You can redirect the output from a program like this:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.