
\begin{DoxyItemize}
\item infos = Information about the jni plugin is in keys below
\item infos/author = Markus Raab \href{mailto:elektra@libelektra.org}{\tt elektra@libelektra.\+org}
\item infos/licence = B\+SD
\item infos/provides =
\item infos/needs =
\item infos/placements = getstorage setstorage
\item infos/status = unittest configurable global memleak experimental discouraged
\item infos/description = generic Java plugin
\end{DoxyItemize}

Allows you to write plugins in Java.

This plugin needs the J\+NA bindings to work. Furthermore, it requires Java 8 or later.

While the plugin internally uses J\+NI (thus the name), the Java binding for your Java plugin may use something different, e.\+g. J\+NA. The requirements for the Java bindings are\+:


\begin{DoxyItemize}
\item needs to have the classes {\ttfamily elektra/\+Key} and {\ttfamily elektra/\+Key\+Set} with
\begin{DoxyItemize}
\item a constructor that takes a C-\/\+Pointer as long (J)
\item a method \char`\"{}release\char`\"{} that gives up ownership (set internal pointer to N\+U\+LL)
\end{DoxyItemize}
\end{DoxyItemize}

The Java plugin itself needs to have the following methods\+:


\begin{DoxyItemize}
\item constructor without arguments (i.\+e. default constructor)
\item open with argument {\ttfamily elektra/\+Key\+Set} (the plugin\textquotesingle{}s conf) and {\ttfamily elektra/\+Key}
\item close with argument {\ttfamily elektra/\+Key}
\item get with arguments {\ttfamily elektra/\+Key\+Set} and {\ttfamily elektra/\+Key}
\item set with arguments {\ttfamily elektra/\+Key\+Set} and {\ttfamily elektra/\+Key}
\item error with arguments {\ttfamily elektra/\+Key\+Set} and {\ttfamily elektra/\+Key}
\end{DoxyItemize}

\subsection*{Installation}

\subsubsection*{Java prerequisites on Debian 9}

openjdk-\/8 and 9 do not work reliable\+: jvm crashes without usable backtrace.

When using non-\/standard paths, you have to set J\+A\+V\+A\+\_\+\+H\+O\+ME before invoking cmake. (For example when you unpack Oracle Java to {\ttfamily /usr/local} or {\ttfamily /opt}.) For example\+: \begin{DoxyVerb} JAVA_HOME=/usr/local/jdk-9.0.1
\end{DoxyVerb}


\subsubsection*{Java prerequisites on Debian 8}

Please install java8 as package, e.\+g. \href{http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html}{\tt for debian} and then let cmake actually find jdk8\+: \begin{DoxyVerb}cd /usr/lib/jvm/ && sudo rm -f default-java && sudo ln -s java-8-oracle default-java
\end{DoxyVerb}


and for the run-\/time, create the file {\ttfamily /etc/ld.so.\+conf.\+d/java-\/8-\/oracle.conf} with the content (for amd64)\+: \begin{DoxyVerb}/usr/lib/jvm/default-java/jre/lib/amd64
/usr/lib/jvm/default-java/lib/amd64
/usr/lib/jvm/default-java/jre/lib/amd64/server
\end{DoxyVerb}


and run\+: \begin{DoxyVerb}sudo ldconfig
\end{DoxyVerb}


\subsubsection*{Java prerequisites on mac\+OS}

mac\+OS includes an old apple specific version of java, based on 1.\+6. However, for the jni plugin version 1.\+8 of Java is required, so either the openjdk or the oracle jdk has to be installed.

Please install oracle\textquotesingle{}s jdk8 via their provided installer. After that, you have to set the J\+A\+V\+A\+\_\+\+H\+O\+ME environment variable to the folder where the jdk is installed, usually like \begin{DoxyVerb}export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/"
\end{DoxyVerb}


As mac\+OS handles linked libraries differently, there is no ldconfig command. Instead you can export an environment variable to tell elektra the location of the java dynamic libraries. \begin{DoxyVerb}export DYLD_FALLBACK_LIBRARY_PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/server/"
\end{DoxyVerb}


Afterwards, the jni plugin should be included in the build and compile successfully.

\paragraph*{Troubleshooting}

If it should still not find the correct jni version, or says the jni version is not 1.\+8, then it most likely still searches in the wrong directory for the jni header file. It has been experienced that if the project has been built already without this environment variable set, the java location is cached. As a result, it will be resolved wrong in future builds, even though the environment variable is set. To resolve this, it should be enough to delete the C\+Make\+Cache.\+txt file in the build directory and reconfigure the build.

\subsubsection*{Enabling the plugin}

Then enable the plugin using ({\ttfamily A\+LL;-\/\+E\+X\+P\+E\+R\+I\+M\+E\+N\+T\+AL} is default)\+: \begin{DoxyVerb}cmake -DPLUGINS="ALL;-EXPERIMENTAL;jni" /path/to/libelektra
\end{DoxyVerb}


Running \begin{DoxyVerb}kdb-full
\end{DoxyVerb}


should work then (needs B\+U\+I\+L\+D\+\_\+\+F\+U\+LL cmake option), if you get one of these\+: \begin{DoxyVerb}kdb-full: error while loading shared libraries: libmawt.so: cannot open shared object file: No such file or directory
kdb-full: error while loading shared libraries: libjawt.so: cannot open shared object file: No such file or directory
\end{DoxyVerb}


You missed one of the ldconfig steps.

\subsection*{Plugin Config}

You need to pass \+:


\begin{DoxyItemize}
\item classname the classname to use as plugin, e.\+g. {\ttfamily elektra/plugin/\+Echo}
\item classpath the classpath where to find J\+NA, the package elektra and other classes needed
\end{DoxyItemize}

Additionally, you can set\+:


\begin{DoxyItemize}
\item option allows you to pass a option to the jvm, default\+: {\ttfamily -\/verbose\+:gc,class,jni}
\item ignore allows you to ignore broken options, default\+: {\ttfamily false}
\item print allows you to print java exceptions for debugging purposes
\end{DoxyItemize}

E.\+g. \begin{DoxyVerb}kdb info -c classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/lib/java:/path/to/libelektra/src/bindings/jna,print= jni
kdb check -c classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/lib/java:/path/to/libelektra/src/bindings/jna,print= jni
kdb mount -c classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/lib/java:/path/to/src/bindings/jna,print= file.properties /jni jni classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/lib/java:/path/to/libelektra/src/bindings/jna,print=
\end{DoxyVerb}


Or if {\ttfamily .jar} is already installed\+: \begin{DoxyVerb}bin/kdb mount -c classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/share/java/elektra.jar,print= file.properties /jni jni classname=elektra/plugin/PropertiesStorage,classpath=.:/usr/share/java/jna.jar:/usr/share/java/elektra.jar,print=
\end{DoxyVerb}


Additionally, the java implementation can request any other additional configuration, read about it below in the section (specific java plugin). If you are reading this page on Git\+Hub, you won\textquotesingle{}t see it, because the plugins dynamically append text after the end of this page.

\subsection*{Development}

To know how the methods of your class are called, use\+: \begin{DoxyVerb}javap -s YourClass
\end{DoxyVerb}


Also explained \href{https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp15773}{\tt here}

\href{https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html}{\tt J\+NI Functions} \href{https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html}{\tt Invocation}

\subsection*{Issues}

Argumentation for discouraged\+:


\begin{DoxyItemize}
\item You cannot use the plugin with openjdk\+: You get a linker error because of some missing private S\+UN symbols. In Debian9 it crashes with openjdk8/9.
\item Only a single java plugin can be loaded
\item When this plugin is enabled, valgrind detects memory problems even if the plugin is not mounted. 
\end{DoxyItemize}