Thread View: comp.lang.java.programmer
2 messages
2 total messages
Started by Juan Pedro Barbancho Manchon
Wed, 21 Jan 1998 00:00
JNI program example, need urgent help
Author: Juan Pedro Barbancho Manchon
Date: Wed, 21 Jan 1998 00:00
Date: Wed, 21 Jan 1998 00:00
102 lines
3533 bytes
3533 bytes
Dear sir,
I use the JNI sun example, It compile and build a .exe, but no run well.
The code:
////////////////////INVOKE.C///////////////////
/* * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information. *
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */
#include <jni.h>
main()
{
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and
beyond */
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
res = JNI_CreateJavaVM(&jvm,&env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = (*env)->FindClass(env, "Prog.class");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class\n");
exit(1);
}
mid = (*env)->GetStaticMethodID(env, cls, "main",
"([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main\n");
exit(1);
}
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
args = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env,
"java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
(*jvm)->DestroyJavaVM(jvm);
}
The Java program:
//////////////////////////PROG.JAVA////////////////////////////////
public class Prog {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The error:
can't fing Prog class
The . is in the classpath, Prog.class is in . directory.
Please, send me faster, Thank you
mail: ct2bamaj@uco.es
JuanP
--
--------------------------------------------------
Nombre y apelllidos:
Juan Pedro Barbancho Manch�n
Organizaci�n:
CTI
http://www.uco.es/grupos/cti
E-mail:
ct2bamaj@uco.es
Java Senior Programmer
Re: JNI program example, need urgent help
Author: Shayne Flint
Date: Sun, 25 Jan 1998 00:00
Date: Sun, 25 Jan 1998 00:00
45 lines
1991 bytes
1991 bytes
Juan Pedro Barbancho Manchon wrote:
>
> Dear sir,
> I use the JNI sun example, It compile and build a .exe, but no run well.
>
> The code:
>
Snip
>
> The error:
>
> can't fing Prog class
>
> The . is in the classpath, Prog.class is in . directory.
>
> Please, send me faster, Thank you
>
> mail: ct2bamaj@uco.es
>
This is a common problem that comes up on this news group every week.
The answer is in the doco somewhere, but here it is again.
You need to set the class path in vm_args. Something like this:
vm_args.version = 0x00010001;
res = JNI_GetDefaultJavaVMInitArgs(&vm_args);
if ( res < 0 )
printf("Error on getting default JavaVM initialization args\n");
vm_args.classpath=getenv("CLASSPATH"); /******* NOTE THIS BIT *****/
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
regards
--
------------------------------------------------------------------
-- Shayne Flint, MIEAust, CPEng shayne@ainslie-software.com
-- Ainslie Software Pty Limited http://www.ainslie-software.com
--
-- ShapeDB, a database and form drawing add-on for Visio
------------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads