How to Run External Program, Command Prompt, and Batch files from Eclipse Console ? Example Tips

Hello guys, have you ever thought about how much time a Java developer spends on Eclipse, IntelliJ IDEA, or his favorite IDE? I guess most of his time, isn't it would be great to do as much as work possible from Eclipse itself, without switching to another application. Since switching between applications or programs requires some time, which is generally gone waste, I like to use Eclipse as much as possible, starting from coding in Java, executing Java programs to exploring XML files, and now even running the batch command from Eclipse. 

Most of you know about Eclipse's external tool configuration feature, which allows you to run any external program like a batch file from a local drive, but it can also be used to run command prompt from the Eclipse console itself.

In this tutorial, we will learn how to run Windows commands from the Eclipse console, by hooking cmd.exe into Eclipse. This is a really good tip to save time and improve your speed and productivity while working for Java applications on Eclipse and Windows.

I really wish that I know about this Eclipse tip early in my career. Btw, if you are new to Eclipse you should spend some time learning the IDE you are going to use every day. If you need a recommendation, I suggest you check out these Best Eclipse IDE Couress. It's good and also very affordable, you can buy in just $10 on Udemy sales. A couple of them are also free. 




How to Run Command Prompt files from Eclipse External Tool Configuration

Here are the steps to hook command prompt to Eclipse IDE:

1. Open Eclipse External Tool Configuration as shown below:

How to run command prompt, batch files from Eclipse - Tips



2.  Create a New External Configuration

Give a name to your External Tool Configuration Setting, I usually give the name of bat file or name of the exe itself like in this case, we will call it cmd

How to Create a New External Configuration



3.  Find the batch file or external program

You can browse executable I mean,  cmd.exe from the File System. You can find it in Windows/System32 folder as shown below. Once you find it, just load that and Click Apply, and you are done.

How to Find the batch file or external program in Eclipse


4.  Run

Now, you can click run and it will open the command prompt as shown below:

How to run the batch file or external program from Eclipse



Now, whenever you will run this external tool configuration by accessing the toolbar, you will see that command prompt open on Eclipse console. You can now execute any DOS or windows command, which is available in PATH.

Here is a snapshot of running DOS commands from Eclipse Console itself.

How to run DOS commands from Eclipse


I wouldn't say this is a very neat trick, but definitely very useful, if you happened to go back and forth to command prompt and Eclipse. This will not only save your time but also you can use Eclipse console settings to control it.

Related Java and Eclipse tutorials you may like to explore
If you like this tutorial and wants to learn more productivity tips, tricks, and new features of Eclipse IDE, then you can also see the following guides and tutorials:
  • The Java Developer RoadMap (guide)
  • How to attach the source code of a JAR file in Eclipse? (steps)
  • How to debug a Java Program in Eclipse IDE? (guide)
  • How to do the static import in Java Eclipse? (solution)
  • How do you create JUnit tests in Eclipse IDE? (guide)
  • Eclipse shortcut for System.out.println() statements (shortcut)
  • How to fix an Unsupported major.minor version 51.0 error in Eclipse? (solution)
  • How to fix must override superclass method error in Eclipse? (solution)
  • How to decompile class files in Eclipse? (solution)
  • How to escape String literal in a Java file when posted on Eclipse? (solution)
  • How to compare two files in Eclipse IDE? (guide)
  • How to increase the console buffer size in Eclipse? (solution)
  • How to make an executable JAR file in Eclipse? (steps)
  • How to solve Eclipse No Java Virtual Machine found the error? (solution)
  • How to fix the JAR dependency not working in Eclipse Maven Plugin? (answer)
  • How to fix Eclipse Not able to connect Internet or Market Place error? (solution)
  • How to set java.library.path in Eclipse IDE? (steps)
  • Step by Step Guide to Remote Debug a Java Application in Linux (guide)
  • How to fix Eclipse Access Restriction error (solution)

Thanks for reading this article so far. If you like this Eclipse tutorial and example then please share this article with your friends and colleagues. If you have any questions or feedback, please drop a note.

