Skip to main content

Start a java program from another java program programmatically

That's the thing I had to perform today. Let's introduce the need : I developed last year a standalone java program that I can start from command line. Now, I want to start it from a web application. As the program is complex (some Spring Batch stuffs), I don't want to make an heavy integration. So, I decided to embed its jar in my webapp (WEB-INF/lib). All I had to do was to find an elegant way to start a dedicated java virtual machine from my webapp to run my program without consequence on my web application.

The most elegant way I found was to use ANT... programmatically. In the following example, I wrote two classes. The MainProgram represents my webapp and the ProgramToRun represents my Spring Batch application. To simplify the code,  I didn't create a specific classpath for the ANT task but keep in mind that it's possible.

Look at the code, isn't it easy and cool?

public class MainProgram {

public static void main(String[] args) {
Project project = new Project();
project.setBaseDir(new File(System.getProperty("user.dir")));
DefaultLogger logger = new DefaultLogger();
Throwable caught = null;
try {
Echo echo = new Echo();
echo.setMessage("Launching Some Class");

Java javaTask = new Java();
Variable aParam = new Variable();
StringBuilder builder = new StringBuilder();
builder.append("Yeah!! I catched the param");
int ret = javaTask.executeJava();
System.out.println("java task return code: " + ret);

} catch (BuildException e) {
caught = e;

public class ProgramToRun {

public static void main(String[] args) {
System.out.println("Well done!!!");


Did you see the tip? No? I passed a parameter to my external program with a system property. Let's have a look to the console output :

     [Echo] Launching Some Class
  [runjava] Well done!!!
  [runjava] Yeah!! I catched the param
java task return code: 0

Total time: 0 seconds

A really nice ANT use case I discovered !!!


simashree said…
Thanks for the great information in your blog Selenium Training in Chennai

Popular posts from this blog

DCcduino usb drivers (CH340 / CH341 chipset)

I've just received my first arduino platform. It's a DCcduino board (a clone of Arduino Uno). As I had some difficulties to have it recognised by my MacBook, I decided to share its drivers. This card has a CH340 USB-to-serial chip. You can find drivers for this chip on the web site of the chinese manufacturer, here :

Or download it directly from my Google Drive. The archive contains drivers for Mac, Linux and Windows platforms.

I hope this will help somebody.

UPDATE for Mac users with Yosemite :

Please, follow this extra instructions :
Install the CH340 driverRun the command in Terminal: sudo nvram boot-args="kext-dev-mode=1"Reboot

The great alternative to JRebel

I'm an old user and addict of JRebel. I started to use it on open source projects and in professional contexts. From the beginning, I've been convinced that the licensing mode was wrong because of its lifetime. Asking for license renewal each year is boring. So, I decided to look for FREE alternative solutions and finally I recently found one.

This solution is efficient for maven projects developed with Eclipse. It is base on :

Hotswap Agent project : Source Lookup plugin for Eclipse :
I tested it with Java 7 & Java 8. I work on web applications that run on Tomcat. I developed wih Spring (IoC), sometimes Hibernate and Vaadin

Hotswap Agent installation consists on the deployment of a patch for your JVM. You just have to download the corresponding patch here : Then, download the hotswap-agent.jar from here :…

How to secure REST services exposed with Jersey (JAX-RS) using Spring Security

This is the challenge I had to perform. I have to open services to business partners and I want to secure them. I use a Java based application with Jersey API (JAX-RS).

The first thing to consider is : what kind of solution offers the best compromise between security and the effort I'll have to provide to maintain this solution?
After some discussions with developers and experts, the conclusion is : expose your services over https and use Basic authentication (Digest authentication and certificate based authentication are too complex for partners)

So, how to implement that? I delegate "https" to my Apache http servers. But I still need to handle authentication (and authorization of course). After long hours on Google, I understood that it is possible to manage security with Jersey by many many ways. Here is a short list :

Delegate simple authentication and authorization to your container (Tomcat) or to your frontend (Apache)Delegate authentication to your container or fro…