> How to write C++ wrapper for java code? Where can I find a sample
> code?
It should be in any good JNI ressource.
Some code attached below.
Arne
================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
void start_java(int maxmem, char *classpath, char *mainclass,int
nargs,char **args)
{
JavaVMOption options[2];
JavaVMInitArgs vm_args;
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass mainclassptr;
jmethodID mainmethod;
jobjectArray mainargs;
jstring *mainargsptr;
int i;
char maxmemopt[16];
char classpathopt[1024];
char mainclass2[128];
sprintf(maxmemopt,"-Xmx%dm",maxmem);
sprintf(classpathopt,"-Djava.class.path=%s",classpath);
options[0].optionString = maxmemopt;
options[1].optionString = classpathopt;
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 2;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_FALSE;
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
if(res<0)
{
printf("Error creating JVM\n");
return;
}
strcpy(mainclass2,mainclass);
for(i=0;i<strlen(mainclass2);i++)
{
if(mainclass2[i]=='.')
{
mainclass2[i]='/';
}
}
mainclassptr = (*env)->FindClass(env,mainclass2);
if(mainclassptr==NULL)
{
printf("Error finding class %s\n",mainclass);
return;
}
mainmethod = (*env)->GetStaticMethodID(env,mainclassptr,"main",
"([Ljava/lang/String;)V");
if (mainmethod==NULL)
{
printf("Error getting main method in class %s\n",mainclass);
return;
}
mainargs =
(*env)->NewObjectArray(env,nargs,(*env)->FindClass(env,"java/lang/String"),NULL);
mainargsptr = (jstring *)malloc(nargs*sizeof(jstring));
for(i=0;i<nargs;i++)
{
mainargsptr[i] = (*env)->NewStringUTF(env,args[i]);
(*env)->SetObjectArrayElement(env,mainargs,i,mainargsptr[i]);
}
(*env)->CallStaticVoidMethod(env,mainclassptr,mainmethod,mainargs);
free(mainargsptr);
(*jvm)->DestroyJavaVM(jvm);
return;
}
int main(int argc,char *argv[])
{
/*
* java -Xmx128m -classpath test.jar test.Test A BB CCC
*/
char *args[] = { "A", "BB", "CCC" };
start_java(128, "test.jar", "test.Test", 3, args);
return 0;
}
Jack - 04 Jul 2007 07:08 GMT
On Jul 3, 7:37 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > How to write C++ wrapper for java code? Where can I find a sample
> > code?
[quoted text clipped - 89 lines]
>
> }
Thanks a lot.
>How to write C++ wrapper for java code? Where can I find a sample
>code?
see http://mindprod.com/jgloss/nativecode.html
here is a little wrapper I use for JAWS:
/**
* setup.cpp :
* invokes corresponding replicatorreceivercd?.jnlp file when invoked
from a CD
* setup.exe usually trigged by autorun.inf
* Need a different version depending on which drive letter is the CD
ROM drive.
* For windows only.
*
* copyright (c) 2003-2007 Roedy Green, Canadian Mind Products
* #101 - 2536 Wark Street
* Victoria, BC Canada V8T 4G8
* tel:(250) 361-9093
* http://mindprod.com
*/
#include "stdafx.h" /* standard precompiled header */
#include <direct.h> /* chdir, mkdir */
#include <process.h> /* exec, spawn */
#include <stdio.h> /* fclose, fgetc, printf, remove, rename, setvbuf
*/
#include <stdlib.h> /* exit, putenv, _splitpath */
#include <string.h> /* strcpy, strcat, strcmp, strupr */
/* Configure root name of all the jnlp files we might invoke */
const char* jnlpBaseName = "replicatorreceivercd";
/**
* Invoke JNLP file corresponding to drive letter
* running from.
*
* @param argc not used
* @param argv not used
*
* @return system error code
*/
int main( int argc, char* argv[] )
{
// get current drive letter, the CD-ROM drive letter.
char curDrive = 'A' + _getdrive() - 1;
// generate name of jnlp file corresponding to CD drive letter
// e.g. replicatorreceiverR.jnlp
int baseLength = strlen( jnlpBaseName );
char* jnlpName = new char[ baseLength + 7];
strcpy( jnlpName, jnlpBaseName );
// tack on drive letter A..Z
jnlpName[ baseLength ] = curDrive;
jnlpName[ baseLength + 1 ] = 0;
strcat ( jnlpName, ".jnlp" );
// Java webstart is usually in
// C:\Program Files\Java\j2re1.4.2_06\javaws\javaws.exe
// but we can't count on it.
// Find command processor shell that knows jnlp-to-javaws.exe
association.
// Shell usually c:\winnt\system32\cmd.exe though
// might be 4NT.exe or COMMAND.COM
char* shell = getenv( "ComSpec" );
if ( shell == NULL )
{
shell = "cmd.exe";
}
printf( "Installing %s with Java Web Start via the %s shell.\n",
jnlpName, shell );
// This won't work if the *.jnlp -> javaws.exe association is
broken.
// start up Java Web Start with, for example,
replicatorreceiverR.jnlp.
if ( _spawnlp( _P_OVERLAY, shell, shell, "/C", jnlpName, NULL ) )
{
// ENOENT = 2 = not found
// EINVAL = 22 = invalid arg
printf ( "Java Web Start, part of the Java JRE, must be
installed first. Error %u\n", errno );
}
delete jnlpName;
return 0;
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com