Misplaced Pages

Java Native Interface

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

In software design , the Java Native Interface ( JNI ) is a foreign function interface programming framework that enables Java code running in a Java virtual machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C , C++ and assembly .

#450549

62-453: JNI enables programmers to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e.g. when the standard Java class library does not support the platform-specific features or program library. It is also used to modify an existing application (written in another programming language) to be accessible to Java applications. Many of

124-671: A jobject pointer, and any Java arguments declared by the Java method. For example, the following converts a Java string to a native string: The env pointer is a structure that contains the interface to the JVM. It includes all of the functions necessary to interact with the JVM and to work with Java objects. Example JNI functions are converting native arrays to/from Java arrays, converting native strings to/from Java strings, instantiating objects, throwing exceptions, etc. Basically, anything that Java code can do can be done using JNIEnv , albeit with considerably less ease. The argument obj

186-467: A constructor and a destructor . An object expresses data type as an interface – the type of each member variable and the signature of each member function (method). A class defines an implementation of an interface, and instantiating the class results in an object that exposes the implementation via the interface. In the terms of type theory, a class is an implementation‍—‌a concrete data structure and collection of subroutines‍—‌while

248-1138: A pair of 16- bit codes: one High Surrogate and one Low Surrogate. A single surrogate code point will never be assigned a character. 65,520 of the 65,536 code points in this plane have been allocated to a Unicode block, leaving just 16 code points in a single unallocated range (2FE0..2FEF). As of Unicode 16.0 , the BMP comprises the following 164 blocks: Plane 1 , the Supplementary Multilingual Plane ( SMP ), contains historic scripts (except CJK ideographic), and symbols and notation used within certain fields. Scripts include Linear B , Egyptian hieroglyphs , and cuneiform scripts. It also includes English reform orthographies like Shavian and Deseret , and some modern scripts like Osage , Warang Citi , Adlam , Wancho and Toto . Symbols and notations include historic and modern musical notation ; mathematical alphanumerics ; shorthands; Emoji and other pictographic sets; and game symbols for playing cards , mahjong , and dominoes . As of Unicode 16.0 ,

310-607: A Java Virtual Machine ( Visual J++ ) had a similar mechanism for calling native code from Java, called the Raw Native Interface ( RNI ). In addition, it had an easy way to call existing native code that was not itself aware of Java, such as (but not limited to) the Windows API, called J/Direct . However, following the Sun–Microsoft litigation about this implementation, Visual J++ is no longer maintained. RNI

372-603: A UITableView is a UIScrollView is a UIView is a UIResponder is an NSObject. In object-oriented analysis and in Unified Modelling Language (UML), an association between two classes represents a collaboration between the classes or their corresponding instances. Associations have direction; for example, a bi-directional association between two classes indicates that both of the classes are aware of their relationship. Associations may be labeled according to their name or purpose. An association role

434-445: A button, and all the buttons together compose the interface (other television sets that are the same model as yours would have the same interface). In its most common form, an interface is a specification of a group of related methods without any associated implementation of the methods. A television set also has a myriad of attributes , such as size and whether it supports color, which together comprise its structure. A class represents

496-405: A class from its interface: the internal structure is made private, while public accessor methods can be used to inspect or alter such private data. Access specifiers do not necessarily control visibility , in that even private members may be visible to client external code. In some languages, an inaccessible but visible member may be referred to at runtime (for example, by a pointer returned from

558-422: A collection of objects, such as instances of Body , Engine , Tires , etc. Object modeling languages such as UML include capabilities to model various aspects of "part of" and other kinds of relations – data such as the cardinality of the objects, constraints on input and output values, etc. This information can be utilized by developer tools to generate additional code besides the basic data definitions for

620-501: A concrete sub class. An abstract class is either labeled as such explicitly or it may simply specify abstract methods (or virtual methods ). An abstract class may provide implementations of some methods, and may also specify virtual methods via signatures that are to be implemented by direct or indirect descendants of the abstract class. Before a class derived from an abstract class can be instantiated, all abstract methods of its parent classes must be implemented by some class in

682-504: A member function), but an attempt to use it by referring to the name of the member from the client code will be prevented by the type checker. The various object-oriented programming languages enforce member accessibility and visibility to various degrees, and depending on the language's type system and compilation policies, enforced at either compile time or runtime . For example, the Java language does not allow client code that accesses

