<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[JNI] Aufruf von CallStaticVoidMethod() kehrt nicht zurück]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich befasse mich seit vorgestern mit dem java native interface.</p>
<p>Hab mir den formatting objects processor (fop) von apache heruntergeladen, mit dem ich meine XML-FO Datei in eine PDF-Datei konvertieren kann.</p>
<p>Der Aufruf per Kommandozeile funktioniert schonmal:</p>
<pre><code>java -cp &lt;HierStehtMeinClassPath&gt; org.apache.fop.cli.Main Quelldatei.fo Zieldatei.pdf
</code></pre>
<p>Die PDF-Datei wird ohne Probleme erstellt.</p>
<p>Das gleiche möchte ich jetzt aus meiner C++-Konsolenanwendung heraus machen (ich verwende VS2010).</p>
<p><strong>Mein Problem: Beim Aufruf von &quot;CallStaticVoidMethod&quot; wird das PDF ebenfalls korrekt erstellt, allerdings beendet sich die Konsolen-Anwendung dann. Alle Anweisungen nach &quot;CallStaticVoidMethod&quot; werden nicht mehr ausgeführt.</strong></p>
<pre><code>#include &quot;stdafx.h&quot;

#include &lt;Windows.h&gt;
#include &lt;jni.h&gt;
#include &lt;string&gt;

using namespace std;

typedef jint (__stdcall* JNI_CREATEJAVAVM)(JavaVM **pvm, void ** penv, void *args); 

