This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Create Java VM from MingW GCJ?


Hi Ranjit / Erik / Everyone,

Is it possible to create a Java VM from a C program on
MingW? The linker is complaining about unresolved symbols
and a two-second explanation from you will save me
hours of banging my head against the wall.

Thanks.

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/

build.bat
---------
setlocal
set TOP=D:\temp\thisiscool-gcc\gcc-3.3
set TOP=D:\PROGRA~1\thisiscool-gcc\gcc-3.3
PATH %TOP%\bin;%PATH%
gcj -c -fjni Prog.java -o prog.o
gcc -c -I%TOP%\include invoke.c -o invoke.o
gcj -o prog.exe prog.o invoke.o
endlocal

Built output
------------
invoke.o(.text+0xc4):invoke.c: undefined reference to `_imp__JNI_GetDefaultJavaVMInitArgs at 4'
invoke.o(.text+0xdd):invoke.c: undefined reference to `_imp__JNI_CreateJavaVM at 12'

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;
    JavaVMInitArgs 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 = 0x00010002;

    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    /* Create the Java VM */
    res = JNI_CreateJavaVM(&jvm,(void**) &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);
}

Prog.java
---------
public class Prog {
    public static void main(String[] args) {
        System.out.println("Hello World" + args[0]);
        System.out.println("gnu.gcj.progname = "+System.getProperty("gnu.gcj.progname"));
    }
}





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