SECTION 10

#1732776026451

744-501: A particular object or with all objects of that class. Object state can differ between each instance of the class whereas the class state is shared by all of them. The object methods include access to the object state (via an implicit or explicit parameter that references the object) whereas class methods do not. If the language supports inheritance , a class can be defined based on another class with all of its state and behavior plus additional state and behavior that further specializes

806-421: A superclass of Rectangle and Ellipse , while Square would be a subclass of Rectangle . These are all subset relations in set theory as well, i.e., all squares are rectangles but not all rectangles are squares. A common conceptual error is to mistake a part of relation with a subclass. For example, a car and truck are both kinds of vehicles and it would be appropriate to model them as subclasses of

868-601: A type is an interface . Different (concrete) classes can produce objects of the same (abstract) type (depending on type system). For example, the type (interface) Stack might be implemented by SmallStack that is fast for small stacks but scales poorly and ScalableStack that scales well but has high overhead for small stacks. A class contains data field descriptions (or properties , fields , data members , or attributes ). These are usually field types and names that will be associated with state variables at program run time; these state variables either belong to

930-491: A vehicle class. However, it would be an error to model the parts of the car as subclass relations. For example, a car is composed of an engine and body, but it would not be appropriate to model an engine or body as a subclass of a car. In object-oriented modeling these kinds of relations are typically modeled as object properties. In this example, the Car class would have a property called parts . parts would be typed to hold

992-407: Is a common set of access specifiers : Although many object-oriented languages support the above access specifiers,their semantics may differ. Object-oriented design uses the access specifiers in conjunction with careful design of public method implementations to enforce class invariants—constraints on the state of the objects. A common usage of access specifiers is to separate the internal data of

1054-487: Is a reference to the Java object inside which this native method has been declared. Native data types can be mapped to/from Java data types. For compound types such as objects, arrays and strings the native code must explicitly convert the data by calling methods in the JNIEnv . A JNI environment pointer ( JNIEnv* ) is passed as an argument for each native function mapped to a Java method, allowing for interaction with

1116-417: Is also commonly known as a has-a relationship. For example, a class "Car" could be composed of and contain a class "Engine". Therefore, a Car has an Engine. One aspect of composition is containment, which is the enclosure of component instances by the instance that has them. If an enclosing object contains component instances by value, the components and their enclosing object have a similar lifetime . If

1178-545: Is called a pure abstract base class (or pure ABC ) in C++ and is also known as an interface by users of the language. Other languages, notably Java and C#, support a variant of abstract classes called an interface via a keyword in the language. In these languages, multiple inheritance is not allowed, but a class can implement multiple interfaces. Such a class can only contain abstract publicly accessible methods. In some languages, classes can be declared in scopes other than

1240-405: Is given end of an association and describes the role of the corresponding class. For example, a "subscriber" role describes the way instances of the class "Person" participate in a "subscribes-to" association with the class "Magazine". Also, a "Magazine" has the "subscribed magazine" role in the same association. Association role multiplicity describes how many instances correspond to each instance of

1302-411: Is not an intrinsic aspect of classes. An object-based language (i.e. Classic Visual Basic ) supports classes yet does not support inheritance. A programming language may support various class relationship features. Classes can be composed of other classes, thereby establishing a compositional relationship between the enclosing class and its embedded classes. Compositional relationship between classes

SECTION 20

#1732776026451

1364-456: Is only available since J2SE 1.3. JNI also allows direct access to assembly code , without even going through a C bridge. Accessing Java applications from assembly is possible in the same way. In the JNI framework, native functions are implemented in separate .c or .cpp files. (C++ provides a slightly simpler interface with JNI.) When the JVM invokes the function, it passes a JNIEnv pointer,

