JNI Functions

 

Using the JExecutable JNI functions

Some of the JExecutable wrappers provides Windows-specific JNI features that allow your application to optionnaly access native information.

In order to use those features, you must enable the JExecutable JNI option, and add the JExecutable.Native calls in your java code. You need to build your project using the jnihelper.jar library, but don't need to package it with your application, as it is embedded (as well as the native functions implementation) in the wrappers.

Before using any JNI feature from the JExecutable.Native, you must check that the native methods are correctly bound to their native implementation by using the JExecutable.Native.isAvailable() method. If this method returns false, you cannot use any of the native method.

Examples

Start the default application for a file

Using the shellExecute(), you can start the default application associated by windows to a document and perform any of the action among SHELLEXECUTE_OPEN (opens the file for visualization), SHELLEXECUTE_EDIT (opens the file for editing), SHELLEXECUTE_PRINT (print the document), SHELLEXECUTE_FIND (launch the windows Find utility on a specific folder), SHELLEXECUTE_EXPLORE (launch the windows file Explorer tool on a specific folder).

 if (JExecutable.Native.isAvailable())

   {

   JExecutable.Native.shellExecute(JExecutable.Native.SHELLEXECUTE_OPEN,

                               "c:\\myfile.pdf",  null, null,

                               JExecutable.Native.SW_SHOWNORMAL);

   }

Launching the default web browser

There are some java libraries available around to launch the default browser on an URL, but using the shellExecute() is probably the best solution for the windows platform on this traditional problem.

 URL target = "http://JExecutable.sourceforge.net/";

 if (JExecutable.Native.isAvailable())

   {

     JExecutable.Native.shellExecute(JExecutable.Native.SHELLEXECUTE_OPEN,

                                 target.toString(), 

                                 null, null,

                                 JExecutable.Native.SW_NORMAL);

   }

  else

   {

     // Use a traditionnal java-based guessing method

     // to find and launch the default browser

   }

Reboot the computer

It may be useful to reboot or log off the computer/session programmatically at a given time. You can use the JExecutable exitWindows() method to shutdown or reboot the computer, but also disconnect the session of the user.

 // Log off the user session

 if (JExecutable.Native.isAvailable())

   {

     JExecutable.Native.exitWindows(JExecutable.Native.EXITWINDOWS_LOGOFF);

   }

Retrieve information on a disk drive

There is no way to distinguish between a removeable media, a cdrom drive, or a fixed disk using the standard java API. It is however a good usability point to use different icons to display fixed, removeable medias, remote drives, or cdrom. It may also be useful to know if there are enough free space on disk before saving a file, or to be aware that the filesystem used for a media is FAT16, in order to alert the user of a possible loss of precision in the filenames.

The JExecutable.DriveInfo object provides all this information for free, provided your application is launched with a jni-enabled JExecutable wrapper.

  File f = new File("d:/my/file/somewhere");

  JExecutable.DriveInfo di = JExecutable.Native.getDriveInfo(f);

 

  if (di.getDriveType() == JExecutable.DriveInfo.DRIVE_REMOVABLE)

  {

     // This file is on a removeable drive !

  }

 

  if (di.getFreeSpaceForUser() < (1024*1024*64))

  {

   // there are less than 64MB free for the

   // user on this drive !

  }

 

[Home] [Features] [Create Java Exe] [Command Line] [Ant Task] [JNI Functions] [JExecutable FAQ] [License]

jexecutable@gurusoft.net