Runtime error Calling Java Methods from C++
I'm trying to add Java methods to an existing C++ program.
I'm working in W2000 enviroment using Cygnus GCC compiler.
Before adding these methods, I tested invoke example from Jni tutorial.
All worked properly. But the problems arouse when I tried to run non-static methods.
Here is the code I'm testing now:
#include <stdio.h>
#include <jni.h>
#define PATH_SEPARATOR ';'
#define USER_CLASSPATH "." /* where Prog.class is */
int main(int argc, char *argv[]) {
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jobject jobj;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
cls = env->FindClass("Prog");
if (cls == 0) {
fprintf(stderr, "Can't find consola class\n");
exit(1);
mid = env->GetStaticMethodID(cls, "Entrada", "()V");
if (mid == 0) {
fprintf(stderr, "Can't find consola.consola\n");
exit(1);
env->CallStaticVoidMethod(cls, mid);
/* jobj = env->NewObject(cls, mid);
if (jobj == 0) {
fprintf(stderr, "Can't find Prog.main\n");
exit(4);
jvm->DestroyJavaVM();
And the java code Prog.java:
public class Prog {
public static void Entrada() {
Consola Cons = new Consola();
And Consola a user class tha shows a Java window and let's do something more.
These code works fine and The Prog and Consola classes run properly.
But when I try to change Entrada method to non-static, following error appears in a window.
Microsoft Visual C++ Runtime library
Runtime Error !
Program: D:\javaini\JNI-INVOKE\invoke.exe
abnormal program termination
If I try
jobj = env->NewObject(cls, mid);
if (jobj == 0) {
fprintf(stderr, "Can't find Prog.main\n");
exit(4);
instead of
env->CallVoidMethod(cls, mid);
I get the same error.
Could anybody help me ?
Am I doing anything wrong ?
Thanks in advance. Ignasi Villagrasa.
I found it.
In JNI examples I saw
mid = env->GetMethodID(cls, "<init>", "()V");
to run constructor.
And then NewObject function.
So it's solved.
But I find another problem now.
If the class I'm calling is a graphic java class (In this case from swing), when the window is closed the application in C++ stops too.
Should I create another thread to manage the window ?
Couldn't I follow running lines after NewObject method ?
Thanks in advance, Ignasi Villagrasa.
Similar Messages
-
How to call Java method from XSLT??
Hi All,
Jdev 11.1.1.3.0
I have a requirement to implement that, I have to call Java method from XSLT. Could anyone please suggest to implement that??
Thanks,
Santosh M EAs pointed by others, you must expose your method as a custom function, registering with JDeveloper (for development time) as well as with SOA Suite (for runtime).
In the link below you will find a simple step by step example:
https://blogs.oracle.com/reynolds/entry/building_your_own_path
Regards,
Luis F. Heckler -
How to call java method from workflow script?
Hi
I have a requirement of updating field value 'Document Status' based on review/approve of content from Workflow and hence need to update the version number. For that I need to call my java method from workflow during submit of review/approve condition. Please let me know how to call java method from workflow?
Is there any alternative better way to achive this requirement from workflow? Please suggest.
Thanks,
SarangOK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
Solved? -
How to call java method from C ?
Hello,
I try to call a native method from java which calls java method from the same class. Exception (noSuchMethodError) is thrown. Any ideas?
Thanx in advance !
here is the source of the native method :
JNIEXPORT jint JNICALL Java_TestDll_Proc_1Mul_1Int_1Var_1Var_1Stdcall (JNIEnv * env, jclass jcl, jint jarg1, jint jarg2) {
int arg1;
int arg2;
int arg3;
jint res;
char * ch = "test";
jfieldID fid;
jmethodID mid;
int (* procedure) (int *, int * ,int *);
int mch;
arg1 = (int )jarg1;
arg2 = (int )jarg2;
procedure = GetProcAddress(libraryHandle,"Proc_Mul_Int_Var_Var_Stdcall");
procedure(&arg1,&arg2,&arg3);
res = (jint) arg3;
printf("(*env)->GetMethodID(env, jcl, \"test\", \"()V\");\n");
mid = (*env)->GetMethodID(env, jcl, "test", "()V");
printf("(*env)->CallVoidMethod(env, jcl, mid);\n");
(*env)->CallVoidMethod(env, jcl, mid);
return res;
here is the source of the java file:
public class TestDll {
static {
System.loadLibrary("testdllwrap");
System.out.println("java: Library testdllwrap.dll loaded");
public TestDll() {
public native int Proc_Mul_Int_Var_Var_Stdcall(int jarg1, int jarg2);
public void test() {
System.out.println("java: test()");
public static void main(String[] args) {
TestDll access = new TestDll();
int a = 5;
int b = 6;
int c = 0;
System.out.println("Calling Proc_Mul_Int_Var_Var_Stdcall");
c = access.Proc_Mul_Int_Var_Var_Stdcall(a,b);
System.out.println("Java Result = " + c);Something is wrong with the code you posted here.
Since your native method is not static, it should have the jobject instance as a parameter in the function prototype, not a jclass. Also, you should be calling CallObjectMethod with a jobject, not jclass.
Check out Jace at http://jace.reyelts.com/jace.
To call the java method you would do:
JNIEXPORT jint JNICALL Java_TestDll_Proc_1Mul_1Int_1Var_1Var_1Stdcall
(JNIEnv * env, jobject jTestDll, jint jarg1, jint jarg2) {
TestDll testDll( jTestDll );
testDll.test();
}God bless,
-Toby Reyelts -
Call Java Method From JavaScript Function
hi everyone
i need a help in calling Java method from a javaScript method
ex:
function confirmAddRecord() {
cHours =document.getElementById('frmP:ChargeHours').value;
cSTime =document.getElementById('frmP:ChargeStartTime').value;
var answer = confirm("Are you sure you want to add Record?")
if (answer){
here i want to call the Java Method that is located in session bean that takes the upper params cHours & cSTime
else{
return false;
i know i can do it as an action button but it is required me to be in that way can any one help plz
Message was edited by:
casper77That depends on the nature of your parameters. I guess you calculate the params on client and then want to submit them. In this case and if you don't want to use Ajax simple add some <input type="hidden"> elements (of course there correspondend components dependent of your framework) and store the params there. If the javascript isn't invoked by a button click, you can use a button nevertheless. Set visible="false" and call
document.getElementById('client_id_of_my_hidden_button').click();
(or maybe doClick() dependent on your framework). -
Calling Java methods from C program
After a year or so using these excellent forums to answer my queries, without ever having to post a question, I've finally had to ask for help with this one. I've already looked through the JNI tutorial, and these forums, extensively, but I'm still not quite sure of how best to solve my problem.
I need to call Java methods from a C program. I've already used JNI to call a simple C program from Java, and that's fine, but I just can't determine exactly what I need to do for the reverse to work.
What is the definitive solution for calling Java from C? Is it to use the invocation APIs to create a JVM, and then access it through the various GetMethod, FindClass methods? Or can I 'reverse engineer' the process I followed to get Java to call C?? The tutorial is a bit confusing, as the example it gives uses Callback stuff, but I guess that's only useful when you want to go Java-C-Java??
Advice appreciated,
RichForgive my blatant bumping, but anyone, please?
-
Calling Java Methods from C++
Hi ppl,
I'm a real java newbie but I'm working on a task which requires me to call java methods from a C++ program. Does anyone have any code snippet for me to start of on, say, a c++ prgram which calls a java method that prints "Hello World :oD " to the screen? Many thanks.
Tyler1. Have you looked at the tutorial?
(Look elsewhere on this website.)
2. Have you considered buying a book?
(Try "essential JNI", by Rob Gordon.) -
Calling Java Methods from Stored Procedures
Can I call Java Methods from Oracle Stored Procedures? I have a Java framework that logs events and would like to reuse it for logging events that occur in stored procedures.
nullYou need to publish java class methods to plsql.
Attached below is some information.
Although both PL/SQL modules and Java classes are stored in the database
and are managed by many of the same mechanisms, each of them resides in
its own namespace. Therefore, Java methods are not accessible from SQL
and PL/SQL by default. In order to expose Java methods to the SQL and
PL/SQL engines, first publish that Java method to the SQL namespace using
a 'Call Spec'.
Note: A 'Call Spec' does not create an additional layer of
execution so there is no performance penalty incurred.
A 'Call Spec' is simply a syntactical mechanism used to
make a method known in the SQL namespace.
The SQL name established by the 'Call Spec' can be top-level or packaged.
The syntax differs only slightly and is consistent with that used for
PL/SQL procedures and packages. For more information on the exact
syntax, see the references listed in 'Related Topics'.
In general, a top-level procedure 'Call Spec' takes the form:
CREATE OR REPLACE PROCEDURE procname ( pname mode ptype, ... )
AS LANGUAGE JAVA NAME 'javaname ( javatype, ... )';
Where: procname is the SQL name you wish to publish
pname is the name for a parameter to procname
mode is the parameter mode (i.e. IN, OUT, IN OUT)
ptype is a valid SQL type (e.g. NUMBER, CHAR, etc.)
javaname is the fully qualified name of the Java method
javatype is a Java type for the corresponding parameter
Likewise, a top-level function 'Call Spec' takes the form:
CREATE OR REPLACE FUNCTION fname ( pname mode ptype, ... ) RETURN rtype
AS LANGUAGE JAVA NAME 'javaname ( javatype, ... ) return javatype';
Where: fname is the SQL name you wish to publish
rtype is the SQL return type of the function
Note: Within the NAME clause, everything within quotes is case
sensitive. For example, if the keyword 'return' is in all
CAPS, this Call Spec will not compile.
Other optional parts of this syntax have been omitted here for simplicity.
Additional examples in subsequent sections illustrate some of these options.
eg
CREATE PROCEDURE MyProc (rowcnt IN NUMBER, numrows OUT NUMBER)
AS LANGUAGE JAVA NAME 'MyClass.MyMethod(int, int[])';
There are several important things to note here:
1.) The 'Call Spec' for a JSP must be created in the same schema as the
corresponding Java class that implements that method.
2.) IN parameters are passed by value. This is the only parameter mode
available in Java. OUT parameters, therefore, must be passed as single
element arrays in order to emulate pass by reference.
3.) Parameter names do not need to match, but the number and types of
the parameters must match (with just one exception - see item 5 below).
Oracle 8i supports conversions between an assortment of SQL and Java.
See the references listed in 'Related Topics' for additional information.
4.) Primitive types (e.g. int, float, etc.) are not required to be fully
qualified with any package name. However, standard Java object types
(e.g. String, Integer, etc.) as well as any user defined object types
(e.g. like those generated by JPublisher) must be prefixed with a
corresponding package name (e.g. java.lang) if applicable.
5.) The 'main' method which takes a single String[] parameter can be
mapped to any PL/SQL procedure or function which takes some number
of VARCHAR2 or CHAR type IN parameters. For example, the java method:
public static void main ( String[] args ) { ... }
can be mapped to each of the following:
PROCEDURE MyProc2 ( arg1 IN CHAR ) ...
PROCEDURE MyProc3 ( arg1 IN CHAR, arg2 IN VARCHAR2 ) ...
PROCEDURE MyProc4 ( arg1 IN VARCHAR2, arg2 IN VARCHAR2 ) ...
and so forth. Parameters map to the corresponding element of the String
array (e.g. arg1 -> args[0], arg2 -> args[1], etc.).
null -
How to call java method from xsl
hi friends,
How to call a java method from xsl, i have a xsl file which will call the java method and retrieve the value and display it to the user. but its work well when i set xalan.jar and xerces.jar and the java class files in my classpath and run as
java org.apache.xalan.xslt.Process -in navigate.xml -xsl nav-exst.xsl -HTML -out navoutpage.html[b]
in the command prompt but when i deploy it as web application it gives error as
[b]Namespace 'MyPack' does not contain any functions[b]OK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
Solved? -
Notable to call java method from xslt
Hi All,
We are not able to call java methods that return org.w3c.dom.NodeList.
All other methods invocation are working fine but process errors out when we try to call a method that returns org.w3c.dom.NodeList.
Any help is highly appiciated.
VigniAs pointed by others, you must expose your method as a custom function, registering with JDeveloper (for development time) as well as with SOA Suite (for runtime).
In the link below you will find a simple step by step example:
https://blogs.oracle.com/reynolds/entry/building_your_own_path
Regards,
Luis F. Heckler -
Problem calling java method from c
Hi ,
I'm trying to call a java method from a C program. it gives no error during compilation as well as building the application. but when i tried to create the JVM by running my application it pops up the message "The application failed to start because jvm.dll was not found. Re-installing the application may fix the problem." I tried out setting all the environment variables to include the jvm.dll(PATH set to c:\j2sdk1.4.2_05\bin;c:\j2sdk1.4.2_05\jre\bin). Still got the same message. Then i re-installed java platform once more. Even now i get the same error. I have more than one jvm.dll at locations jre\bin\client and server, oracle has some jvm.dll . Will that be a problem? if so can i remove those? which of them should be removed and how?
The code i'm using is
#include <stdio.h>
#include <jni.h>
#include <windows.h>
//#pragma comment (lib,"C:\\j2sdk1.4.2_05\\lib\\jvm.lib")
JavaVM jvm; / Pointer to a Java VM */
JNIEnv env; / Pointer to native method interface */
JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization requirements */
int verbose = 1; /* Debugging flag */
FARPROC JNU_FindCreateJavaVM(char *vmlibpath)
HINSTANCE hVM = LoadLibrary("jre\\bin\\server\\jvm.dll");
if (hVM == NULL)
return NULL;
return GetProcAddress(hVM, "JNI_CreateJavaVM");
void main(int argc, char **argv )
JavaVM jvm = (JavaVM )0;
JNIEnv env = (JNIEnv )0;
JavaVMInitArgs vm_args;
jclass cls;
jmethodID mid;
jint res;
FARPROC pfnCreateVM;
JavaVMOption options[4];
// jint (__stdcall pfnCreateVM)(JavaVM *pvm, void **penv, void *args) = NULL;
options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
options[1].optionString = "-Djava.class.path=c:/j2sdk1.4.2_05/jre/lib/rt.jar"; /* user classes */
options[2].optionString = "-Djava.library.path=lib"; /* set native library path */
options[3].optionString = "-verbose:jni"; /* print JNI-related messages */
/* Setup the environment */
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = 1;
JNI_GetDefaultJavaVMInitArgs ( &vm_args );
pfnCreateVM = JNU_FindCreateJavaVM("jre\\bin\\server\\jvm.dll");
res = (*pfnCreateVM)(&jvm,(void **) &env, &vm_args );
// res = JNI_CreateJavaVM(&jvm,(void **) &env, &vm_args );
/* Find the class we want to load */
cls = (*env)->FindClass( env, "InstantiatedFromC" );
if ( verbose )
printf ( "Class: %x" , cls );
/*jvm->DestroyJavaVM( );*/
Could anyone help me solve this problem as early as possible, bcoz i'm in an urge to complete the project.
Thanks in advance.
Usha.You either have to add to the system path of where is your jvm.dll is located or explicitly link to jvm.dll call GetProcAddress to obtain the address of an exported function in the DLL.
-
Call Java Method from BPM Process
Hi,
I have defined a java class with set of methods , and I want to know if there is any way to call those methods from a BPM process..
Thanks in Advance,,
Best Regards,,
Ola Essa..Use the spring component context in soa suite instead of ejb. Look for documentation in soa suite developer guide. Only if you have existing ejbs then use ejb adapters.
See this http://www.oracle.com/technetwork/middleware/soasuite/learnmore/fivecoolusecasesforspring-1885529.pdf
Vikram
Message was edited by: VikramFusionApplied -
How to call java method from actionscript
I've just now started working on Flex. May be its basic question but I’m not aware of it – how can I call a java method from actionscript. I want to call some java method on double click of a event. Can you please let me know how to proceed on this?
OK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
Solved? -
How to call Java methods from a Windows application?
Hello all,
At our company, we need to integrate our product which is a Java Swing application with a Windows application. Specifically, we are trying to call Java methods that reside in our application from the Lotus Notes email client application, which is a native Windows application. Such that when a user clicks a button in the Lotus Notes email client, it will trigger an event in our Swing application. Is this possible using JNI? Do you know of any resources or references relating to this kind of a project?
Thanks,
Mete KuralIf there is some dll interface that lets Lotus Notes load up and use a DLL, then yes, this should be possible.
-
How to call Java method from SAPUI5 applications?
Hi Experts,
Please give me information that how can I call Java method or jars from SAPUI5 applications?
Thanks,
NagHello Nag,
why do open this thread in BRM Space? I would suggest reopen this in "UI Development Toolkit for HTML5 Developer Center" Space.
Regards,
Tobias
Maybe you are looking for
-
Web Service From Java class, serialization problem
Hi, I want to create Web Service from Java class, I made java project, generated web service from it, create web service archive project and deployed it to WAS 6.40. My class have 2 methods, public int add(int a, int b); public MyResponse doSomthing(
-
DV imports - iMovie vs Final Cut - Another question
I'm importing NTSC VHS content through a Canopus ADVC-55. 1) Audio Capture via iMovie produces a DV files that works well in iMovie. If I use this same DV file (produced by iMovie) in FCE, audio will not render in FCE (red line above the track). Taki
-
How can I detect that a device is from Motorola !
hi all ! I want to detect if my device is a Motorola one, to do so, I was checking the class com.motorola.extensions.ScalableImage, if it's present I know that the device is a Motorola. but, now, I have some devices (V8) that didn't support this clas
-
Mod_pl/sql in Oracle 9i
Hi All, Is mod_pl/sql is supported in Oracle 9i and 10g. Does we have any problems in this ? I know that mod_pl/sql and WebDB concept is very much moved into Oracle Portal. So, i was wondering anybody implemented in new versions... Thanks in advance.
-
I'm still learning how to use the new iTunes format. Today I purchased an album and added it to theapprpriate playlist. That list was alreadyon my iPod Touch, so I want to replace it with the updated version. The problem is that I casn't figure out h