Janardhan's insights
Home PageEmotional Intelligence book
  • About Insights
  • Janardhanpulivarthi.com
  • Books I've read
  • Bridge Engineering
    • code and books
    • Pier calculations
    • Suspension cables
    • Seismic coefficients
    • Shear check in pilecap
    • finance tasks
    • /bin/sh: 1: flex: not found
    • Biology animations
  • 🏕️SEASON 1
    • Social formulation
    • Words in Computer Science
    • Best use of internet
    • Products for sustainability
    • Tools for parents in digital age
    • Identify books in library
    • Control digital footprint
    • Search like a Pro
    • Dev productivity tips & tools
    • McKinsey, How it operates
    • Secrets, which ones to share
    • How to be Straightforward
    • Productive procrastination
    • Everyday things
    • Future of waste
    • Emotional weather
    • Money rules
    • Qualities of an Artist
    • Shameful Inconsistency
    • Mean Sea Level
    • Screenshot on Windows
    • Edu loan EMI calculation
    • Advanced Gmail tips
    • 24 hours before an exam
    • FAQ - git
    • Life skills by 25
    • Install Microsoft IIS server
    • gcloud commands hang
    • Google Cloud Data Engineering
    • Google Cloud Architect
    • find location of the command
    • JNI short tutorial
    • Civil distance
    • Being a friend to children
    • How to talk to a teenager
    • Resilience and relationship
    • 9 to 5 vs 4 hour workweek
    • Return on Investment
    • Alternative to PFA
    • Hand position for Ctrl key
    • Education: The Good Parts
    • Education: The proposed parts
    • On feeling stuck
    • How to read Financial news
    • What is in my hands
    • Mistakes founders do
    • On Self Worth
    • Emotional draining situations
    • Time
    • Hygiene
    • 5 Percent rule
    • How not to build a startup
    • How to lead a mediocre life
    • Day-to-day tips from extraordinary people
    • Who owns .com domain
    • Do not make bed first thing in morning
    • How to read kubernetes docs
    • Do not use morning alarm
  • ☀️Season 2
    • Practicing self care
    • Payment methods
    • Things to learn before MBA
    • Hum toh udd gaye
    • Time windows
    • How to quit a job emotionally
    • Journal on a year after quitting
    • Engineering the future
    • Build linux kernel
    • Important inventions
    • bashrc and bash_profile
    • $TERM
    • How Kubernetes pod get terminated
    • What data does google collect? when you search...
    • Should I build a personal brand?
    • The better startup ideas
    • Regex in Visual Studio Code
    • Few pieces to read before breakfast
    • core file
    • How to keep home for ambience
    • 🐬A story on repetition
    • Basic Unicode characters
    • Java operator precedence
    • Install latest maven on Ubuntu
    • Build ecosystems, not only products
  • ⛈️SEASON 3
  • ❄️Season 4
    • Be original
    • Change and Contingency
    • Read and write email
    • How to use Pomodoro
    • Humans and software
    • Saving money without money
    • Five skills for Civil Engineers
    • Que sera sera
    • Keep scrolling
    • Being unemployed
    • Friendship and modularity
    • Happy is not a default emotion
    • The missing piece in the online courses
    • How to build products
    • I am always tired
    • Instead company subscribe to user
    • Car electric or diesel ask google ngram viewer
    • Emotional currency and binge watching
    • ADR
  • Known Nokia 7.1 problems
  • Coursera-dl HTTPError: 400
  • Failed to retrieve identities from agent
  • Custom domain email
  • Qwiklabs tips and tricks
  • Online shopping rules
  • Protect a apt package from upgrade
  • Kubernetes troubleshooting guide
  • TOOLS
    • Lenovo Ideapad
    • Quantum - Holevo's theorem
    • Install VPP on Ubuntu
  • Phone camera slider
  • Physical internet infrastructure
  • 100 Days Of Code - Learning Java
  • Blogs I found interesting
  • 5g
    • 5G Glossary
    • Archive
      • Latex and gitbook
      • Japan Progress
      • Online buying guide
      • ఏవిధంగా కృషి చేయాలి?
      • Tech prediction 2030
      • Alphabet Financials
      • Apachecoin does not exist
      • non profits
      • What to Google Search
      • Resume tips
      • Discussion before marriage
      • Wi-Fi
      • How to read
      • Basic soil test
Powered by GitBook
On this page
  • Troubleshooting
  • References

Was this helpful?

Edit on GitHub
  1. SEASON 1

JNI short tutorial

Create a Java class to use code in C

1 Write a Java class that uses C codes

CraftJNI.java
public class CraftJNI {
  static {
    System.loadLibrary("hello"); // Load native library hello.dll (windows)
                                 // or libhello.so (Unixes) at runtime.
                                 // This library contains a native method sayHello()
                                 
  }
  
  // Declare an instance native method sayHello() which receives no parameter
  // returns void
  private native void sayHello();
  
  // Test Driver
  public static void main(String[] args) {
    new CraftJNI().sayHello(); // Create an instance and invoke native method
  }
}

2 Compile Java program CraftJNI.java

javac -h . CraftJNI.java
CraftJNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class CraftJNI */

#ifndef _Included_CraftJNI
#define _Included_CraftJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     CraftJNI
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_CraftJNI_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

To know more about the generated class file

javap -verbose CraftJNI.class

3 Implement C program

CraftJNI.c
#include <jni.h>   // Standard JDK provided header
#include <stdio.h> // Standard C IO header
#include <CraftJNI.h> // Generated header

JNIEXPORT void JNICALL Java_CraftJNI_sayHello(JNIEnv *env, jobject thisObj) {
  printf("I am in C code \n");
  return;
}

4 Compile C program

gcc -fPIC -I"." -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" \
   -shared -o libhello.so CraftJNI.c
> java -Djava.library.path=. CraftJNI
I am in C code

Troubleshooting

Error 1: class file has wrong version

> javah CraftJNI.class
Error: cannot access CraftJNI
  bad class file: ./CraftJNI.class
    class file has wrong version 55.0, should be 52.0

Reason: javah is not available with Java 11

> java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-post-Debian-1deb10u2)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-post-Debian-1deb10u2, mixed mode, sharing)

> javac -version
javac 11.0.9.1

> javah -version
javah version "1.8.0_252"

> whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

> ls /usr/share/man/man1 | grep java*
java.1.gz
javac.1.gz
javadoc.1.gz
javah.1.gz
javap.1.gz

Error 2:

> java -Djava.library.path=. CraftJNI
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path: [.]
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2670)
        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
        at java.base/java.lang.System.loadLibrary(System.java:1873)
        at CraftJNI.<clinit>(CraftJNI.java:4)

References

Previousfind location of the commandNextCivil distance

Last updated 4 years ago

Was this helpful?

JNI reference docs -

Some of the code is shamelessly copied from

🏕️
https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html