1
votes

I'm getting a "error LNK1104: cannot open file {path}\jvm.lib" when trying tocompile a VS C++/CLI (managed) project. It's very simple and my goal is to call some Java methods in pre-existing java libs - here is the code I'm using:

// This is the main DLL file.

#include "stdafx.h"
#include <jni_md.h>
#include <jni.h>
#include "JBridge.h"

#pragma once

using namespace System;

namespace JBridge 
{

public ref class JniBridge
{
    // TODO: Add your methods for this class here.


public:
    void HelloWorldTest()
    {
        System::Console::WriteLine("Hello Worldl from managed C++!");
    }

    JNIEnv* create_vm(JavaVM ** jvm) 
    {
        JNIEnv *env;
        JavaVMInitArgs vm_args;

        JavaVMOption options; 
        //Path to the java source code     
        options.optionString = "-Djava.class.path=D:\\Java Src\\TestStruct"; 
        vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
        vm_args.nOptions = 1;
        vm_args.options = &options;
        vm_args.ignoreUnrecognized = 0;

        int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
        if(ret < 0)
            printf("\nUnable to Launch JVM\n");       
        return env;
    }
    };
}

I've verified the file does exist in the path location and I've added it to the project properties for the include dir and the linker property pages.

Update Got the jvm.lib to be linked with a bit more fiddling.

Compilation causes following errors during build:

Error 1 error LNK2028: unresolved token (0A00000A) "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *)" (?create_vm@@$$FYAPAUJNIEnv_@@PAPAUJavaVM_@@@Z) c:\Temp\CLRTest\JBridge\JBridge\JBridge.obj JBridge Error 2 error LNK2019: unresolved external symbol "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *)" (?create_vm@@$$FYAPAUJNIEnv_@@PAPAUJavaVM_@@@Z) c:\Temp\CLRTest\JBridge\JBridge\JBridge.obj JBridge Error 3 error LNK1120: 2 unresolved externals c:\temp\CLRTest\JBridge\Debug\JBridge.dll JBridge

1
BTW - i'm trying to run this in x64 which is what the JVM is...bbqchickenrobot
Looks like it can't find the jvm. Did you set JAVA_HOME, if using env variable? Did you move the jvm from its original location?Jason Huntley
@Jason didn't move the location, but I rebooted and it somehow went away. Very odd. Still receiving errors however.bbqchickenrobot
Did you check to make sure the headers included match the same version of jvm.lib?Jason Huntley

1 Answers

1
votes

Work around was to dynamically load the JVM by using LoadLibrary("path/to/jvm"); and then invoking the native functions.