Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, April 16, 2009

JNI Example

How to use JNI ?? Click here to find out.

NativeAdd.java
public class NativeAdd{
native int addNums(int a, int b);
static{
System.loadLibrary("Add");
}
public static void main(String args[]){
NativeAdd na = new NativeAdd();
System.out.println(""+na.addNums(1,2));
}
}

NativeAdd.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class NativeAdd */

#ifndef _Included_NativeAdd
#define _Included_NativeAdd
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: NativeAdd
* Method: addNums
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_NativeAdd_addNums
(JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

NativeAdd.c
#include "jni.h"
#include "NativeAdd.h"
JNIEXPORT jint JNICALL Java_NativeAdd_addNums
(JNIEnv * jenv, jobject jobj, jint a, jint b){
int sum = 0;
sum = a + b;
return sum;
}

How to use JNI (Java Native Interface) in Linux

Java Native Interface allows you to call functions written in different programming languages from java code. For eg C, C++.
Steps involved:
  1. Create your java class & declare the method to be called using JNI with the native keyword.
  2. Compile the java program (javac FileName.java)
  3. Create the C or C++ header (javah -jni FileName) , FileName.h is generated by the machine.
  4. Write your C or C++ program and include the above generated header file.
  5. Create the shared object file (.so) (gcc -path_where_jdk_is_located/jdk1.6/include -path_where_jdk_is_located/jdk1.6/include/linux FileName.c -shared -o filename.so)
Note : Be sure to specify the same "filename.so" in the java program as well...

How to create effective GUI in JAVA

Steps involved in creating a effective GUI (Graphical User Interfaces) in Java

1. Design the GUI on paper.

2. Get feedback from users.

3. Decide if the class that will display the GUI extends a Container, or has references to a Container.

( This choice will effect to some extend the flexibility of the class. If the purpose of a class is to be a GUI then extend from a Container.)

4. Create references to all components used in the GUI as attributes of the class.

5. Implement a contructor that creates instances of all the components used.

6. Select a layout manager fo each of the containers.

7. Assemble the components in a window or a frame.

8. Set the size of the container.

9. Make the container visible.

How to create a simple applet

Steps in creating an Applet
(An applet is a java program that runs in the web browser)

1. The java.applet package must be included.

2. The class must extend from the Applet class.

3. Writing and compiling the Java code.

4. Writing a HTML code that will run the applet.

A Simple Applet

import java.applet.*;
public class MyApplet extends Applet{}
HTML Code

<html>

<body>
<title>Applet Test Page</title>
<h1>Applet Test Page</h1>
<applet code="MyApplet.class" width=250 height=250 name="MyApplet">
</applet>

</body>
</html>

Life cycle of an applet

Invoking a .exe from Java code

Invoking a .exe file from Java

pass the .exe file path as a command line argument

import java.io.*;
public class InvokeExe
{
public static void main(String[] args) throws IOException
{
if (args.length <= 0)
{
System.err.println("Need command to run");
System.exit(-1);
}
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args);
System.exit(0);
}

}