Start a native application from your Java code
April 4th, 2008 by Christos Fragoulides

Starting a native application from your Java code is as simple as the following command:
Runtime.getRuntime().exec("notepad.exe");

Of course, this adds platform dependency to your code, and if you want your application to be portable it’s a good practice to avoid using the exec() method. But if it is necessary to initiate a native application for any reason, the way is easy and pretty straight-forward. The fact is that there’s always a case where executing a command of the operating system is the only way to achieve specific functionality.

In this case, independence can be approached if the command to be executed exist(with common or different syntax) in various operating systems. The first step is to find out the syntax of the command for every OS. Then within the code, you can use System.getProperty(“os.name”) to determine the OS that is being to host your Java application and finally use this information to execute the command using the corresponding syntax.

There is one more useful feature of the exec() method: it’s return value is an instance of the Process class, which can be used to get access to the actual process of the application initiated. Check out the javadoc of the Process class to find out more.

Posted in General

Post a Comment Below »
Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.