1

I have to execute the following cmd commands of Windows in Java.

The cmd commands:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

//First i have to change my default directory//

C:\Users\shubh>D:

//Then move to a specific folder in the D drive.//

D:\>cd MapForceServer2017\bin\

//then execute the .mfx file from there.

D:\MapForceServer2017\bin>mapforceserver run C:\Users\shubh\Desktop\test1.mfx 

Result of the execution

Output files:
library: C:\Users\shubh\Documents\Altova\MapForce2017\MapForceExamples\Tutorial\library.xml
Execution successful.
TT.
  • 15,774
  • 6
  • 47
  • 88
shubham0001
  • 63
  • 1
  • 1
  • 7
  • 1
    Why don't you try a windows .bat script? Just a thought! – harshavmb Dec 03 '16 at 08:22
  • i wish to add the java code for above command in my servlet file where the user will be uploading the file and then it will be consumed by the mapforceserver run command. So the file name will be changing every-time the user uploads a new file. can we change the contents of the batch file dynamically during runtime? – shubham0001 Dec 03 '16 at 08:38
  • 1
    Yeah, you can write using java.io.File API, go through the below link. http://stackoverflow.com/questions/29820079/write-into-bat-file-with-java – harshavmb Dec 03 '16 at 08:45

2 Answers2

4

I suggest using https://commons.apache.org/proper/commons-exec/ for executing o/s command from within Java because it deals with various issues you may encounter later.

You can use:

CommandLine cmdLine = CommandLine.parse("cmd /c d: && cd MapForceServer2017\\bin\\ && mapforceserver run ...");
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
cherouvim
  • 31,725
  • 15
  • 104
  • 153
  • 2
    +1 for use of `&&` and using a third-party library to handle the subprocess. Incidentally you can replace `d: && cd MapForceServer...` with `cd /d D:\\MapForceServer...`: the `/d` switch allows you to change drive and directory at the same time. – Luke Woodward Dec 03 '16 at 12:00
  • Thank you cherouivm for your answer and Luke Woodward for your comment.Basically what i did is that used the combination of both the approaches that is creating a batch file(approach suggested by harshavmb ) that is having multiple commands and than using the cherouvim's approach of using commandline.parse( ) function for invoking that batch file with a string variable that sends the value of the variable(which is basically a filename uploaded by the user) to the batch file for further execution. – shubham0001 Dec 03 '16 at 16:34
  • @shubham0001 creating a batch file seems like an overkill in case the total amount of commands you need to execute aren't that many. You can concatenate them with `&&`. – cherouvim Dec 03 '16 at 17:25
0

I had somewhat same requirement some time back and at that time I got the following snippet from stack I think. Try this.

   String[] command =
    {
        "cmd",
    };
    Process p = Runtime.getRuntime().exec(command);
    new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
    new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
    PrintWriter stdin = new PrintWriter(p.getOutputStream());
    stdin.println("dir c:\\ /A /Q");
    // write any other commands you want here
    stdin.close();
    int returnCode = p.waitFor();
    System.out.println("Return code = " + returnCode);

SyncPipe Class:

class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
              ostrm_.write(buffer, 0, length);
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  private final OutputStream ostrm_;
  private final InputStream istrm_;
}