1426-463: The V code. These types are interchangeable. One can use jint where you normally use an int , and vice versa, without any typecasting required. However, mapping between Java Strings and arrays to native strings and arrays is different. If a jstring is used where a char * would be, the code could crash the JVM. JNI incurs considerable overhead and performance loss under certain circumstances: Microsoft's proprietary implementation of

1488-641: The Basic Multilingual Plane ( BMP ), contains characters for almost all modern languages, and a large number of symbols . A primary objective for the BMP is to support the unification of prior character sets as well as characters for writing . Most of the assigned code points in the BMP are used to encode Chinese, Japanese, and Korean ( CJK ) characters. The High Surrogate ( U+D800–U+DBFF ) and Low Surrogate ( U+DC00–U+DFFF ) codes are reserved for encoding non-BMP characters in UTF-16 by using

1550-601: The Internet requires this level of flexibility and the technology standards such as the Web Ontology Language (OWL) are designed to support it. A similar issue is whether or not the class hierarchy can be modified at run time. Languages such as Flavors, CLOS, and Smalltalk all support this feature as part of their meta-object protocols . Since classes are themselves first-class objects, it is possible to have them dynamically alter their structure by sending them

1612-634: The JNI environment within the native method. This JNI interface pointer can be stored, but remains valid only in the current thread. Other threads must first call AttachCurrentThread() to attach themselves to the VM and obtain a JNI interface pointer. Once attached, a native thread works like a regular Java thread running within a native method. The native thread remains attached to the VM until it calls DetachCurrentThread() to detach itself. The JNI framework does not provide any automatic garbage collection for non-JVM memory resources allocated by code executing on

1674-503: The JVM. On Windows platforms, Structured Exception Handling (SEH) may be employed to wrap native code in SEH try/catch blocks so as to capture machine (CPU/FPU) generated software interrupts (such as NULL pointer access violations and divide-by-zero operations), and to handle these situations before the interrupt is propagated back up into the JVM (i.e. Java side code), in all likelihood resulting in an unhandled exception. The encoding used for

1736-599: The NewStringUTF, GetStringUTFLength, GetStringUTFChars, ReleaseStringUTFChars and GetStringUTFRegion functions is "modified UTF-8", which is not valid UTF-8 for all inputs, but a different encoding really. The null character (U+0000) and codepoints not on the Basic Multilingual Plane (greater than or equal to U+10000, i.e. those represented as surrogate pairs in UTF-16) are encoded differently in modified UTF-8. Many programs actually use these functions incorrectly and treat

1798-664: The SMP comprises the following 161 blocks: Plane 2 , the Supplementary Ideographic Plane ( SIP ), is used for CJK Ideographs, mostly CJK Unified Ideographs , that were not included in earlier character encoding standards. As of Unicode 16.0 , the SIP comprises the following seven blocks: Plane 3 is the Tertiary Ideographic Plane (TIP). CJK Unified Ideographs Extension G was added to

1860-559: The TIP in Unicode 13.0, released in March 2020. It also is tentatively allocated for Oracle Bone script and Small Seal Script . As of Unicode 16.0 , the TIP comprises the following two blocks: Planes 4 to 13 (planes 4 to D in hexadecimal ): No characters have yet been assigned, or proposed for assignment, to Planes 4 through 13. Plane 14 ( E in hexadecimal) is designated as

1922-565: The UTF-8 strings returned or passed into the functions as standard UTF-8 strings instead of modified UTF-8 strings. Programs should use the NewString, GetStringLength, GetStringChars, ReleaseStringChars, GetStringRegion, GetStringCritical and ReleaseStringCritical functions, which use UTF-16LE encoding on little-endian architectures and UTF-16BE on big-endian architectures, and then use a UTF-16 to UTF-8 conversion routine. The following table shows

Java Native Interface - Misplaced Pages Continue

1984-507: The appropriate messages. Other languages that focus more on strong typing such as Java and C++ do not allow the class hierarchy to be modified at run time. Semantic web objects have the capability for run time changes to classes. The rationale is similar to the justification for allowing multiple superclasses, that the Internet is so dynamic and flexible that dynamic changes to the hierarchy are required to manage this volatility. Although many class-based languages support inheritance, inheritance

