GithubHelp home page GithubHelp logo

jnic's Introduction


JniC - The Java Native Interface Compiler Tool

NOTE: JniC is now deprecated. Use the other script by me: NativeInsert.

A Java Native Method Tool

Never heard of a native method in Java? A native method is a method that is implemented later in the compilation process. However, if this just seems strange, I mean, why would you want a method that you have to implement later? But it is much better than that. A native method's source code, is written in the C/C++ programming language!

If you don't know how to program in at least C, then you might want to learn that before you do anything with this.

You can do lot's of things that you wouldn't normally be able to do in java, with a native method. But doing this takes time. JniC is an interactive command line tool that lets you create your programs in as little as 15 seconds, not including the time it takes to write and compile the C source code.

Download it now for free on Linux, DOS, or Darwin (Mac OS X)! Or you can download the most recent official release here!

Table of Contents

Installation

Click one of the links below to download the most updated stable version of JniC for your OS

OS Latest Version Download Link
Linux 1.0.0 jnic-linux-1.0.0.sh
DOS 1.0.0 jnic-dos-1.0.0.sh
Darwin (Mac OS X) 1.0.0 jnic-darwin-1.0.0.sh

Quick Start

This section assumes you are ready to go, and the software for your specific OS is currently running in the terminal. (Use source my-jnic-file.sh to load) All you need now is to have a basic understanding of how the software works.

How to Use

In this section you will learn how to create a native method in java, call it, and have the source code in C run.

To start, launch open your favorite editor to work with java. We'd prefer this test project to be in a main folder by itself. Write the following program:

File: Program.java

public class Program {
  
  public static native void printText(); // The native method we call
  static { // This block will be called as the class is being loaded into memory.
    System.loadLibrary("program"); // Load the soon to be created libprogram.so, or program.dll on DOS.
  }
  
  public static void main(String[] args) {
    printText(); // Executing the C code.
  }
  
}

Alright! Your're done with the Java source code. Now to write the source code for the native method "printText()".

Open your command line and navigate to the directory with your source code in it. Enter jnic Program, and you should see this:

$ jnic Program
Input: Program
What would you like the name of this library to be? Don't include the lib prefix or .so suffix.

Here, we will enter the name that we had in the loadLibrary method, "program".

program
Ok. The resulting file name will be libprogram.so and your static block should contain `System.loadLibrary("program");'.
Starting initialization progress...
Compiling code...
Code compilation successful.
Generating header files...
Header code generation successful.
Generating implementation C++ files...
C++ implementation file generation successful.
Launching VI Improved editor for each file. Write and quit using :wq in normal mode (Get to normal mode by using ESC), and the writing will continue to the last file.

Now, if you have followed through so far, you are probably in the Vim text editor. If you are new to it, open a shell and type vimtutor to learn.

Add this to the bottom of the file that pops up:

JNIEXPORT void JNICALL Java_Program_printText(JNIEnv *env, jclass self) { // Naming convention: Java_<package_and_class_name>_<method_name>
  printf("Hello, world!\n"); // Comes from <stdio.h> header
  return;
}

Save and Quit, and the program will finish.

Editing complete.
Compiling C++ code...
C++ compilation successful!
Be careful. The LD_LIBRARY_PATH environment variable is now loaded. If you wish to add on, just enter the following:
export LD_LIBRARY_PATH="yourdir:$LD_LIBRARY_PATH"
Ok! You're done! Just enter `java <MyClass>', and you will run the native code along side your java code!
$ 

As the last line says, all we have to do is call java Program. So let's do just that!

$ java Program
Hello, world!
$

Yay! It works! You just wrote your first Java program using native methods!

Frequently Asked Questions

We know you probably have many questions by now, and we would love to answer them, but to avoid repeat questions, here are some of our most commonly asked questions.

Q: Can you create these libraries with multiple packages in mind?

A: Yes! Yes you can. All you have to do is run the command at the root of all packages. Not a directory lower. Thanks for asking!

jnic's People

Contributors

codedojoofficial avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lvyitian

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.