P.S. - If you are looking for some free online courses to learn Eclipse better and improve your productivity then I suggest you check out this list of 6 Free Courses to Learn Eclipse and JUnit which not only teach you Eclipse better but also introduce you to JUnit, one of the most popular Java testing library.

5 comments:

  1. I am also working on this task and i need your help .
    I have a batch file which i am opening using your given steps and its successfully getting open but after it open i want to execute some command like /term Remote and this is not happening . My question is why i am unable to execute any command after the batch file gets open using the said procedure ? Please help me in this sir.

    ReplyDelete
    Replies
    1. you batch file may not be created properly, can you run your batch file from command prompt and it does what you want? First try that and once worked, you can use these steps to run from Eclipse.

      Delete
    2. Yes, from Command prompt its working fyn.

      When i am directly running my batch file from command prompt C:\Windows\System32\cmd.exe, JCShell.bat file is working fine . and from this cmd prompt , all cmd like /term Remote , /card are working fine .

      But when i want to open JCShell.bat file from java code using the below code, only echo statement are getting printed on java console . i am not able to execute any command like /term Remote or /card from the java code . So , i need your help . I want to know that how to code for executing cmd like /term Remote or /Card from java code once the batch file is communicating via java code . Please see this below java code of mine:

      // code
      package com.jcshell;

      import java.io.InputStream;
      import java.io.OutputStream;
      import java.io.PrintWriter;

      public class JCShellMain {

      public static void main(String[] args) {
      String cmd1 = "\\t"+ "erm Remote"; // /term Remote
      try {
      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("C:\\Users\\Mohit\\Music\\offcardRow\\jcshell.bat");

      // write any other commands you want here
      stdin.print(cmd1);
      stdin.close();
      int returnCode = p.waitFor();
      System.out.println("Return code = " + returnCode);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      }

      class SyncPipe implements Runnable {
      public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
      }

      public void run() {
      try {
      String st = null;
      final byte[] buffer = new byte[1024];
      for (int length = 0; (length = istrm_.read(buffer)) != -1;) {
      st = new String(buffer, "UTF-8");
      ostrm_.write(buffer, 0, length);
      }
      System.out.println(st);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      private final OutputStream ostrm_;
      private final InputStream istrm_;
      }

      Please check these above code and please help me in executing different cmd on java console such as :
      /term Remote
      /Card

      Delete
    3. Yes, from Command prompt its working fyn.

      When i am directly running my batch file from command prompt C:\Windows\System32\cmd.exe, JCShell.bat file is working fine . and from this cmd prompt , all cmd like /term Remote , /card are working fine .

      But when i want to open JCShell.bat file from java code using the below code, only echo statement are getting printed on java console . i am not able to execute any command like /term Remote or /card from the java code . So , i need your help . I want to know that how to code for executing cmd like /term Remote or /Card from java code once the batch file is communicating via java code . Please see this below java code of mine:

      // code
      package com.jcshell;

      import java.io.InputStream;
      import java.io.OutputStream;
      import java.io.PrintWriter;

      public class JCShellMain {

      public static void main(String[] args) {
      String cmd1 = "\\t"+ "erm Remote"; // /term Remote
      try {
      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("C:\\Users\\Mohit\\Music\\offcardRow\\jcshell.bat");

      // write any other commands you want here
      stdin.print(cmd1);
      stdin.close();
      int returnCode = p.waitFor();
      System.out.println("Return code = " + returnCode);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      }

      class SyncPipe implements Runnable {
      public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
      }

      public void run() {
      try {
      String st = null;
      final byte[] buffer = new byte[1024];
      for (int length = 0; (length = istrm_.read(buffer)) != -1;) {
      st = new String(buffer, "UTF-8");
      ostrm_.write(buffer, 0, length);
      }
      System.out.println(st);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      private final OutputStream ostrm_;
      private final InputStream istrm_;
      }

      Please check these above code and please help me in executing different cmd on java console such as :
      /term Remote
      /Card

      hoping for your reply

      Delete
    4. If you want to run native shell commands from Java code then you need to use Process class and its exec() method, check this article for an example of running shell command form Java code.

      Delete

Feel free to comment, ask questions if you have any doubt.