This file serves as a tutorial on how to get started with writing a java plugin.

If you want to know more about plugins in general, please check out the \hyperlink{doc_tutorials_plugins_md}{How-\/\+To\+: Write a Plugin} page. If you need a tutorial for java, please check out the \hyperlink{doc_tutorials_java-kdb_md}{How-\/\+To\+: Java kdb} page.

Before we will take a look into how to write a plugin in Java, it is important to note, that there are two ways of doing that\+:


\begin{DoxyItemize}
\item using J\+NI plugin
\item using J\+NA 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 J\+NI Plugin} and {\ttfamily J\+NA Binding}, so let\textquotesingle{}s clarify, what they are.

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 J\+NI plugin, which was created specifically for Elektra to write plugins using Java code. For more information on J\+NI plugins, please take a look \hyperlink{autotoc_md338_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 J\+NI Plugin, which has to be enabled, while installing Elektra. More information on how to install the J\+NI plugin can be found \hyperlink{autotoc_md338_src_plugins_jni_README_md}{here}. Please read carefully and follow instructions in the “\+Installation” and “\+Enabling the Plugin” sections. If you don\textquotesingle{}t have the {\ttfamily jni.\+h} file, it can be easily downloaded and placed under this path\+:


\begin{DoxyItemize}
\item {\ttfamily /path/to/\+S\+D\+K/include}
\end{DoxyItemize}

or you can reinstall the Java S\+DK.

Java Native Access is Java technology (library), which like J\+NI allows a developer to run native code using only Java by providing access to native shared libraries. In order to use the J\+NA binding, it should be installed first. You can find more information on \href{https://github.com/java-native-access/jna}{\tt J\+N\+A’s Git\+Hub page}.

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

Here we will look into, how can we write Java Plugins using -\/ {\ttfamily J\+NA Binding}.

Under the 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. You can leave some of the methods unimplemented, if there is no need for them.

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


\begin{DoxyItemize}
\item \href{https://doc.libelektra.org/api/current/html/group__plugin.html#ga23c2eb3584e38a4d494eb8f91e5e3d8d}{\tt {\ttfamily elektra\+Plugin\+Open}},
\item \href{https://doc.libelektra.org/api/current/html/group__plugin.html#gacb69f3441c6d84241b4362f958fbe313}{\tt {\ttfamily elektra\+Plugin\+Get}},
\item \href{https://doc.libelektra.org/api/current/html/group__plugin.html#gae65781a1deb34efc79c8cb9d9174842c}{\tt {\ttfamily elektra\+Plugin\+Set}},
\item \href{https://doc.libelektra.org/api/current/html/group__plugin.html#gad74b35f558ac7c3262f6069c5c47dc79}{\tt {\ttfamily elektra\+Plugin\+Error}}, and
\item \href{https://doc.libelektra.org/api/current/html/group__plugin.html#ga1236aefe5b2baf8b7bf636ba5aa9ea29}{\tt {\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.

To use the bindings in a Java project, we 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 J\+NA binding. You can also use Maven to take care of the dependencies. After that you can take your implemented class out of the {\ttfamily libelektra} folder and integrate it into your project. 