.. _Using_javastub_to_Generate_Ada_Package_Specifications:

*******************************************************
Using javastub to Generate Ada Package Specifications
*******************************************************

.. index:: javastub command

The ``javastub`` utility program generates an Ada package specification
from a Java class file that has native methods.

It is invoked as follows


::

  $ javastub [<filename>.class] {<filename>.class}
  

where each ``<filename>.class`` is a class file
for a Java class that has native methods.

For each class file argument, ``javastub`` generates an
Ada package specification with subprograms corresponding to the native
methods.  The name of the generated file is ``<filename>_jni.ads``.

The ``javastub`` utility is the Ada analog to the
``javah -jni`` command in the Java Development Kit, which takes
a class file as input and produces a C header file with the
correct function prototypes.

You can use ``javastub`` if you intend to use Ada, rather than C,
to implement the native methods in a Java class.  You will then be
responsible for doing the necessary JNI programming, using the Ada binding
to the C JNI library.

As an example, here is a simple Java class with a native method:


::

  // Foo.java
  class Foo{
      native void bar(int i);
  }
  

Compile the Java source file and then invoke ``javastub``
on the class file:


::

  $ javac Foo.java
  $ javastub Foo.class
  

The following file ``foo_jni.ads`` is generated by ``javastub``:


::

  --  Stub for class Foo
  with Interfaces.Java.JNI; use Interfaces.Java.JNI;
  package Foo_JNI is
     --  Class:     Foo
     --  Method:    bar
     --  Signature: (I)V
     procedure bar (Env : JNI_Env_Access; Obj : J_Object; P1 : J_Int);
     pragma Export (C, bar, "Java_Foo_bar__I");
  end Foo_JNI;

