jni
Class JNIUtilities

java.lang.Object
  extended byjni.JNIUtilities

public class JNIUtilities
extends java.lang.Object

A collection of utilities for generating Java Native Interface (JNI) classes.

For information about JNI, see http://java.sun.com/docs/books/tutorial/native1.1/concepts/index.html

For information about how to create shared libraries under Windows using Cygwin, see http://cygwin.com/cygwin-ug-net/dll.html

For information about using JNI with Cygwin, see http://www.inonit.com/cygwin/jni/helloWorld/c.html

How to call an actor that calls C code that returns a double

In this example, we call a C method called meaningOfLife() that returns a double.
  1. Create the C file called meaningOfLife.c that contains
       // Return the answer to "the meaning of life, the universe, and everything"
       double meaningOfLife() {
       return 42.0;
       }
       
  2. Create meaningOfLife.h that contains
       extern "C" double meaningOfLife();
       
  3. Compile and create the shared library. Under Windows, we create a .dll
       gcc -shared -o meaningOfLife.dll meaningOfLife.c
       
  4. Set the CLASSPATH to include the current directory
       CLASSPATH=.
       export CLASSPATH
       
  5. Start up Vergil with the JNI interface enabled.
       $PTII/bin/vergil -jni
       
    Note that the -jni option may go away in the future if we merge the jni facility into the main tree
  6. Create a new model with File -> New -> Graph Editor
  7. Drag in the JNIActor from the jni folder
  8. Right click on the actor and select Configure Arguments.
  9. Fill in the form as follows
    Name:
    output
    C or C++ type:
    double
    Kind:
    return
  10. Select Ok and then Commit to close the argument configurer
  11. Right Click on the actor to edit the parameters
    libraryDirectory
    ""
    nativeFunction:
    "meaningOfLife"
    nativeLibrary
    "meaningOfLife"
  12. Select Commit to close the Edit Parameters window
  13. Select JNI from the menu and then select Generate C Interface
  14. FIXME: Copy the dlls into a directory that is in the path
       cp meaningOfLife.dll $PTII/bin
       cp jni/jnimeaningOfLife/JnijnimeaningOfLife.dll $PTII/bin
       
  15. Save the model.
    FIXME: Because of an apparent bug, it is necessary to save the model for the port we just created to appear
  16. Add a Display actor from the Sink folder in the Actor Library and connect the input of the Display actor to the output of the meaningOfLife Actor
  17. FIXME: Because of a bug in the Ptolemy interface to the JNI actor, you must specify the type of the output of the meaningOfLife actor by right clicking on the actor and selecting Configure Ports and then entering double for output type
  18. Drag in a SDF director and set the number of iterations to 1.
  19. Select Run
  20. The answer to "the meaning of life, the universe, and everything" should appear in the Display actor.
If you get an error like:
   /cygdrive/c/Program Files/j2sdk1.4.1_01/include/win32/jni_md.h:16: syntax error before `;'
   
Then see http://www.xraylith.wisc.edu/~khan/software/gnu-win32/README.jni.txt
You might need to edit jni_md.h, the above URL says
GCC doesn't have a __int64 built-in, and this patch basically uses "long long" instead.
  1. Edit the file /include/win32/jni_md.h, Where is the installation root (eg., c:/jdk1.1.7A).
  2. Replace the segment:
   typedef long jint;
   typedef __int64 jlong;
   
with:
   typedef long jint;
   #ifdef __GNUC__
   typedef long long jlong;
   #else
   typedef __int64 jlong;
   #endif
   typedef signed char jbyte;
   

Since:
Ptolemy II 2.3
Version:
$Id: JNIUtilities.java,v 1.49 2004/04/21 03:04:25 cxh Exp $
Author:
Vincent Arnould (vincent.arnould@thalesgroup.com), contributor Christopher Hylands
Accepted Rating:
Red (vincent.arnould)
Proposed Rating:
Red (vincent.arnould)

Method Summary
protected static java.io.File _exportCInterfaceFile(GenericJNIActor actor, java.lang.String destinationDirectory)
          Create the JNI C file.
protected static void _exportDSP(GenericJNIActor actor, java.lang.String destinationDirectory)
          Export the Visual Studio project.
protected static java.io.File _exportJavaInterfaceFile(GenericJNIActor actor, java.lang.String destinationDirectory)
          Create the JNI Java file.
protected static void _exportMakefile(GenericJNIActor actor, java.lang.String destinationDirectory)
          Export a makefile
protected static java.util.Vector _getArguments(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn)
          Get the args belonging to this entity.
protected static java.lang.String _getArguments(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
          Get the args belonging to this entity.
protected static java.util.Vector _getArgumentsIn(GenericJNIActor actor)
          Get the args In belonging to this entity.
protected static java.lang.String _getArgumentsIn(GenericJNIActor actor, java.lang.String separator)
          Get the args In name belonging to this entity.
protected static java.util.Vector _getArgumentsInOut(GenericJNIActor actor)
          Get the args InOut belonging to this entity.
protected static java.lang.String _getArgumentsInOut(GenericJNIActor actor, java.lang.String separator)
          Get the args InOut belonging to this entity.
protected static java.lang.String _getArgumentsInOutWithCType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their c type.
protected static java.lang.String _getArgumentsInOutWithJNIType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsInOutWithJType(GenericJNIActor actor, java.lang.String separator)
          Get the args Out belonging to this entity with their java type.
protected static java.lang.String _getArgumentsInWithCType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their c type.
protected static java.lang.String _getArgumentsInWithJNIType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsInWithJType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their java type.
protected static java.util.Vector _getArgumentsOut(GenericJNIActor actor)
          Get the args Out belonging to this entity.
protected static java.lang.String _getArgumentsOut(GenericJNIActor actor, java.lang.String separator)
          Get the args out name belonging to this entity.
protected static java.lang.String _getArgumentsOutWithCType(GenericJNIActor actor, java.lang.String separator)
          Get the args Out belonging to this entity with their c type.
protected static java.lang.String _getArgumentsOutWithJNIType(GenericJNIActor actor, java.lang.String separator)
          Get the args In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsOutWithJType(GenericJNIActor actor, java.lang.String separator)
          Get the args Out belonging to this entity with their java type.
protected static java.lang.String _getArgumentsWithCType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
          Get the args belonging to this entity with their c type.
protected static java.lang.String _getArgumentsWithCTypeCast(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
          Get the arguments belonging to this entity with their c type.
protected static java.lang.String _getArgumentsWithJNIType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
          Get the arguments In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsWithJType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
          Get the arguments belonging to this entity with their java type.
protected static java.lang.String _signature(java.lang.String typ)
           
protected static java.lang.String _signatureSendResults(GenericJNIActor actor)
           
static boolean generateJNI(CompositeEntity model)
          Given a model, generate JNI files for all GenericJNIActors.
static void generateJNI(CompositeEntity model, GenericJNIActor actor)
          Generate JNI files for one actor in a model
static java.lang.String getNativeLibrary(GenericJNIActor actor)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

generateJNI

public static boolean generateJNI(CompositeEntity model)
                           throws java.lang.Exception
Given a model, generate JNI files for all GenericJNIActors.

Parameters:
model - The model to generate JNI files for any contained GenericJNIActors.
Returns:
true if a GenericJNIActor was found.
Throws:
java.lang.Exception - If there was a problem creating the JNI files.

generateJNI

public static void generateJNI(CompositeEntity model,
                               GenericJNIActor actor)
                        throws java.lang.Exception
Generate JNI files for one actor in a model

Throws:
java.lang.Exception

getNativeLibrary

public static java.lang.String getNativeLibrary(GenericJNIActor actor)
                                         throws IllegalActionException
Throws:
IllegalActionException

_exportCInterfaceFile

protected static java.io.File _exportCInterfaceFile(GenericJNIActor actor,
                                                    java.lang.String destinationDirectory)
                                             throws IllegalActionException,
                                                    java.io.IOException
Create the JNI C file.

Parameters:
actor - Actor to generate a JNI C file for.
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException
java.io.IOException

_exportDSP

protected static void _exportDSP(GenericJNIActor actor,
                                 java.lang.String destinationDirectory)
                          throws IllegalActionException,
                                 java.io.IOException
Export the Visual Studio project.

Parameters:
actor - Actor to generate a JNI Java file for.
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException
java.io.IOException

_exportJavaInterfaceFile

protected static java.io.File _exportJavaInterfaceFile(GenericJNIActor actor,
                                                       java.lang.String destinationDirectory)
                                                throws IllegalActionException,
                                                       java.io.IOException
Create the JNI Java file.

Parameters:
actor - Actor to generate a JNI Java file for.
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException
java.io.IOException

_exportMakefile

protected static void _exportMakefile(GenericJNIActor actor,
                                      java.lang.String destinationDirectory)
                               throws IllegalActionException,
                                      java.io.IOException
Export a makefile

Parameters:
actor - Actor to generate a makefile for
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException
java.io.IOException

_getArguments

protected static java.util.Vector _getArguments(GenericJNIActor actor,
                                                boolean isInput,
                                                boolean isOutput,
                                                boolean isReturn)
Get the args belonging to this entity.

Returns:
a vector of in arguments.

_getArguments

protected static java.lang.String _getArguments(GenericJNIActor actor,
                                                boolean isInput,
                                                boolean isOutput,
                                                boolean isReturn,
                                                java.lang.String separator)
Get the args belonging to this entity.

Returns:
the name of each in arguments.

_getArgumentsIn

protected static java.util.Vector _getArgumentsIn(GenericJNIActor actor)
Get the args In belonging to this entity.

Returns:
a vector of out arguments, excluding the in arguments.

_getArgumentsIn

protected static java.lang.String _getArgumentsIn(GenericJNIActor actor,
                                                  java.lang.String separator)
Get the args In name belonging to this entity.

Returns:
the name of the out arguments, excluding the in arguments.

_getArgumentsInOut

protected static java.util.Vector _getArgumentsInOut(GenericJNIActor actor)
Get the args InOut belonging to this entity.

Returns:
a vector of inout arguments.

_getArgumentsInOut

protected static java.lang.String _getArgumentsInOut(GenericJNIActor actor,
                                                     java.lang.String separator)
Get the args InOut belonging to this entity.

Returns:
a vector of inout arguments.

_getArgumentsInOutWithCType

protected static java.lang.String _getArgumentsInOutWithCType(GenericJNIActor actor,
                                                              java.lang.String separator)
Get the args In belonging to this entity with their c type.

Returns:
the c type and name of each in arguments.

_getArgumentsInOutWithJNIType

protected static java.lang.String _getArgumentsInOutWithJNIType(GenericJNIActor actor,
                                                                java.lang.String separator)
Get the args In belonging to this entity with their JNI type, excluding the out arguments.

Returns:
the JNI type and name of each in arguments.

_getArgumentsInOutWithJType

protected static java.lang.String _getArgumentsInOutWithJType(GenericJNIActor actor,
                                                              java.lang.String separator)
Get the args Out belonging to this entity with their java type.

Returns:
the name and the java type of the out arguments, excluding the in arguments.

_getArgumentsInWithCType

protected static java.lang.String _getArgumentsInWithCType(GenericJNIActor actor,
                                                           java.lang.String separator)
Get the args In belonging to this entity with their c type.

Returns:
the c type of the out arguments.

_getArgumentsInWithJNIType

protected static java.lang.String _getArgumentsInWithJNIType(GenericJNIActor actor,
                                                             java.lang.String separator)
Get the args In belonging to this entity with their JNI type, excluding the out arguments.

Returns:
the JNI type and name of each in arguments, excluding the in arguments.

_getArgumentsInWithJType

protected static java.lang.String _getArgumentsInWithJType(GenericJNIActor actor,
                                                           java.lang.String separator)
Get the args In belonging to this entity with their java type.

Returns:
the name and the java type of the out arguments, excluding the in arguments.

_getArgumentsOut

protected static java.util.Vector _getArgumentsOut(GenericJNIActor actor)
Get the args Out belonging to this entity.

Returns:
a vector of out arguments, excluding the in arguments.

_getArgumentsOut

protected static java.lang.String _getArgumentsOut(GenericJNIActor actor,
                                                   java.lang.String separator)
Get the args out name belonging to this entity.

Returns:
the name of the out arguments, excluding the in arguments.

_getArgumentsOutWithCType

protected static java.lang.String _getArgumentsOutWithCType(GenericJNIActor actor,
                                                            java.lang.String separator)
Get the args Out belonging to this entity with their c type.

Returns:
the c type of the out arguments.

_getArgumentsOutWithJNIType

protected static java.lang.String _getArgumentsOutWithJNIType(GenericJNIActor actor,
                                                              java.lang.String separator)
Get the args In belonging to this entity with their JNI type, excluding the out arguments.

Returns:
the JNI type and name of each in arguments, excluding the in arguments.

_getArgumentsOutWithJType

protected static java.lang.String _getArgumentsOutWithJType(GenericJNIActor actor,
                                                            java.lang.String separator)
Get the args Out belonging to this entity with their java type.

Returns:
the name and the java type of the out arguments, excluding the in arguments.

_getArgumentsWithCType

protected static java.lang.String _getArgumentsWithCType(GenericJNIActor actor,
                                                         boolean isInput,
                                                         boolean isOutput,
                                                         boolean isReturn,
                                                         java.lang.String separator)
Get the args belonging to this entity with their c type.

Returns:
the c type and name of each in arguments.

_getArgumentsWithCTypeCast

protected static java.lang.String _getArgumentsWithCTypeCast(GenericJNIActor actor,
                                                             boolean isInput,
                                                             boolean isOutput,
                                                             boolean isReturn,
                                                             java.lang.String separator)
Get the arguments belonging to this entity with their c type.

Returns:
the c type and name of each in arguments.

_getArgumentsWithJNIType

protected static java.lang.String _getArgumentsWithJNIType(GenericJNIActor actor,
                                                           boolean isInput,
                                                           boolean isOutput,
                                                           boolean isReturn,
                                                           java.lang.String separator)
Get the arguments In belonging to this entity with their JNI type, excluding the out arguments.

Returns:
the JNI type and name of each in arguments.

_getArgumentsWithJType

protected static java.lang.String _getArgumentsWithJType(GenericJNIActor actor,
                                                         boolean isInput,
                                                         boolean isOutput,
                                                         boolean isReturn,
                                                         java.lang.String separator)
Get the arguments belonging to this entity with their java type.

Returns:
the java type and name of each in arguments

_signature

protected static java.lang.String _signature(java.lang.String typ)
Returns:
the signature of the interface function.

_signatureSendResults

protected static java.lang.String _signatureSendResults(GenericJNIActor actor)
Returns:
the signature of the interface function.