This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Compiling C code to Invoke the JVM using JNI with Cygwin/GCC


----------------Explanation of problem-------------

I am trying to make a simple C program invoke
the JVM (Java Virtual Machine) using JNI (Java Native Interface).

The platform I am using is Windows NT 4.0;  Cygwin with GCC 2.95.2  ( 
however I will use preferably EGCS 1.1.2 due to a template expansion bug 
which may make it inevitable that I use EGCS 1.1.2 instead of GCC 2.95.2).

Could someone please tell me the exact compile/link commands to compile this 
under Cygwin B20.1 using GCC or EGCS...

I tried this command for instance, but didn't get it to work:

g++ -o invoke.exe -g invoke.o -Ic:/jdk2/include -Ic:/jdk2/include/win32 
-Lc:/jdk2/jre/bin/classic -ljvm

then I tried

g++ -o invoke.exe -g -Ic:/jdk2/include -Ic:/jdk2/include/win32 invoke.c 
-Lc:/jdk2/jre/bin/classic jvm

And many others attempts.....  I'm so confused.. What do I need?  jvm.dll  
or jvm.lib or what?  (Once again, I'm using GCC under Cygwin B20.1).  I 
found so many examples using  .a, .lib, .dll..........  non particularly 
pertain to Cygwin..

I am confused on how to link in the required .dll or .a,
or .lib file.  Can someone please clarify this for me using; once again, 
this is using Cygwin B20.1 ......   Perhaps I need Ming?...


This is the example I'm trying to get to compile; located in Sun's Java 
(JNI) tutorial.....     The example listed there mentions MSVC++ 4.0.. and 
kind of shows how to compile for that...   But that doesn't help me.. I 
don't know how to do it with GCC under Cygwin..

Thank you very much in advance.


Here are the two files:
-----------------Proj.java--------------------------
public class Prog {
    public static void main(String[] args) {
	System.out.println("Hello World" + args[0]);
    }
}
----------------------------------------------------


----------------invoke.c----------------------------
#include <jni.h>

#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif

#define USER_CLASSPATH "." /* where Prog.class is */

main() {
    JNIEnv *env;
    JavaVM *jvm;
    JDK1_1InitArgs vm_args;
    jint res;
    jclass cls;
    jmethodID mid;
    jstring jstr;
    jobjectArray args;
    char classpath[1024];


    /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond 
*/
    vm_args.version = 0x00010001;

    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    /* Append USER_CLASSPATH to the end of default system class path */
    sprintf(classpath, "%s%c%s",
            vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
    vm_args.classpath = classpath;

    /* Create the Java VM */
    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");
    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);
}

----------------------------------------------------


________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]