2046-410: The class from outside the enclosing class. A related concept is inner types , also known as inner data type or nested type , which is a generalization of the concept of inner classes. C++ is an example of a language that supports both inner classes and inner types (via typedef declarations). A local class is a class defined within a procedure or function. Such structure limits references to

2108-431: The class name to within the scope where the class is declared. Depending on the semantic rules of the language, there may be additional restrictions on local classes compared to non-local ones. One common restriction is to disallow local class methods to access local variables of the enclosing function. For example, in C++, a local class may refer to static variables declared within its enclosing function, but may not access

2170-465: The class or specific instances of the class. In most languages, the structure defined by the class determines the layout of the memory used by its instances. Other implementations are possible: for example, objects in Python use associative key-value containers. Some programming languages such as Eiffel support specification of invariants as part of the definition of the class, and enforce them through

2232-453: The class. The specialized class is a sub-class , and the class it is based on is its superclass . As an instance of a class, an object is constructed from a class via instantiation . Memory is allocated and initialized for the object state and a reference to the object is provided to consuming code. The object is usable until it is destroyed – its state memory is de-allocated. Most languages allow for custom logic at lifecycle events via

2294-597: The classes that they are derived from. For example, if "class A" inherits from "class B" and if "class B" implements the interface "interface B" then "class A" also inherits the functionality(constants and methods declaration) provided by "interface B". In languages that support access specifiers , the interface of a class is considered to be the set of public members of the class, including both methods and attributes (via implicit getter and setter methods ); any private members or internal data structures are not intended to be depended on by external code and thus are not part of

2356-502: The components are contained by reference, they may not have a similar lifetime. For example, in Objective-C 2.0: This Car class has an instance of NSString (a string object), Engine , and NSArray (an array object). Classes can be derived from one or more existing classes, thereby establishing a hierarchical relationship between the derived-from classes ( base classes , parent classes or superclasses ) and

2418-474: The derivation chain. Most object-oriented programming languages allow the programmer to specify which classes are considered abstract and will not allow these to be instantiated. For example, in Java , C# and PHP , the keyword abstract is used. In C++ , an abstract class is a class having at least one abstract method given by the appropriate syntax in that language (a pure virtual function in C++ parlance). A class consisting of only pure virtual methods

2480-624: The derived class ( child class or subclass ) . The relationship of the derived class to the derived-from classes is commonly known as an is-a relationship. For example, a class 'Button' could be derived from a class 'Control'. Therefore, a Button is a Control. Structural and behavioral members of the parent classes are inherited by the child class. Derived classes can define additional structural members (data fields) and behavioral members (methods) in addition to those that they inherit and are therefore specializations of their superclasses. Also, derived classes can override inherited methods if

2542-1753: The entirety of planes 15 and 16). For future usage, ranges of characters have been tentatively mapped out for most known current and ancient writing systems. 0000–​0FFF 1000–​1FFF 2000–​2FFF 3000–​3FFF 4000–​4FFF 5000–​5FFF 6000–​6FFF 7000–​7FFF 8000–​8FFF 9000–​9FFF A000–​AFFF B000–​BFFF C000–​CFFF D000–​DFFF E000–​EFFF F000–​FFFF 10000–​10FFF 11000–​11FFF 12000–​12FFF 13000–​13FFF 14000–​14FFF 16000–​16FFF 17000–​17FFF 18000–​18FFF 1A000–​1AFFF 1B000–​1BFFF 1C000–​1CFFF 1D000–​1DFFF 1E000–​1EFFF 1F000–​1FFFF 20000–​20FFF 21000–​21FFF 22000–​22FFF 23000–​23FFF 24000–​24FFF 25000–​25FFF 26000–​26FFF 27000–​27FFF 28000–​28FFF 29000–​29FFF 2A000–​2AFFF 2B000–​2BFFF 2C000–​2CFFF 2D000–​2DFFF 2E000–​2EFFF 2F000–​2FFFF 30000–​30FFF 31000–​31FFF 32000–​32FFF E0000–​E0FFF 15: SPUA-A F0000–​FFFFF 16: SPUA-B 100000–​10FFFF The first plane, plane 0 ,

