ShellExec

ShellExec is a JNI-based Java library (J2ME - CDC) that provides a set of API to execute shell commands from a Java application that runs on Windows Mobile Pocket PC (PPC).

Introduction

Most commercial JVM for Windows Mobile PocketPC does not implement the System.exec() method, because the Pocket PC exec() equivalent system call does not support stdin and stdout handles, and Java support cannot be accomplished (Java specifications require to handle input and output streams of the executed command).

ShellExec is a Java library that provides a set of API to perform the launch of an application or to open a file with an external program, but does not provide any method to get its input and/or output stream. So, if you do not care about stdin or stdout from the program you are going to launch, you can use this library to bypass current PocketPC JVMs limitations.

Features

  • Simple API
  • Lightweight jar: 5 KB

Design

ShellExec uses JNI (Java Native Inteface) to access the operating system’s calls and provide a simple Java class (ShellExecute) to execute commands and launch applications from a Java environment.

Example

A simple testing application has been provided with the library. To run it you have just to launch the jar as application. To do it, I suggest you to create a link file (.lnk), like the following:

255#\Programmi\J9\PPRO10\bin\j9.exe -jcl:ppro10 -cp
\shellexec.jar com.pracucci.ppc.library.shellexec.Main

You have to change the path to the JVM and the classpath (set it to the absolute path to shellexec.jar).

This simple testing application, launch Windows Media Player.

API Example

How to open the browser:

ShellExecute.exec("\\Windows\\iexplore.exe","http://www.yahoo.com");

How to open Windows Media Player:

// Opens Windows Media Player without arguments
ShellExecute.exec("\\Windows\\wmplayer.exe", null);

Testing

Device Operating system Java Virtual Machine Test result
HP iPAQ hx2100 Microsoft Windows Mobile 5.0 Insignia Jeode Passed
HP iPAQ hx2490 Microsoft Windows Mobile 5.0 IBM J9 VM v.2.0 Passed
HP iPAQ hx2490 Microsoft Windows Mobile 5.0 IBM J9 VM v.2.2 Passed
HP iPAQ hx3800 Microsoft Windows Pocket PC 2002 Insignia Jeode Passed
HP iPAQ h5100 Microsoft Windows CE IBM J9 VM Passed
HP iPAQ h5500 Microsoft Windows CE IBM J9 VM Passed
HP iPAQ h5550 Microsoft Pocket PC 2003 IBM J9 VM v.5.7.2 Passed
HP iPAQ h6300 Microsoft Pocket PC 2003 IBM J9 VM v.5.7.2 Passed
Qtek 9100 Microsoft Windows Mobile 5.0 unknown Passed
Unknown Microsoft Windows CE 3.0 Insigna Jeode Passed
Motorola MC70 Windows Mobile 5.0 IBM J9 V v. 2.3 Passed
Dell Axim X3 Unknown IBM J9 Passed

Please, let me know if you experience any problem or if you test this library on another device / operating system / java virtual machine!

F.A.Q.

Is it possible to run ShellExec in a CLDC environment?

No. Unfortunatly, CLDC 1.0 / 1.1 specifications doesn’t allow to load native libraries, so it’s impossibile to use ShellExec in a CLDC environment.

How can I execute a command with parameters?

You can execute a command with parameters passing the command string as the first argument of ShellExecute.exec() method and the parameters list (a string with space-separated parameters) as the second argument of the method.

// Example
ShellExecute.exec("\\Windows\\wmplayer.exe", "\\myfolder\\mymusic.mp3");

Note: Microsoft Windows PocketPC doesn’t support relative paths. You have always to use absolute paths when you specify a file.

Author

Marco Pracucci
For more information: www.pracucci.com

License

ShellExec has been released under GNU GPL license.

Download

Please, visit the download area!

Change log

Version 1.1

  • Fixed a problem with command arguments

Version 1.2

  • Fixed a little bug about parameters handling
  • Compiled for Java 1.2