Every now and then even a windows user might feel the urge to run some batch processes.
In eclipse, batch files can be run by right clicking on the file | Open With | System Editor. This runs the batch file in a new console window.
However, the execution of the batch file starts working in the eclipse installation directory. That is, path that are relative to the folder of the batch file are now invalid.
The (seemingly) simplest solution might be to just use absolute file paths. However, this might turn out a bad idea. For example, when sharing the project – as others might store their workspace on a different position within their file systems.
So, basically, there are three ways to avoid this problem.
- Changing the batch file’s working directory.
- Using eclipse’s external tools
- Alternative: Using Ant’s exec task
- Integrate the shell into eclipse using Wicked Shell Plugin
- Changing the batch file’s working directory
Add the following line to the beginning of your batch file:
@cd /D %~dp0
This will change the working directory of to the path of the batch file. So every subsequent command in the batch file is relative to the path of the batch file itself.
Note: the /D-switch makes sure the file path is changed even though it resides on a different partition.
You can verify this behavior by executing the current batch file in eclipse:
:: Right now we're in the eclipse working directory @echo %cd% @cd /D %~dp0 :: We changed the directory to the script location @echo %cd% :: We now can use workspace relative paths! @pause
This will put out the path to the eclipses installation and then the path to the batch file.
- Using eclipse’s external tools
This approach offers the advantage that the batch file’s output is shown on the eclipse console view. However, a launch configuration is needed.
The following steps are necessary:
- In the menu, click Run | External Tools or on the drop down menu next to .
- Click External Tools configurations….
- Click Program , then .
- Choose Location and Working Directory within your workspace.
- (If you want to persist the launch configuration (e.g. for sharing among your team), go to the Common tab, press the radio button Shared file and enter a path, such as
/<project>/launchor whatever suits.)
- Finally, hit Run and check eclipse’s console view.
- Using Ant’s exec task
First of all we need an Ant build file:
- Right click on the project | New | File.
- In the upcoming dialog enter a file name ending in “.xml” and press Finish. Note that if you name the file “build.xml”, you will have less trouble running the file later.
- Within the editor paste an Ant-script, such as this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE project> <project name="runBatch" basedir="." default="runBatch"> <target name="runBatch"> <echo>Running batch file directly</echo> <exec executable="test.bat"> <!-- <arg value="-p someParam" /> --> </exec> <echo>Running batch file using cmd</echo> <exec executable="cmd"> <arg value="/c" /> <arg value="test.bat" /> <!-- <arg value="-p someParam" /> --> </exec> </target> </project>
As you can see, there are two ways of running a batch file from ant: Using the exec task directly or using the exec task to run cmd.exe and passing the batch file to this process using the /c-switch (as described in the exec task’s manual). Which one you use, is up to you. Either way, you can pass parameters to the batch file using the <arg> tag.
- Using Wicked Shell Plugin
If you’re using the command line a lot, the Wicked Shell might be interesting to you. It allows for using the command line from within eclipse. It also provides a comfortable way of running batch files. See this blog post for more info.
It you’re running eclipse on an OS other than Windows, Wicked Shell works as well. It always runs the system’s default shell, i.e. bash for Linux and OS X.
Now that we have a build file, what will we do with it? If you called it “build.xml” earlier, just right click the file | Run As (or hit Ctrl + F11 from the editor). Then click Ant Build an that’s it!
(If you ignored my warning and chose a different file name, you’ll need a run configuration to execute the Ant build. This is pretty much the same as described above for using eclipse’s external tools to execute the batch file directly. Except that you have to click on Ant Build instead of Program.
So, if you want to run batch files from eclipse, just choose one of the approaches described above. Personally, I prefer the first approach, because it comes with the least overhead. No additional launch configuration needed, just double click the batch file and that’s it.
For this post I used the eclipse icons published here. As mentioned there, they are part of the Eclipse Project and licensed under the EPL.
2 thoughts on “Running Windows batch files from eclipse using workspace-relative paths”
I’m using the WickedShell Eclipse plugin (see http://mcuoneclipse.wordpress.com/2012/03/31/5-best-eclipse-plugins-5/) which makes running batch files from Eclipse really easy.
Thanks for the hint – I’ll definitely have a look at the plugin