Forum Stats

  • 3,826,755 Users
  • 2,260,705 Discussions
  • 7,897,071 Comments

Discussions

Calling Java Method from Objective C

PavlovOnline
PavlovOnline Member Posts: 1
edited Jun 17, 2015 4:48PM in Java Native Interface (JNI)

I am trying to invoke a simple helloWorld java method from Objective C via JNI.

The JNI starts, but FindClass returns nil. No class is found, therefore, can't invoke the method.

Please suggest.

My setup:

Java class (located on desktop, compiled with javac)


<span class="kwd" style="color: #00008b;">public</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">class</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">HelloWorld</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"><br/>   </span><span class="kwd" style="color: #00008b;">public</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">static</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">void</span><span class="pln" style="color: #000000;"> main</span><span class="pun" style="color: #000000;">(</span><span class="typ" style="color: #2b91af;">String</span><span class="pun" style="color: #000000;">[]</span><span class="pln" style="color: #000000;"> args</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> <br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #000000;">.</span><span class="kwd" style="color: #00008b;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"Hello, World"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/>   </span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"><br/></span><span class="pun" style="color: #000000;">}</span>


Objective C


<span class="typ" style="color: #2b91af;">JNIEnv</span><span class="pun" style="color: #000000;">*</span><span class="pln" style="color: #000000;"> jniEnv</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/></span><span class="typ" style="color: #2b91af;">JavaVM</span><span class="pun" style="color: #000000;">*</span><span class="pln" style="color: #000000;"> javaVM</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/><br/></span><span class="pun" style="color: #000000;">-</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(</span><span class="kwd" style="color: #00008b;">void</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;">launch </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"><br/>   </span><span class="typ" style="color: #2b91af;">JavaVMOption</span><span class="pln" style="color: #000000;"> javaVMOptions</span><span class="pun" style="color: #000000;">[</span><span class="lit" style="color: #800000;">2</span><span class="pun" style="color: #000000;">];</span><span class="pln" style="color: #000000;"><br/>  javaVMOptions</span><span class="pun" style="color: #000000;">[</span><span class="lit" style="color: #800000;">0</span><span class="pun" style="color: #000000;">].</span><span class="pln" style="color: #000000;">optionString </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"-Djava.awt.headless=true"</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/><br/>   </span><span class="com" style="color: #808080;">// directory path</span><span class="pln" style="color: #000000;"><br/>  javaVMOptions</span><span class="pun" style="color: #000000;">[</span><span class="lit" style="color: #800000;">1</span><span class="pun" style="color: #000000;">].</span><span class="pln" style="color: #000000;">optionString </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"-Djava.class.path=/Users/me/Desktop"</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/><br/>   </span><span class="typ" style="color: #2b91af;">JavaVMInitArgs</span><span class="pln" style="color: #000000;"> javaVMInitArgs</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  javaVMInitArgs</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">version </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> JNI_VERSION_1_6</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  javaVMInitArgs</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">options </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> javaVMOptions</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  javaVMInitArgs</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">nOptions </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #800000;">1</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  javaVMInitArgs</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">ignoreUnrecognized  </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> JNI_TRUE</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/><br/>   </span><span class="kwd" style="color: #00008b;">int</span><span class="pln" style="color: #000000;"> result </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> JNI_CreateJavaVM</span><span class="pun" style="color: #000000;">(&</span><span class="pln" style="color: #000000;">javaVM</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(</span><span class="kwd" style="color: #00008b;">void</span><span class="pun" style="color: #000000;">**)&</span><span class="pln" style="color: #000000;">jniEnv</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">&</span><span class="pln" style="color: #000000;">javaVMInitArgs</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/>   </span><span class="typ" style="color: #2b91af;">NSLog</span><span class="pun" style="color: #000000;">(@</span><span class="str" style="color: #800000;">"Java vm launched %i"</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> result</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/>  invoke_class</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">jniEnv</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/></span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"><br/><br/><br/></span><span class="kwd" style="color: #00008b;">void</span><span class="pln" style="color: #000000;"> invoke_class</span><span class="pun" style="color: #000000;">(</span><span class="typ" style="color: #2b91af;">JNIEnv</span><span class="pun" style="color: #000000;">*</span><span class="pln" style="color: #000000;"> env</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"><br/>  jclass helloWorldClass</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  jmethodID mainMethod</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  jobjectArray applicationArgs</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/>  jstring applicationArg0</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"><br/><br/>  helloWorldClass </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">FindClass</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"HelloWorld"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/><br/>  mainMethod </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">GetStaticMethodID</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> helloWorldClass</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"main"</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"([Ljava/lang/String;)V"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/><br/>  applicationArgs </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">NewObjectArray</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #800000;">1</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">FindClass</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"java/lang/String"</span><span class="pun" style="color: #000000;">),</span><span class="pln" style="color: #000000;"> NULL</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/>  applicationArg0 </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">NewStringUTF</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"From-C-program"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/>   </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">SetObjectArrayElement</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> applicationArgs</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #800000;">0</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> applicationArg0</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/><br/>   </span><span class="pun" style="color: #000000;">(*</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">)-></span><span class="typ" style="color: #2b91af;">CallStaticVoidMethod</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">env</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> helloWorldClass</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> mainMethod</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> applicationArgs</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"><br/></span><span class="pun" style="color: #000000;">}</span>

Tagged:

Answers

  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    edited Jun 17, 2015 4:48PM

    When it returns null it means an exception occurred.  So get the exception and then look at what it says.  JNI documentation has methods for getting that information.

    And you should always check return types for java methods.  Ignoring exceptions after they occur, which is what happens here, can result in an unstable VM.

This discussion has been closed.