[[Property:title|Eiffel2Java Tutorial]] [[Property:weight|0]] [[Property:uuid|2d0fc62b-90e9-650b-2896-d92db66899c9]] ==Introduction== The Java interface allows you to call Java routines or attributes from your Eiffel code. It uses the Java Native Interface (JNI) provided by the Java Development Kit (JDK). You can get more information about the JNI at: [http://java.sun.com/j2se/1.5.0/docs/guide/jni/ http://java.sun.com/j2se/1.5.0/docs/guide/jni/] ===Requirements=== * JDK 1.1.8 or newer should be correctly set up (download it at [http://java.sun.com/javase/downloads/index.jsp http://java.sun.com/javase/downloads/index.jsp] ) * The environment variable CLASSPATH should defined (check with the JDK documentation on how to do so) and that it contains the Java classes you want to access. * The environment variables should be setup correctly. See $ISE_EIFFEL\library\Eiffel2Java\README.txt for information how to do this. ===Limitations=== * In this version, you can only use one JNI environment. * Only one thread can interact with the Java Virtual Machine (JVM). * It is not possible to call Eiffel features from Java program. * The Eiffel feature `destroy_vm` of `JAVA_VM` calls a C function of the Java NativeInterface that is not fully implemented in jdk 1.1.8. This function, called DestroyJavaVM, always returns -1 in jdk 1.1.8. For further information, go on the JNI pages at the address above. ==Interface classes== ===JNI_ENVIRONMENT=== Holds information about JNI environment. Potentially many JNI environments can exists at once, but more than one was never tested. This class provide the facilities to interact with the JVM: * creation of instances of Java Classes * exceptions mechanism * destruction of the JVM ===SHARED_JNI_ENVIRONMENT=== Shared JNI environment. Since one JNI is needed per thread we limit Eiffel to having one thread that deals with Java. The class that calls Java routines or attributes must inherit from this class. ===JAVA_VM=== This class is used to initially load the JVM into the running program. This is the Eiffel representation of the JVM. ===JAVA_CLASS=== Access to Java classes. Static methods and attributes are accessed via this class.This is the Eiffel representation of a Java Class. ===JAVA_OBJECT=== This class gives Eiffel access to Java objects. You can use it directly or inherit from to and create a more convenient Eiffel class that makes the Java object look like an Eiffel object. The Eiffel representation of a Java Object. {{warning|to access the static fields or routines of a Java Class, you have to use the features of a JAVA_CLASS instance!! }} ===JAVA_EXTERNALS=== JNI external declarations. Don't use this class directly. ===JAVA_***_ARRAY=== Access to Java array of "***". "***" can be all the usual types of Java (byte, short, int, float, double, char, boolean) or object if it is an array of Java Object (a String is considered as an object) ===JAVA_ARGS=== Class representing the arguments that can be passed to a Java method. See below about the signature of the methods ===JAVA_OBJECT_TABLE=== This class provides a mapping between Java and Eiffel objects '''Mapping the Eiffel classes and the Java types:''' The following table describes the mapping of Java primitive types and classes to Eiffel classes. {| |- | '''Java type/class''' | '''Eiffel class''' |- | boolean | BOOLEAN |- | char, byte | CHARACTER |- | short, int | INTEGER |- | long | INTEGER_64 |- | float | REAL |- | double | DOUBLE |- | String | STRING |- | void | Void |} The interface does the mapping automatically. For example, if you call a Java method that returns a 'float', by using float_method you will get a REAL. '''The signature of Java methods and attributes:''' When you want to call a Java method or access a field, you need to specify its signature.The Eiffel to Java interface follows the JNI specifications. The table below summarizes the encoding for the Java type signatures: {| |- | '''Signature''' | '''Java Type''' |- | Z | boolean |- | B | byte |- | C | char |- | S | short |- | I | int |- | J | long |- | F | float |- | D | double |- | V | void |- | [type | type [] |} The signature for a Java class has the following form: L fully-qualified-class; For example, class String: Ljava/lang/String; The signature for a method has the following form: (arguments-types) returned-types For example, the signature of a method that takes as arguments an integer and a string and return void is: (ILjava/lang/String;)V