Accessing the Windows Shell from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access the Windows Shell from Java. J-Integra® for COM is a Java interoperability component that bridges Java and the Windows Shell. It provides bi-directional access of Java objects and COM components.

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about Microsoft Shell programming, please refer to the Microsoft Shell Reference. The quickest way to start programming Java-to-Shell is to find a VB example first, and then map the VB example to Java.

import com.linar.jintegra.AuthInfo;
import shell.*;

//
// This example minimises all the windows, and also lists all disk drives on the
// machine.
//
// In order to run this example, you will need to use the 'com2java' tool on the
// following file: \WINNT\system32\SHDOCVW.DLL.  Create a 'shell' subdirectory prior
// to running com2java, and specify 'shell' as the package name, and the shell
// subdirectory as the output directory.
//
// You will also need to configure DCOM access to the components on that DLL.  Use the SETDLLHOOST
// as shown in the 'Jintegra and Visual C++. In-process (DLL) COM component example' in the
// documentation to configure \WINNT\system32\SHDOCVW.DLL with the description
// "Shell Automation Service".

public class ShellExample {

  public static void main(java.lang.String[] args) throws Exception {
    try {
      // If running this Java client under MS windows then make sure the J-Integra® 'bin' directory
      // is in your PATH, otherwise run DCOMCNFG on the machine running the shell (Start|Run|DCOMCNFG),
      // grant a specific user default launch and access permissions, and uncomment the following
      // line, specifying the appropriate domain/user/password
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
      Shell shell = new Shell();

      // Minimizes all windows
      shell.minimizeAll();

      // Find all the drives on this machine
      Folder drives = shell.nameSpace(new Integer(ShellSpecialFolderConstants.ssfDRIVES));
      FolderItems items = drives.items();
      for (int i = 0; i < items.getCount(); i++) {
        FolderItem item = items.item(new Integer(i));
        if (item.IsFileSystem()) {
          // Otherwise we get things like the 'control panel'
          System.out.println(item.getName());
        }
      }
    } finally {
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}