Slide 17. 1 Advanced Programming 2004, based on LY Stefanus's slides Native Methods.

Slides:



Advertisements
Similar presentations
Linking & Loading CS-502 Operating Systems
Advertisements

Chapter FourModern Programming Languages1 Language Systems.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Software. What Is Software? software –Also called Computer programs –Are a list of instructions –Instructions are called code –CPU performs the instructions.
Java Native Interface Modified from CS587x Lecture Department of Computer Science Iowa State University.
Object Orientated Programming
IC211 Object Oriented Programming Overview of Java.
C Module System C and Data Structures Baojian Hua
I Dream of JNI When it absolutely, positively has to be written in COBOL... shudder.
C Module System C and Data Structures Baojian Hua
Administrivia No class next Monday (May 2) Enjoy extra time on P3. ;-) Reading 3 available “Reflections on Trusting Trust”, Ken Thompson No written summary.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Communication in Distributed Systems –Part 2
Guide To UNIX Using Linux Third Edition
Introduction to Java.
COP4020 Programming Languages
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
public static void main (String[] args)
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
JAVA v.s. C++ Programming Language Comparison By LI LU SAMMY CHU By LI LU SAMMY CHU.
A First Program Using C#
Language Systems Chapter FourModern Programming Languages 1.
Programming Languages and Paradigms Object-Oriented Programming.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
Java Native Interface CS587x Lecture Department of Computer Science Iowa State University.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
What is Android NDK ● A toolset that lets you embed in you app native source code ● C, C++(recently supported December 2010) and assembly(?) ● It is supported.
chap13 Chapter 13 Programming in the Large.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Rutgers University Excellence Campaign 2/20/2004 Java Native Interface Tutorial Xiaolin Li Rutgers.
Java Introduction to JNI Prepared by Humaira Siddiqui.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
Programming With C.
POS 406 Java Technology And Beginning Java Code
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
Replay Compilation: Improving Debuggability of a Just-in Time Complier Presenter: Jun Tao.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/
LANGUAGE SYSTEMS Chapter Four Modern Programming Languages 1.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Java to C++: What would be needed ? Norman Graf (SLAC) ILC-CLIC Software, CERN May 28, 2009.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 Sections Java Virtual Machine and Byte Code Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lesson 2: First Java Programs. 2.1 Why Java? Java is one of the most popular programming languages in the world. Java is a modern object-oriented programming.
Learning Plan 6 Java Programming Intro to Object Oriented Programming.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Programs – Preprocessing, Compilation and Linking
Object Oriented Programming in
Chapter 5: Enhancing Classes
Before You Begin Nahla Abuel-ola /WIT.
Java Programming Language
Linking & Loading.
CS-3013 Operating Systems C-term 2008
Lecture 1 Runtime environments.
Programming Fundamentals Lecture #3 Overview of Computer Programming
Linking & Loading CS-502 Operating Systems
C Programming Language
Lecture 1 Runtime environments.
Linking & Loading CS-502 Operating Systems
Compiler vs linker The compiler translates one .c file into a .o file
SPL – PS1 Introduction to C++.
Presentation transcript:

Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods

Slide Advanced Programming 2004, based on LY Stefanus's slides Why Use Native Methods? While a pure Java solution is nice in principle for an application, there are situations where we want to write (or use) code written in another programming language. Such code is usually called native code. There are three reasons why this may be the right choice: 1. We have substantial amounts of tested and debugged code available in that programming language. Porting the code to Java would be time consuming, and the resulting code would need to be tested and debugged again.

Slide Advanced Programming 2004, based on LY Stefanus's slides Why Use Native Methods? 2. Our application requires access to system features or devices, and using Java technology would be cumbersome or impossible. 3. Maximizing the speed of the code is essential. For example, the task may be time-critical, or it may be code that is used so often that optimizing it has a big payoff. This third reason is not very plausible, since with just-in-time (JIT) compilation, intensive computa- tions coded in Java are not that much slower than compiled C code.

Slide Advanced Programming 2004, based on LY Stefanus's slides Why Use Native Methods? If we are in one of these three situations, it might make sense to call the native code from Java programs. Note that if we use native methods, we lose portability. Native libraries are not as safe as Jave code, especially if they are written in a language like C or C++ that offers no protection against overwriting memory through invalid pointer usage. Don't use native code if you don't really need it. Use native code only as a last resort.

Slide Advanced Programming 2004, based on LY Stefanus's slides Java Native Interface (JNI) To make calling native methods possible, Java technology comes with libraries and software tools to relieve some (but not all) of the programming tasks. We use C as our language for native methods because C is the language most often used for native codes. We will learn the so-called Java Native Interface (JNI) binding.

Slide Advanced Programming 2004, based on LY Stefanus's slides Calling a C Function from Java Programs We have to give the run time system enough information so that it can link a native method call to the right implementation of the method. This requires a three-step process: 1. Generate a C stub for a function that translates between the Java method call and the actual C function. The stub does this translation by taking parameter information off the JVM stack and passing it to the compiled C function. 2. Create a special shared library and export the stub from it. 3. Use a special method, such as System.load, to tell the Java runtime environment to load the library from Step 2.

Slide Advanced Programming 2004, based on LY Stefanus's slides Calling a C Function from Java Programs First we need to declare the native method in a class. The native keyword tells the compiler that the method is defined externally. The method header is followed immediately by a terminating semicolon. This means that native method declarations look similar to abstract method declarations. Native methods can be static or nonstatic.

Slide Advanced Programming 2004, based on LY Stefanus's slides HelloNative.java class HelloNative { public static native void greeting(); static { System.load( "/tmp/advprog2004/native/libHelloNative.so"); } Compile this program to obtain HelloNative.class with javac.

Slide Advanced Programming 2004, based on LY Stefanus's slides Next, we write a corresponding C function. This function must be named exactly the way the Java runtime environment expects. The javah utility can be used to generate the function name automatically. The execution of the command: javah HelloNative produces a C header file, HelloNative.h.

Slide Advanced Programming 2004, based on LY Stefanus's slides /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class HelloNative */ #ifndef _Included_HelloNative #define _Included_HelloNative #ifdef __cplusplus extern "C" { #endif /* * Class: HelloNative * Method: greeting * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloNative_greeting (JNIEnv *, jclass); #ifdef __cplusplus } #endif HelloNative.h

Slide Advanced Programming 2004, based on LY Stefanus's slides #include "HelloNative.h" #include JNIEXPORT void JNICALL Java_HelloNative_greeting (JNIEnv *env, jclass cl) { printf("Hello native world!\n"); } HelloNative.c

Slide Advanced Programming 2004, based on LY Stefanus's slides Next, the C code is compiled into a dynamically loaded library: gcc -c -fPIC -I/usr/local/j2sdk1.4.2/include -I/usr/local/j2sdk1.4.2/include/linux HelloNative.c gcc -shared -o libHelloNative.so HelloNative.o Now, the application HelloNativeTest can be run.

Slide Advanced Programming 2004, based on LY Stefanus's slides HelloNativeTest.java class HelloNativeTest { public static void main( String[] args ) { HelloNative.greeting(); }

Slide Advanced Programming 2004, based on LY Stefanus's slides Example using the function printf() Printf1.java Printf1Test.java Printf1.c Printf1.h