Java Native Interface - Misplaced Pages Continue

2604-431: The full description of a television, including its attributes (structure) and buttons (interface). Getting the total number of televisions manufactured could be a static method of the television class. This method is associated with the class, yet is outside the domain of each instance of the class. A static method that finds a particular instance out of the set of all television objects is another example. The following

2666-523: The function's automatic variables . A metaclass is a class where instances are classes. A metaclass describes a common structure of a collection of classes and can implement a design pattern or describe particular kinds of classes. Metaclasses are often used to describe frameworks . Plane (Unicode)#Basic Multilingual Plane In the Unicode standard, a plane is a contiguous group of 65,536 (2 ) code points . There are 17 planes, identified by

2728-446: The global scope. There are various types of such classes. An inner class is a class defined within another class. The relationship between an inner class and its containing class can also be treated as another type of class association. An inner class is typically neither associated with instances of the enclosing class nor instantiated along with its enclosing class. Depending on the language, it may or may not be possible to refer to

2790-432: The interface. Object-oriented programming methodology dictates that the operations of any interface of a class are to be independent of each other. It results in a layered design where clients of an interface use the methods declared in the interface. An interface places no requirements for clients to invoke the operations of one interface in any particular order. This approach has the benefit that client code can assume that

2852-623: The language allows. Not all languages support multiple inheritance. For example, Java allows a class to implement multiple interfaces, but only inherit from one class. If multiple inheritance is allowed, the hierarchy is a directed acyclic graph (or DAG for short), otherwise it is a tree . The hierarchy has classes as nodes and inheritance relationships as links. Classes in the same level are more likely to be associated than classes in different levels. The levels of this hierarchy are called layers or levels of abstraction. Example (Simplified Objective-C 2.0 code, from iPhone SDK): In this example,