int _tmain(int argc, _TCHAR* argv[])
{
   JavaVM *jvm;       /* denotes a Java VM */ 
   JNIEnv *env;       /* pointer to native method interface */ 

   jstring jstrSource;
   jstring jstrTarget;

   jobjectArray args;
   jclass stringClass;

   JavaVMInitArgs vm_args;
   JavaVMOption options[4];

   string strClasses = &quot;&quot;;
   strClasses += &quot;-Djava.class.path=&quot;;
   strClasses += &quot;D:/fop/fop.jar;&quot;;
   strClasses += &quot;D:/fop/avalon-framework-4.2.0.jar;&quot;;
   strClasses += &quot;D:/fop/batik-all-1.7.jar;&quot;;
   strClasses += &quot;D:/fop/commons-io-1.3.1.jar;&quot;;
   strClasses += &quot;D:/fop/commons-logging-1.0.4.jar;&quot;;
   strClasses += &quot;D:/fop/serializer-2.7.0.jar;&quot;;
   strClasses += &quot;D:/fop/xalan-2.7.0.jar;&quot;;
   strClasses += &quot;D:/fop/xercesImpl-2.7.1.jar;&quot;;
   strClasses += &quot;D:/fop/xml-apis-1.3.04.jar;&quot;;
   strClasses += &quot;D:/fop/xml-apis-ext-1.3.04.jar;&quot;;
   strClasses += &quot;D:/fop/xmlgraphics-commons-1.4.jar;&quot;;

   options[0].optionString = &quot;-Djava.compiler=NONE&quot;;
   options[1].optionString = (char*)strClasses.c_str();
   options[2].optionString = &quot;-verbose:jni&quot;;

   vm_args.version  = JNI_VERSION_1_6;
   vm_args.nOptions = 3;
   vm_args.options = options;
   vm_args.ignoreUnrecognized = FALSE; 

   JNI_CREATEJAVAVM jni_JNI_CreateJavaVM = NULL;

   HMODULE hJvm = ::LoadLibrary(&quot;C:\\Programme\\Java\\jdk1.6.0_25\\jre\\bin\\server\\jvm.dll&quot;);

   if(hJvm == NULL)
   {
      printf(&quot;Could not load jvm.dll\n&quot;);
      return 0;
   }

   jni_JNI_CreateJavaVM = (JNI_CREATEJAVAVM)GetProcAddress(hJvm, &quot;JNI_CreateJavaVM&quot;);

   long status  = jni_JNI_CreateJavaVM(&amp;jvm, (void**) &amp;env, &amp;vm_args);

   if(status == JNI_OK &amp;&amp; jvm != NULL)
   {
      jclass cls = env-&gt;FindClass(&quot;org/apache/fop/cli/Main&quot;);

      if(env-&gt;ExceptionOccurred())
      {
         env-&gt;ExceptionDescribe();
      }	

      jmethodID mid = env-&gt;GetStaticMethodID(cls, &quot;main&quot;, &quot;([Ljava/lang/String;)V&quot;);

      stringClass = env-&gt;FindClass(&quot;java/lang/String&quot;);
      args = env-&gt;NewObjectArray(2, stringClass, env-&gt;NewStringUTF(&quot;&quot;));

      env-&gt;SetObjectArrayElement(args,0,env-&gt;NewStringUTF(&quot;D:\\Quelldatei.fo&quot;));
      env-&gt;SetObjectArrayElement(args,1,env-&gt;NewStringUTF(&quot;D:\\Zieldatei.pdf&quot;));
      env-&gt;CallStaticVoidMethod(cls, mid, args);

      if(env-&gt;ExceptionOccurred())
      {
         env-&gt;ExceptionDescribe();
      }	

      jvm-&gt;DestroyJavaVM();
   }	 

   return 0;
}
</code></pre>
<p>Die letzten Zeilen der Konsole sehen so aus:</p>
<pre><code>[Dynamic-linking native method sun.dc.pr.PathStroker.appendLine ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.closedSubpath ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.endPath ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.dispose ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.initIDs ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.init ... JNI]
[Dynamic-linking native method sun.java2d.cmm.kcms.CMM.cmmGetProfileSize ... JNI]
[Dynamic-linking native method sun.java2d.cmm.kcms.CMM.cmmGetProfileData ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.deflateBytes ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.end ... JNI]
[Dynamic-linking native method java.io.FileOutputStream.close0 ... JNI]
[Dynamic-linking native method sun.awt.windows.WToolkit.shutdown ... JNI]
[Dynamic-linking native method java.lang.Shutdown.halt0 ... JNI]
</code></pre>
<p>Es tritt nirgendwo eine Ausnahme auf. Und, wie bereits erwähnt: Das PDF wird korrekt erstellt.</p>
<p>Hab dann auch mal eine HelloWorld.class erstellt und deren main-Funktion mit CallStaticVoidMethod() aufgerufen. Dieser Aufruf kehrt wieder zurück!</p>
<p>Kann mir jemand von euch helfen? Evtl. auch mit Ratschlägen zu weiteren Logging- oder Debug-Möglichkeiten für das JNI.</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/301220/jni-aufruf-von-callstaticvoidmethod-kehrt-nicht-zurück</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 03:43:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/301220.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 22 Mar 2012 06:26:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [JNI] Aufruf von CallStaticVoidMethod() kehrt nicht zurück on Thu, 22 Mar 2012 06:26:25 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich befasse mich seit vorgestern mit dem java native interface.</p>
<p>Hab mir den formatting objects processor (fop) von apache heruntergeladen, mit dem ich meine XML-FO Datei in eine PDF-Datei konvertieren kann.</p>
<p>Der Aufruf per Kommandozeile funktioniert schonmal:</p>
<pre><code>java -cp &lt;HierStehtMeinClassPath&gt; org.apache.fop.cli.Main Quelldatei.fo Zieldatei.pdf
</code></pre>
<p>Die PDF-Datei wird ohne Probleme erstellt.</p>
<p>Das gleiche möchte ich jetzt aus meiner C++-Konsolenanwendung heraus machen (ich verwende VS2010).</p>
<p><strong>Mein Problem: Beim Aufruf von &quot;CallStaticVoidMethod&quot; wird das PDF ebenfalls korrekt erstellt, allerdings beendet sich die Konsolen-Anwendung dann. Alle Anweisungen nach &quot;CallStaticVoidMethod&quot; werden nicht mehr ausgeführt.</strong></p>
<pre><code>#include &quot;stdafx.h&quot;

#include &lt;Windows.h&gt;
#include &lt;jni.h&gt;
#include &lt;string&gt;

using namespace std;

typedef jint (__stdcall* JNI_CREATEJAVAVM)(JavaVM **pvm, void ** penv, void *args); 

int _tmain(int argc, _TCHAR* argv[])
{
   JavaVM *jvm;       /* denotes a Java VM */ 
   JNIEnv *env;       /* pointer to native method interface */ 

   jstring jstrSource;
   jstring jstrTarget;

   jobjectArray args;
   jclass stringClass;

   JavaVMInitArgs vm_args;
   JavaVMOption options[4];

   string strClasses = &quot;&quot;;
   strClasses += &quot;-Djava.class.path=&quot;;
   strClasses += &quot;D:/fop/fop.jar;&quot;;
   strClasses += &quot;D:/fop/avalon-framework-4.2.0.jar;&quot;;
   strClasses += &quot;D:/fop/batik-all-1.7.jar;&quot;;
   strClasses += &quot;D:/fop/commons-io-1.3.1.jar;&quot;;
   strClasses += &quot;D:/fop/commons-logging-1.0.4.jar;&quot;;
   strClasses += &quot;D:/fop/serializer-2.7.0.jar;&quot;;
   strClasses += &quot;D:/fop/xalan-2.7.0.jar;&quot;;
   strClasses += &quot;D:/fop/xercesImpl-2.7.1.jar;&quot;;
   strClasses += &quot;D:/fop/xml-apis-1.3.04.jar;&quot;;
   strClasses += &quot;D:/fop/xml-apis-ext-1.3.04.jar;&quot;;
   strClasses += &quot;D:/fop/xmlgraphics-commons-1.4.jar;&quot;;

   options[0].optionString = &quot;-Djava.compiler=NONE&quot;;
   options[1].optionString = (char*)strClasses.c_str();
   options[2].optionString = &quot;-verbose:jni&quot;;

   vm_args.version  = JNI_VERSION_1_6;
   vm_args.nOptions = 3;
   vm_args.options = options;
   vm_args.ignoreUnrecognized = FALSE; 

   JNI_CREATEJAVAVM jni_JNI_CreateJavaVM = NULL;

   HMODULE hJvm = ::LoadLibrary(&quot;C:\\Programme\\Java\\jdk1.6.0_25\\jre\\bin\\server\\jvm.dll&quot;);

   if(hJvm == NULL)
   {
      printf(&quot;Could not load jvm.dll\n&quot;);
      return 0;
   }

   jni_JNI_CreateJavaVM = (JNI_CREATEJAVAVM)GetProcAddress(hJvm, &quot;JNI_CreateJavaVM&quot;);

   long status  = jni_JNI_CreateJavaVM(&amp;jvm, (void**) &amp;env, &amp;vm_args);

   if(status == JNI_OK &amp;&amp; jvm != NULL)
   {
      jclass cls = env-&gt;FindClass(&quot;org/apache/fop/cli/Main&quot;);

      if(env-&gt;ExceptionOccurred())
      {
         env-&gt;ExceptionDescribe();
      }	

      jmethodID mid = env-&gt;GetStaticMethodID(cls, &quot;main&quot;, &quot;([Ljava/lang/String;)V&quot;);

      stringClass = env-&gt;FindClass(&quot;java/lang/String&quot;);
      args = env-&gt;NewObjectArray(2, stringClass, env-&gt;NewStringUTF(&quot;&quot;));

      env-&gt;SetObjectArrayElement(args,0,env-&gt;NewStringUTF(&quot;D:\\Quelldatei.fo&quot;));
      env-&gt;SetObjectArrayElement(args,1,env-&gt;NewStringUTF(&quot;D:\\Zieldatei.pdf&quot;));
      env-&gt;CallStaticVoidMethod(cls, mid, args);

      if(env-&gt;ExceptionOccurred())
      {
         env-&gt;ExceptionDescribe();
      }	

      jvm-&gt;DestroyJavaVM();
   }	 

   return 0;
}
</code></pre>
<p>Die letzten Zeilen der Konsole sehen so aus:</p>
<pre><code>[Dynamic-linking native method sun.dc.pr.PathStroker.appendLine ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.closedSubpath ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.endPath ... JNI]
[Dynamic-linking native method sun.dc.pr.PathStroker.dispose ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.initIDs ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.init ... JNI]
[Dynamic-linking native method sun.java2d.cmm.kcms.CMM.cmmGetProfileSize ... JNI]
[Dynamic-linking native method sun.java2d.cmm.kcms.CMM.cmmGetProfileData ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.deflateBytes ... JNI]
[Dynamic-linking native method java.util.zip.Deflater.end ... JNI]
[Dynamic-linking native method java.io.FileOutputStream.close0 ... JNI]
[Dynamic-linking native method sun.awt.windows.WToolkit.shutdown ... JNI]
[Dynamic-linking native method java.lang.Shutdown.halt0 ... JNI]
</code></pre>
<p>Es tritt nirgendwo eine Ausnahme auf. Und, wie bereits erwähnt: Das PDF wird korrekt erstellt.</p>
<p>Hab dann auch mal eine HelloWorld.class erstellt und deren main-Funktion mit CallStaticVoidMethod() aufgerufen. Dieser Aufruf kehrt wieder zurück!</p>
<p>Kann mir jemand von euch helfen? Evtl. auch mit Ratschlägen zu weiteren Logging- oder Debug-Möglichkeiten für das JNI.</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2193969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2193969</guid><dc:creator><![CDATA[Delerium]]></dc:creator><pubDate>Thu, 22 Mar 2012 06:26:25 GMT</pubDate></item><item><title><![CDATA[Reply to [JNI] Aufruf von CallStaticVoidMethod() kehrt nicht zurück on Thu, 22 Mar 2012 07:06:59 GMT]]></title><description><![CDATA[<p>Vermutlich beendet sich die Main Methode von fop mit System.exit() statt mit return. Da wirst Du nichts dran machen können, ausser fop zu ändern oder deren API (statt CLI) zu benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2193979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2193979</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 22 Mar 2012 07:06:59 GMT</pubDate></item><item><title><![CDATA[Reply to [JNI] Aufruf von CallStaticVoidMethod() kehrt nicht zurück on Thu, 22 Mar 2012 08:48:06 GMT]]></title><description><![CDATA[<p>Danke für die Antwort.<br />
Dass ich eine Funktion des command line interfaces benutze, hatte ich zwischenzeitlich auch kurz als Ursache in Verdacht.<br />
Aber da ich absolut Null Ahnung von Java habe, wusste ich nicht, dass es verschiedene Methoden gibt die main-Funktion zu beenden und erst recht nicht, welche Folgen das für mein C++ Programm hat.</p>
<p>Das cli zu verwenden waren meine ersten Gehversuche. Mangels Funktionalität muss ich in Zukunft ohnehin die API verwenden.</p>
<p>Dank deinem Hinweis brauch ich mich jetzt nicht weiter mit dem Problem zu beschäftigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2194005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2194005</guid><dc:creator><![CDATA[Delerium]]></dc:creator><pubDate>Thu, 22 Mar 2012 08:48:06 GMT</pubDate></item></channel></rss>