
javah Commnad in Linux
In classic Linux operating systems, a command line utility named javah is used to generate C header and source files. These files and headers are required to implement native methods. These files allow C programs to access an object's instance variables from the native source code.
Table of Contents
Here is a comprehensive guide to the options available with the javah command −
- What is javah?
- Syntax of javah Command
- Options Javah Command
- How to Check if javah is Installed or Not?
- Examples of javah Command in Linux
What is javah?
javah stands for Java Header. As the name itself suggests, this command is used to generate C header and source files from Java class files. The generated file has an extension ".h". This file contains a struct definition that matches the structure of the related class. The fields in the struct are the same as the instance variables in the class.
Syntax of javah Command
To use this command in Linux, type javah followed by the class name for which you want to generate a header −
javah [options] className
Options Javah Command
The javah command can accept the following optional flags to manage the output format −
Option | Description |
---|---|
-d <directory> | It specifies the directory where the generated header files will be saved. If not specified, the headers will be generated in the current working directory. |
-o <filename> | It is used to specify the output files name. However, if you do not specify the file name, javah creates a file <classname>.h, by default. |
-classpath <path> | It specifies the class path where Java should look for user-defined classes and packages. You can specify multiple directories using a colon separated syntax. |
-sourcepath <path> | It specifies the source path for the class files, which enables javah to find the corresponding .java files. |
-jni | It creates Java Native Interface (JNI) function declarations in the header file. |
-version | It returns the javah version installed on your system. |
-help | It returns help page for the javah command that includes |
For better understanding, you can access the man page of the javah command, as follows −
man javah

You can also use the javah command with the -help option to achieve the similar functionality −
javah -help
This command returns the basic usage and options description −

How to Check if javah is Installed or Not?
To check the existence of javah on your system, run the following command −
javah -version
The output shows that we are using "1.8.0_422" version of the javah command −

You can use the following command to install javah in Linux, in case it is not installed on your system −
#for Ubuntu or Debian sudo apt install default-jdk #for RHEL/CentOS/Fedora sudo yum install java-11-openjdk #For Arch Linux sudo pacman -S jdk-openjdk
Examples javah Command in Linux?
Lets go through the following examples to learn how the javah command works in Linux −
- Creating a Header File in the Current Directory
- Creating a Header File in a Specific Directory
- Redirecting Output to a Specific File
Creating a Header File in the Current Directory
We have already created a Java file named javaExample.java, lets compile it using the following command −
javac javaExample.java
Now run the javah command to generate a header file −
javah javaExample
This command generates a header file the in the current working directory as shown below −

We use the ls command to verify the creation of the header file in the current directory.
Creating a Header File in a Specific Directory
You can create a header file in a specific directory. For this purpose, you must use the -d option followed by the directory name, as shown below −
javah -d Documents javaExample
After running this command, we use the cd command to access the Documents directory and then the ls command to confirm the header file generation −

Redirecting Output to a Specific File
The -o option in javah lets you choose the name of the output file. It redirected the generated header to that file instead of following the default naming pattern −
javah -o javaOutput.h javaExample
This command generates a header file named javaOutput.h for the Java class javaExample −

This sums up the working of the javah command in Linux.
Conclusion
The javah command is a valuable command line tool in classic Linux systems that generates C header and source files from Java class files. This utility is essential for implementing native methods, allowing C programs to access Java object's instance variables.
In this tutorial, we explored the syntax, options, and practical usage of the javah command through examples. We demonstrated how to generate header files in the current directory, in specific directories, and how to redirect output to a specified file.