| <!doctype html> |
| <html class="no-js" lang="en" dir="ltr"> |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="x-ua-compatible" content="ie=edge"> |
| <title>Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux</title> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <meta name="description" content="Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux - Apache NetBeans"> |
| <meta name="author" content="Apache NetBeans"> |
| <meta name="description" content="Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux - Apache NetBeans"> |
| <meta name="keywords" content="Apache NetBeans, Tutorials, Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux"> |
| <meta name="generator" content="Apache NetBeans"> |
| <link rel="stylesheet" href="../../../../../../_/css/font-awesome.min.css"> |
| <link rel="alternate" type="application/atom+xml" title="Apache NetBeans Blog" href="https://netbeans.apache.org/blogs/atom" /> |
| <link rel="stylesheet" href="../../../../../../_/css/highlightjs/default.min.css"> |
| <link rel="stylesheet" href="../../../../../../_/css/netbeans.css"> |
| <link rel="apple-touch-icon" sizes="180x180" href="../../../../../../_/images/fav/apple-touch-icon.png"> |
| <link rel="icon" type="image/png" sizes="32x32" href="../../../../../../_/images/fav/favicon-32x32.png"> |
| <link rel="icon" type="image/png" sizes="16x16" href="../../../../../../_/images/fav/favicon-16x16.png"> |
| <link rel="manifest" href="../../../../../../_/images/fav/site.webmanifest"> |
| <link rel="mask-icon" href="../../../../../../_/images/fav/safari-pinned-tab.svg" color="#5bbad5"> |
| <meta name="msapplication-TileColor" content="#ffc40d"> |
| <meta name="theme-color" content="#ffffff"> |
| <link href="../../../../../../_/css/font-open-sans.css" rel="stylesheet"> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| --> |
| </head> |
| <body> |
| <div class="title-bar" data-responsive-toggle="responsive-menu" data-hide-for="medium"> |
| <button type="button" data-toggle="responsive-menu"><i style='font-size: 32px; color: #fff; padding: 8px' class='fa fa-bars'></i></button> |
| <div class="title-bar-title">Apache NetBeans</div> |
| </div> |
| <div class="top-bar" id="responsive-menu"> |
| <div class='top-bar-left'> |
| <a class='title' href="../../../../../../index.html"><img src='../../../../../../_/images/apache-netbeans.svg' style='padding: 8px; height: 48px;'> Apache NetBeans</a> |
| </div> |
| <div class="top-bar-right"> |
| <ul class="vertical medium-horizontal menu" data-responsive-menu="drilldown medium-dropdown"> |
| <li> <input id="search-input" type="text" placeholder="Search the docs"> </li> |
| <li> <a href="../../../../../../front/main/community">Community</a> </li> |
| <li> <a href="../../../../../../front/main/participate">Participate</a> </li> |
| <li> <a href="../../../../../../front/main/blogs">Blog</a></li> |
| <li> <a href="../../../../../../front/main/help">Get Help</a> </li> |
| <li> <a href="https://plugins.netbeans.apache.org/">Plugins</a> </li> |
| <li> <a href="../../../../../../front/main/download">Download</a> </li> |
| </ul> |
| </div> |
| </div> |
| |
| <!-- src/templates/news --> |
| <section class="hero news alternate"> |
| <div class='grid-container'> |
| <div class='cell'> |
| <div class="annotation">Latest release</div> |
| <h1>Apache NetBeans 27</h1> |
| <p><a class="button success" href="../../../../../../front/main/download/nb27">Download</a></p> |
| </div> |
| </div> |
| </section> |
| <div class='grid-container main-content tutorial'> |
| <article class="doc"> |
| <h1 class="sect0">Beginning JNI with NetBeans IDE and C/C++ Plugin on Linux</h1> |
| <div class="sectionbody"> |
| <div class="admonitionblock note"> |
| <table> |
| <tbody><tr> |
| <td class="icon"><i class="fa icon-note" title="Note"></i></td> |
| <td class="content">This tutorial needs a review. |
| You can <a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/cnd/beginning-jni-linux.adoc" title="Edit this tutorial in github">edit it in GitHub </a> |
| following these <a href="../../../../../../tutorial/main/kb/docs/contributing">contribution guidelines.</a></td> |
| </tr></tbody> |
| </table> |
| </div> |
| </div> |
| <div id="toc" class="toc"> |
| <div id="toctitle"></div> |
| <ul class="sectlevel1"> |
| <li><a href="#_requirements">Requirements</a></li> |
| <li><a href="#_setting_up_your_environment_for_the_tutorial">Setting Up Your Environment for the Tutorial</a></li> |
| <li><a href="#_setting_up_the_java_application_project">Setting Up the Java Application Project</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_editing_the_main_class_source">Editing the Main Class Source</a></li> |
| <li><a href="#_creating_the_native_library_header_file">Creating the Native Library Header File</a></li> |
| </ul> |
| </li> |
| <li><a href="#_setting_up_a_new_cc_dynamic_library_project">Setting Up a New C/C++ Dynamic Library Project</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_setting_project_properties">Setting Project Properties</a></li> |
| <li><a href="#_adding_a_header_file">Adding a Header File</a></li> |
| <li><a href="#_implementing_a_method">Implementing a Method</a></li> |
| </ul> |
| </li> |
| <li><a href="#_building_and_running_the_application">Building and Running the Application</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_configuring_the_java_project">Configuring the Java Project</a></li> |
| <li><a href="#_running_the_jnidemojava_application">Running the JNIDemoJava Application</a></li> |
| <li><a href="#_summary">Summary</a></li> |
| </ul> |
| </li> |
| <li><a href="#_next_steps">Next Steps</a></li> |
| </ul> |
| </div> |
| <div id="preamble"> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>This tutorial takes you through the creation of a simple application that uses Java<sup>TM</sup> Native Interface (JNI) code written in the C programming language.</p> |
| </div> |
| <div class="paragraph"> |
| <p>The tutorial is specific to Linux.</p> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_requirements"><a class="anchor" href="#_requirements"></a>Requirements</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p><strong>To follow this tutorial, you need the following software and resources.</strong></p> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Software or Resource</th> |
| <th class="tableblock halign-left valign-top">Version Required</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">NetBeans IDE</p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="../../../../../../front/main/download/" class="xref page">version 7.4 or 8.0 with NetBeans C/C++ plugin</a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Java Developer Kit (JDK)</p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://www.oracle.com/java/technologies/downloads/">version 7 or 8</a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">C and C++ compilers, <code>make</code> , <code>gdb</code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="../cpp-setup-instructions/" class="xref page">Configuring the NetBeans IDE for C/C++/Fortran</a></p></td> |
| </tr> |
| </tbody> |
| </table> |
| <div class="paragraph"> |
| <p>See the <a href="../../../../../../front/main/download/" class="xref page">NetBeans IDE 8.0 Installation Instructions</a> and <a href="../cpp-setup-instructions/" class="xref page">Configuring the NetBeans IDE for C/C++/Fortran</a> for information on downloading and installing the required software.</p> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_setting_up_your_environment_for_the_tutorial"><a class="anchor" href="#_setting_up_your_environment_for_the_tutorial"></a>Setting Up Your Environment for the Tutorial</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>You need both Java modules and C/C modules for this tutorial. If you already have downloaded the NetBeans IDE C/C bundle, you can download the additional Java modules separately.</p> |
| </div> |
| <div class="paragraph"> |
| <p>To determine if you have the Java and C/C modules, select File > New Project. The project categories should include both Java and C/C.</p> |
| </div> |
| <div class="paragraph"> |
| <p><strong>To download Java and C/C++ modules that may be missing:</strong></p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>In the NetBeans IDE, select Tools > Plugins.</p> |
| </li> |
| <li> |
| <p>In the Available Plugins tab, select the checkbox for Java or C/C++, depending on which is missing from your IDE. If you already have the plugins, they will be listed in the Installed tab.</p> |
| </li> |
| <li> |
| <p>Click Install.</p> |
| </li> |
| <li> |
| <p>Click Next in the NetBeans IDE Installer dialog box, accept the license terms checkbox, and click Install.</p> |
| </li> |
| <li> |
| <p>Click Finish when the installation is complete.</p> |
| </li> |
| </ol> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_setting_up_the_java_application_project"><a class="anchor" href="#_setting_up_the_java_application_project"></a>Setting Up the Java Application Project</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>This program requires a Java project and a C project. In this section, you will create and configure the Java project for the JNI application you will be developing. You will create a new Java application project, initialize its main class, and add a native method to this class.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Choose File > New Project. Select the Java category and Java Application project type. Click Next.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-new-project-java.png" alt="jni new project java"> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="2"> |
| <li> |
| <p>In the Project Name field, type <code>JNIDemoJava</code> .</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>You can change the Project Location to any directory on your computer, but here we use the default NetBeansProjects in the user directory.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="4"> |
| <li> |
| <p>Leave the Create Main Class checkbox selected and change the Main class name to <code>jnidemojava.Main</code> .</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-new-java-app.png" alt="jni new java app"> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="5"> |
| <li> |
| <p>Click Finish.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>The IDE creates the <code>NetBeansProjects/JNIDemoJava</code> project folder.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_editing_the_main_class_source"><a class="anchor" href="#_editing_the_main_class_source"></a>Editing the Main Class Source</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>To open the Main class source in the editor, right-click the <code>Main.java</code> class node and choose Open.</p> |
| </li> |
| <li> |
| <p>Replace the line <code>//TODO code application logic here</code> in the <code>main</code> method with the following:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">new Main().nativePrint();</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>Notice the indicator in the left margin showing an error and lightbulb. Click on the indicator, and you are prompted with a shortcut to create the method <code>nativePrint</code> .</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="4"> |
| <li> |
| <p>Click on this shortcut and the IDE inserts the following code:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private void nativePrint() { |
| throw new UnsupportedOperationException("Not supported yet"); |
| }</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="5"> |
| <li> |
| <p>Delete the line</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">throw new UnsupportedOperationException("Not supported yet");</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="6"> |
| <li> |
| <p>Modify the <code>nativePrint()</code> method by inserting the <code>native</code> keyword into the method signature so that it now looks as follows:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private native void nativePrint();</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>The <code>native</code> keyword indicates that the method has an implementation located in an external native library. However, at runtime the library location is not clear.</p> |
| </div> |
| <div class="paragraph"> |
| <p>The new main method should look as follows:</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public static void main(String[] args) { |
| new Main().nativePrint(); |
| } |
| |
| private native void nativePrint(); |
| }</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="7"> |
| <li> |
| <p>Right-click the project name and select Clean and Build. The project should build successfully.</p> |
| </li> |
| </ol> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_creating_the_native_library_header_file"><a class="anchor" href="#_creating_the_native_library_header_file"></a>Creating the Native Library Header File</h3> |
| <div class="paragraph"> |
| <p>In this section we use <code>javah</code> , a Java tool that creates a C header from a Java class.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>In a terminal window, navigate to the <code>NetBeansProjects</code> directory.</p> |
| </li> |
| <li> |
| <p>Type the following:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">javah -o JNIDemoJava.h -classpath JNIDemoJava/build/classes jnidemojava.Main</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>A <code>JNIDemoJava.h</code> C header file is generated in the NetBeansProjects directory. This file is required to provide a correct function declaration for the native implementation of the <code>nativePrint()</code> method. You will need it later when you create the C part of this application.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>Switch back to the NetBeans IDE window.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p><strong>Summary</strong></p> |
| </div> |
| <div class="paragraph"> |
| <p>In this exercise you created a new Java application project, specified its location, and defined the package and name of the main class of the project. You also added a new method to the main class and marked it as a method having a native implementation. As a final step, you created a C header file, which is required later for the native library compilation.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_setting_up_a_new_cc_dynamic_library_project"><a class="anchor" href="#_setting_up_a_new_cc_dynamic_library_project"></a>Setting Up a New C/C++ Dynamic Library Project</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>This section shows you how to create the native part of the application. You will create the C++ Dynamic Library project and configure it to be able to build JNI code.</p> |
| </div> |
| <div class="paragraph"> |
| <p>After you have set up the project, you will create the implementation for the native method you declared earlier in the Java part of the application.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Choose File > New Project. Under Categories, select C/C. Under Projects, select C/C Dynamic Library. Click Next.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-new-project-c.png" alt="jni new project c"> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="2"> |
| <li> |
| <p>In the Project Name field, type <code>JNIDemoCdl</code> .</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>In the Project Location field, use the same location that you used for the Java application project, <code>NetBeansProjects</code> . The location should be shown as the default value.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="4"> |
| <li> |
| <p>Accept the defaults for all other fields and click Finish.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>The IDE creates the <code>NetBeansProjects/JNIDemoCdl</code> project folder.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_setting_project_properties"><a class="anchor" href="#_setting_project_properties"></a>Setting Project Properties</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Right-click the JNIDemoCdl project node and choose Properties.</p> |
| </li> |
| <li> |
| <p>In the Properties dialog box, select the C Compiler node under the Build properties.</p> |
| </li> |
| <li> |
| <p>Click the Include Directories and Headers …​ button and click Add in the Include Directories and Headers dialog box.</p> |
| </li> |
| <li> |
| <p>Browse into your JDK directory, and select the <code>include</code> subdirectory.</p> |
| </li> |
| <li> |
| <p>Select the Store path as Absolute option, then click Select to add this directory to the project’s Include Directories.</p> |
| </li> |
| <li> |
| <p>Add the JDK’s <code>include/linux</code> directory in the same way, then click OK.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-include-directories.png" alt="jni include directories"> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>These settings are required to enable references to the Java <code>jni.h</code> library from your C code.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="7"> |
| <li> |
| <p>Find the Compilation Line area of the C Compiler options. Click in the text field of the Additional Options property and type <code>-shared -m32</code> . <span class="image"><img src="../../../../_images/kb/docs/cnd/jni-project-properties-cmd-options.png" alt="jni project properties cmd options"></span></p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>The <code>-shared</code> option tells the compiler to generate a dynamic library. |
| The <code>-m32</code> option tells the compiler to create a 32-bit binary. By default on 64-bit systems the compiled binaries are 64-bit, which causes a lot of problems with 32-bit JDKs.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="8"> |
| <li> |
| <p>Click the Linker category in the left panel.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="9"> |
| <li> |
| <p>Click the Output text field, and replace the string</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libJNIDemoCdl.so</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>with the string</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">dist/libJNIDemoCdl.so</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>to simplify the path of the resulting shared object file. This will make the file easer to reference from Java.</p> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-project-properties-linker.png" alt="jni project properties linker"> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="10"> |
| <li> |
| <p>Click OK. The defined settings are saved.</p> |
| </li> |
| </ol> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_adding_a_header_file"><a class="anchor" href="#_adding_a_header_file"></a>Adding a Header File</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Go to a terminal window and move the <code>JNIDemoJava.h</code> header file that you generated previously from your <code>NetBeansProjects</code> directory to the C/C++ Library project directory, <code>NetBeansProjects/JNIDemoCdl</code> . |
| 2. |
| In the Projects window, right-click the Header Files node of the <code>JNIDemoCdl</code> project and choose Add Existing Item. Navigate to the <code>NetBeansProjects/JNIDemoCdl</code> directory and select the <code>JNIDemoJava.h</code> file, then click Select.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>The <code>JNIDemoJava.h</code> file appears under Header Files.</p> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-source-files-include-file.png" alt="jni source files include file"> |
| </div> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_implementing_a_method"><a class="anchor" href="#_implementing_a_method"></a>Implementing a Method</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Right-click the Source Files node of the <code>JNIDemoCdl</code> project and choose New > C Source File. Type <code>JNIDemo</code> in the File Name field, and click Finish. The editor opens the <code>JNIDemo.c</code> file.</p> |
| </li> |
| <li> |
| <p>Edit the <code>JNIDemo.c</code> file by typing the following code:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-c hljs" data-lang="c">#include <jni.h> |
| #include <stdio.h> |
| #include "JNIDemoJava.h" |
| |
| JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint |
| (JNIEnv *env, jobject obj) |
| { |
| |
| printf("\nHello World from C\n"); |
| |
| }</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>Save the <code>JNIDemo.c</code> file.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="4"> |
| <li> |
| <p>Right-click the <code>JNIDemoCdl</code> project node and choose Build. The Output window displays <code>BUILD SUCCESSFUL (total time 171ms)</code> or similar.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p><strong>Summary</strong></p> |
| </div> |
| <div class="paragraph"> |
| <p>In this exercise you created a new C/C++ Dynamic Library, specified its location, and configured it to be able to build a JNI implementation of your Java method. You added the generated header file for the native method you declared in the Java application, and implemented it.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_building_and_running_the_application"><a class="anchor" href="#_building_and_running_the_application"></a>Building and Running the Application</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>In this exercise, you will perform some final alterations to the Java part of the application. These changes are required to ensure the Java part properly loads the native library you compiled in the previous exercise. After that you will compile and run the resulting application.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_configuring_the_java_project"><a class="anchor" href="#_configuring_the_java_project"></a>Configuring the Java Project</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Open the <code>Main.java</code> file in the editor.</p> |
| </li> |
| <li> |
| <p>Add the following initialization code for the C++ dynamic library after the <code>public class Main</code> line, using the path to the output file that you shortened in the previous exercise:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">static { |
| System.load("_full-path-to-NetBeansProjects-dir_/JNIDemoCdl/dist/libJNIDemoCdl.so"); |
| }</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>Replace <em>full-path-to-NetBeansProjects-dir</em> with the path to your NetBeansProjects directory, which should be something similar to <code>/home/<em>username</em>/NetBeansProjects</code></p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic" start="3"> |
| <li> |
| <p>Save the <code>Main.java</code> file.</p> |
| </li> |
| </ol> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_running_the_jnidemojava_application"><a class="anchor" href="#_running_the_jnidemojava_application"></a>Running the JNIDemoJava Application</h3> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Select the JNIDemoJava application in the Projects window.</p> |
| </li> |
| <li> |
| <p>Press F6 or click the Run button in the toolbar to run the application. The program should execute correctly and the Output window should display output similar to the following:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/cnd/jni-build-success.png" alt="jni build success"> |
| </div> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_summary"><a class="anchor" href="#_summary"></a>Summary</h3> |
| <div class="paragraph"> |
| <p>In this exercise you made some final configuration steps and ran the application to verify that the implementation of the native method comes from the native C library.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_next_steps"><a class="anchor" href="#_next_steps"></a>Next Steps</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>If you want to check your work against a working example, you can <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FCPlusPlus%252FJNIDemo.zip">download a zip file containing the source code</a> from netbeans.org.</p> |
| </div> |
| <div class="paragraph"> |
| <p>You can use the following documents to get more information:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="../quickstart/" class="xref page">C/C++ Projects Quick Start Tutorial</a></p> |
| </li> |
| <li> |
| <p><a href="https://docs.oracle.com/en/java/javase/11/docs/specs/jni/index.html">JNI Specification</a></p> |
| </li> |
| <li> |
| <p><a href="https://en.wikipedia.org/wiki/Java_Native_Interface">Java Native Interface</a></p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p><a href="../../../../../../front/main/community/mailing-lists/" class="xref page">Send Feedback on This Tutorial</a></p> |
| </div> |
| </div> |
| </div> |
| <section class='tools'> |
| <ul class="menu align-center"> |
| <li><a title="Facebook" href="https://www.facebook.com/NetBeans"><i class="fa fa-md fa-facebook"></i></a></li> |
| <li><a title="Twitter" href="https://twitter.com/netbeans"><i class="fa fa-md fa-twitter"></i></a></li> |
| <li><a title="Github" href="https://github.com/apache/netbeans"><i class="fa fa-md fa-github"></i></a></li> |
| <li><a title="YouTube" href="https://www.youtube.com/user/netbeansvideos"><i class="fa fa-md fa-youtube"></i></a></li> |
| <li><a title="Atom Feed" href="https://netbeans.apache.org/blogs/atom"><i class="fa fa-mf fa-rss"></i></a></li> |
| <li><a title="Slack" href="https://tinyurl.com/netbeans-slack-signup/"><i class="fa fa-md fa-slack"></i></a></li> |
| <li><a title="Issues" href="https://github.com/apache/netbeans/issues"><i class="fa fa-mf fa-bug"></i></a></li> |
| </ul> |
| <ul class="menu align-center"> |
| <li><a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/cnd/beginning-jni-linux.adoc" title="See this page in github"><i class="fa fa-md fa-edit"></i> See this page in GitHub.</a></li> |
| </ul> |
| </section> |
| </article> |
| </div> |
| <div class='grid-container incubator-area' style='margin-top: 64px'> |
| <div class='grid-x grid-padding-x'> |
| <div class='large-auto cell text-center'> |
| <a href="https://www.apache.org/"> |
| <img style="height: 60px" title="Apache Software Foundation" src="../../../../../../_/images/asf_logo_wide.svg" /> |
| </a> |
| </div> |
| <div class='large-auto cell text-center'> |
| <a href="https://www.apache.org/events/current-event.html"> |
| <img style="width:234px; height: 60px;" title="Apache Software Foundation current event" src="https://www.apache.org/events/current-event-234x60.png"/> |
| </a> |
| </div> |
| </div> |
| </div> |
| <footer> |
| <div class="grid-container"> |
| <div class="grid-x grid-padding-x"> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/about">About</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/community/who">Who's Who</a></li> |
| <li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="https://www.apache.org/security/">Security</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/community">Community</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/community/mailing-lists">Mailing lists</a></li> |
| <li><a href="../../../../../../front/main/community/committer">Becoming a committer</a></li> |
| <li><a href="../../../../../../front/main/community/events">NetBeans Events</a></li> |
| <li><a href="https://www.apache.org/events/current-event.html">Apache Events</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/participate">Participate</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/participate/submit-pr">Submitting Pull Requests</a></li> |
| <li><a href="../../../../../../front/main/participate/report-issue">Reporting Issues</a></li> |
| <li><a href="../../../../../../front/main/participate/#documentation">Improving the documentation</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/help">Get Help</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/help/#documentation">Documentation</a></li> |
| <li><a href="../../../../../../wiki/main/wiki">Wiki</a></li> |
| <li><a href="../../../../../../front/main/help/#support">Community Support</a></li> |
| <li><a href="../../../../../../front/main/help/commercial-support">Commercial Support</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/download">Download</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/download">Releases</a></li> |
| <li><a href="https://plugins.netbeans.apache.org/">Plugins</a></li> |
| <li><a href="../../../../../../front/main/download/#_daily_builds_and_building_from_source">Building from source</a></li> |
| <li><a href="../../../../../../front/main/download/#_older_releases">Previous releases</a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </footer> |
| <div class='footer-disclaimer'> |
| <div class="footer-disclaimer-content"> |
| <p>Copyright © 2017-2025 <a href="https://www.apache.org">The Apache Software Foundation</a>.</p> |
| <p>Licensed under the Apache <a href="https://www.apache.org/licenses/">license</a>, version 2.0</p> |
| <div style='max-width: 40em; margin: 0 auto'> |
| <p>Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache NetBeans logo are trademarks of <a href="https://www.apache.org">The Apache Software Foundation</a>.</p> |
| <p>Oracle and Java are registered trademarks of Oracle and/or its affiliates.</p> |
| <p>The Apache NetBeans website conforms to the <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Apache Software Foundation Privacy Policy</a></p> |
| </div> |
| </div> |
| </div> |
| |
| |
| <script src="../../../../../../_/js/vendor/lunr.js"></script> |
| <script src="../../../../../../_/js/search-ui.js" id="search-ui-script" data-site-root-path="../../../../../.." data-snippet-length="100" data-stylesheet="../../../../../../_/css/search.css"></script> |
| <script async src="../../../../../../search-index.js"></script> |
| <script src="../../../../../../_/js/vendor/jquery.min.js"></script> |
| <script src="../../../../../../_/js/vendor/what-input.min.js"></script> |
| <script src="../../../../../../_/js/vendor/foundation.min.js"></script> |
| <script src="../../../../../../_/js/vendor/jquery.colorbox-min.js"></script> |
| <script src="../../../../../../_/js/netbeans.js"></script> |
| <script> |
| $(function(){ $(document).foundation(); }); |
| </script> |
| <script src="../../../../../../_/js/vendor/highlight.min.js"></script> |
| <script> |
| document.addEventListener('DOMContentLoaded', (event) => { |
| document.querySelectorAll('pre code').forEach((el) => { |
| hljs.highlightElement(el); |
| }); |
| }); |
| </script> |
| </body> |
| </html> |