\label{doc_tutorials_java-plugins_md_md_doc_tutorials_java_plugins}%
\Hypertarget{doc_tutorials_java-plugins_md_md_doc_tutorials_java_plugins}%
 This file serves as a tutorial on how to get started with writing a Java plugin.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3633}{}\doxysection{Basics}\label{doc_tutorials_java-plugins_md_autotoc_md3633}
If you want to know more about plugins in general, please check out the \mbox{\hyperlink{doc_tutorials_plugins_md}{How-\/\+To\+: Write a Plugin}} page. If you need a tutorial for using Key and Key\+Set in Java, please check out the \mbox{\hyperlink{doc_tutorials_java-kdb_md}{How-\/\+To\+: Java kdb}} page.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3634}{}\doxysection{Two Technologies used in Java Plugins}\label{doc_tutorials_java-plugins_md_autotoc_md3634}
Before we will take a look into how to write a plugin in Java, it is important to note, that there are two technologies needed\+:


\begin{DoxyItemize}
\item JNI plugin
\item JNA binding
\end{DoxyItemize}

If you would like to write a plugin in Java and look into the documentation, then you will definitely meet two notions like {\ttfamily JNI Plugin} and {\ttfamily JNA Binding}, so let\textquotesingle{}s clarify, what they are.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3635}{}\doxysubsection{JNI Plugin}\label{doc_tutorials_java-plugins_md_autotoc_md3635}
Java Native Interface is a framework, which allows Java code to execute or to be executed by programms, written in other languages like C, C++ and Assembly. Most of Elektra’s plugins are written in C and C++. Developer can use the JNI plugin, which was created specifically for Elektra to write plugins using Java code. For more information on JNI plugins, please take a look \mbox{\hyperlink{md_src_plugins_jni_README_src_plugins_jni_README_md}{here}}.

In order to use {\ttfamily jni} in Elektra, the library {\ttfamily jni.\+h} must be provided for it. This file is used by the JNI Plugin, which has to be enabled, while installing Elektra. More information on how to install the JNI plugin can be found \mbox{\hyperlink{md_src_plugins_jni_README_src_plugins_jni_README_md}{here}}. Please read carefully and follow instructions in “\+Installation”.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3636}{}\doxysubsection{JNA Binding}\label{doc_tutorials_java-plugins_md_autotoc_md3636}
Java Native Access is Java technology (library), which like JNI allows a developer to run native code using only Java by providing access to native shared libraries. In order to use the JNA binding, it should be installed first. You can find more information on \href{https://github.com/java-native-access/jna}{\texttt{ JNA’s Git\+Hub page}}.

Generally speaking, {\ttfamily JNI} and {\ttfamily JNA} are both different Java technologies that could be used to bind Java code and code written in C and C++. {\ttfamily JNI} gives developers the ability to integrate Java code into C too. That is not possible with {\ttfamily JNA}.

Here we will look into how can we write Java Plugins using the {\ttfamily JNA Binding} executed via the {\ttfamily JNI Plugin}.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3637}{}\doxysubsection{Writing a Plugin}\label{doc_tutorials_java-plugins_md_autotoc_md3637}
Under the following path you can find the examples of already existing plugins and you can look into libelektra Java library, which is used for communication with the configuration database.

In order to write a new Java Plugin, the new class has to be created under the {\ttfamily plugin} folder. It has also to extend the {\ttfamily Plugin.\+java} interface, which contains all required methods to communicate with Elektra’s database.

The {\ttfamily Plugin.\+java} interface contains methods for the five basic functions of plugins\+:


\begin{DoxyItemize}
\item \href{https://doc.libelektra.org/api/latest/html/group__plugin.html\#ga23c2eb3584e38a4d494eb8f91e5e3d8d}{\texttt{ {\ttfamily elektra\+Plugin\+Open}}},
\item \href{https://doc.libelektra.org/api/latest/html/group__plugin.html\#gacb69f3441c6d84241b4362f958fbe313}{\texttt{ {\ttfamily elektra\+Plugin\+Get}}},
\item \href{https://doc.libelektra.org/api/latest/html/group__plugin.html\#gae65781a1deb34efc79c8cb9d9174842c}{\texttt{ {\ttfamily elektra\+Plugin\+Set}}},
\item \href{https://doc.libelektra.org/api/latest/html/group__plugin.html\#gad74b35f558ac7c3262f6069c5c47dc79}{\texttt{ {\ttfamily elektra\+Plugin\+Error}}}, and
\item \href{https://doc.libelektra.org/api/latest/html/group__plugin.html\#ga1236aefe5b2baf8b7bf636ba5aa9ea29}{\texttt{ {\ttfamily elektra\+Plugin\+Close}}}
\end{DoxyItemize}

It is expected that each method returns -\/1 if an error occurred or 0 if everything worked as expected.\hypertarget{doc_tutorials_java-plugins_md_autotoc_md3638}{}\doxysubsection{Usage of Plugin}\label{doc_tutorials_java-plugins_md_autotoc_md3638}
To use the binding in a Java project, you have to include the jar file {\ttfamily libelektra-\/\$\+VERSION.\+jar} in the project. The version number is the same one as used for Elektra. This jar is created upon build, if you enable the JNA binding. If you are going to use a build tool supporting dependency resolution (e.\+g. Gradle, Maven, ...), you can fetch the Java binding from maven central with group id {\ttfamily \mbox{\hyperlink{namespaceorg_1_1libelektra}{org.\+libelektra}}} and artefact id {\ttfamily libelektra} using the desired version number. You can also install the jar into your local maven repository. See JNA binding for more information. 