2914-413: The mapping of types between Java (JNI) and native code. In addition, the signature "L fully-qualified-class ;" would mean the class uniquely specified by that name; e.g., the signature "Ljava/lang/String;" refers to the class java.lang.String . Also, prefixing [ to the signature makes the array of that type; for example, [I means the int array type. Finally, a void signature uses

2976-414: The native side. Consequently, native side code (such as assembly language) assumes the responsibility for explicitly releasing any such memory resources that the native code acquires. On Linux and Solaris platforms, if the native code registers itself as a signal handler, it could intercept signals intended for the JVM. A chain of responsibility can be used to allow native code to better inter-operate with

3038-505: The numbers 0 to 16, which corresponds with the possible values 00–10 16 of the first two positions in six position hexadecimal format (U+ hh hhhh ). Plane 0 is the Basic Multilingual Plane (BMP), which contains most commonly used characters. The higher planes 1 through 16 are called "supplementary planes". The last code point in Unicode is the last code point in plane 16, U+10FFFF. As of Unicode version 16.0, five of

3100-667: The object-oriented community consider antithetical to the goals of using object classes in the first place. Understanding which class will be responsible for handling a message can get complex when dealing with more than one superclass. If used carelessly this feature can introduce some of the same system complexity and ambiguity classes were designed to avoid. Most modern object-oriented languages such as Smalltalk and Java require single inheritance at run time. For these languages, multiple inheritance may be useful for modeling but not for an implementation. However, semantic web application objects do have multiple superclasses. The volatility of

3162-473: The objects, such as error checking on get and set methods . One important question when modeling and implementing a system of object classes is whether a class can have one or more superclasses. In the real world with actual sets, it would be rare to find sets that did not intersect with more than one other set. However, while some systems such as Flavors and CLOS provide a capability for more than one parent to do so at run time introduces complexity that many in

SECTION 50

#1732776026451

3224-405: The operations of an interface are available for use whenever the client has access to the object. The buttons on the front of your television set are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to toggle the television on and off. In this example, your particular television is the instance, each method is represented by

3286-507: The other class of the association. Common multiplicities are "0..1", "1..1", "1..*" and "0..*", where the "*" specifies any number of instances. There are many categories of classes, some of which overlap. In a language that supports inheritance, an abstract class , or abstract base class ( ABC ), is a class that cannot be directly instantiated. By contrast, a concrete class is a class that can be directly instantiated. Instantiation of an abstract class can occur only indirectly, via

3348-477: The pairs in UTF-16), 66 are non-characters , and 137,468 are reserved for private use , leaving 974,530 for public assignment. Planes are further subdivided into Unicode blocks , which, unlike planes, do not have a fixed size. The 338 blocks defined in Unicode 16.0 cover 27% of the possible code point space, and range in size from a minimum of 16 code points (sixteen blocks) to a maximum of 65,536 code points (Supplementary Private Use Area-A and -B, which constitute

3410-530: The planes have assigned code points (characters), and seven are named. The limit of 17 planes is due to UTF-16 , which can encode 2 code points (16 planes) as pairs of words , plus the BMP as a single word. UTF-8 was designed with a much larger limit of 2 (2,147,483,648) code points (32,768 planes), and would still be able to encode 2 (2,097,152) code points (32 planes) even under the current limit of 4 bytes . The 17 planes can accommodate 1,114,112 code points. Of these, 2,048 are surrogates (used to make

3472-516: The platform portability Java offers (a partial workaround is to write a separate implementation of JNI code for each platform and have Java detect the operating system and load the correct one at runtime). Not only can native code interface with Java, it can also draw on a Java Canvas , which is possible with the Java AWT Native Interface . The process is almost the same, with just a few changes. The Java AWT Native Interface

3534-461: The private data of a class to compile. In the C++ language, private methods are visible, but not accessible in the interface; however, they may be made invisible by explicitly declaring fully abstract classes that represent the interfaces of the class. Some languages feature other accessibility schemes: Conceptually, a superclass is a superset of its subclasses. For example, GraphicObject could be

3596-766: The programmer to define and call these special methods. Every class implements (or realizes ) an interface by providing structure and behavior. Structure consists of data and state, and behavior consists of code that specifies how methods are implemented. There is a distinction between the definition of an interface and the implementation of that interface; however, this line is blurred in many programming languages because class declarations both define and implement an interface. Some languages, however, provide features that separate interface and implementation. For example, an abstract class can define an interface without providing an implementation. Languages that support class inheritance also allow classes to inherit interfaces from

3658-488: The redistribution of the dependent jar library. The tradeoff is between JNI being harder to code and JNA being slower. JNI is built in to core Java. Class (computer science) In object-oriented programming , a class defines the shared aspects of objects created from the class. The capabilities of a class differ between programming languages , but generally the shared aspects consist of state ( variables ) and behavior ( methods ) that are each either associated with

3720-719: The standard library classes depend on JNI to provide functionality to the developer and the user, e.g. file I/O and sound capabilities. Including performance- and platform-sensitive API implementations in the standard library allows all Java applications to access this functionality in a safe and platform-independent manner. The JNI framework lets a native method use Java objects in the same way that Java code uses these objects. A native method can create Java objects and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code. Only applications and signed applets can invoke JNI. An application that relies on JNI loses

3782-653: The type system. Encapsulation of state is necessary for being able to enforce the invariants of the class. The behavior of a class or its instances is defined using methods . Methods are subroutines with the ability to operate on objects or classes. These operations may alter the state of an object or simply provide ways of accessing it. Many kinds of methods exist, but support for them varies across languages. Some types of methods are created and called by programmer code, while other special methods—such as constructors, destructors, and conversion operators—are created and called by compiler-generated code. A language may also allow

SECTION 60

#1732776026451

3844-509: Was less clumsy to use than JNI, because no bookkeeping with a Java environment pointer was needed. Instead, all Java objects could be accessed directly. To facilitate this, a tool was used that generated header files from Java classes. Similarly, J/Direct was easier to use than using the necessary intermediate native library and JNI. Java Native Access (JNA) is a community-developed library that provides Java programers easy access to native shared libraries without using JNI. However, this requires

#450549