devxlogo

Generate a Native Header File and Copy It to the Desired Directory

Generate a Native Header File and Copy It to the Desired Directory

If you have used a native method through a Java interface, then you might already know that a header file is generated from the Java class that contains the native method declaration. This header file is then included in the C or C++ file, where you implement the native method, declared in the Java file. The header file is generated using the “javah” utility, which comes with the Java Standard Edition. Mostly, the Java source file is a part of the package. To make the native call work properly, the header file name must be derived from the package name and class name. The following command does it in one step:

 javah -d [directory where you want the header file] classname

For example, if you declare a native method in the Java file “SharedMemory.java” as shown below, and you want the header file to be in the same directory where the Java source files are.

 	package com.mycompany.sysutil;	public class SharedMemory	{		//create the shared and intialize with the value		public native int createShm(int shm_key, String value);	}

Then the command to generate a header file will be:

 javah -d com/mycompany/sysutil/. com.mycompany.sysutil.SharedMemory

devx-admin

Share the Post: