Calling Java Methods from Visual C/C++ using C++/CLI - java

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

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

Related

Error while generating .dll files in Visual Studio 2022. Login authentication using ADSI Api in C++

I want to make a login authentication to my web application(Java). Username and password are created in windows server 2016 active directory. Using that username and password I have to login to my web app using ADSI(Active Directory Service Interface) API in C++ language to connect to the windows server. To do this I have to create the .dll file from the C++ code in Visual Studio 2022. Here I have to add Activeds.lib, adsiid.lib to make the API work. After generating the .dll file to load this in Java System.load() using JNI Concept.
This is the C++ code I used in Visual Studio 2022...
#include <iostream>
#include <jni.h>
#include<string.h>
#include "com_library_ldap_JNIHelper.h"
#include <Iads.h>
#include <adshlp.h>
#include "activeds.h"
#include <windows.h>
#include <stdlib.h>
using namespace std;
JNIEXPORT jstring JNICALL Java_com_library_ldap_JNIHelper_authenticateUser
(JNIEnv* env, jclass, jstring uname, jstring pwd) {
const char* uName = env->GetStringUTFChars(uname, NULL);
std::string username = std::string(uName);
const char* pwd1 = env->GetStringUTFChars(pwd, NULL);
std::string password = std::string(pwd1);
env->ReleaseStringUTFChars(uname, uName);
env->ReleaseStringUTFChars(pwd, pwd1);
IADs* pObject;
wchar_t wname[20];
mbstowcs(wname, username.c_str(), username.length());
LPWSTR szUsername = wname;
wchar_t wpwd[20];
mbstowcs(wpwd, password.c_str(), password.length());
LPWSTR szPassword = wpwd;
HRESULT hr;
string uAdmin = "Admin";
string uUser = "User";
string uType;
bool res = false;
if (uAdmin == "Admin") {
hr = ADsOpenObject(L"LDAP://test.local/CN=Users,DC=test,DC=local",
szUsername,
szPassword,
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);
if (SUCCEEDED(hr))
{
// Use the object.
res = true;
// Release the object.
pObject->Release();
}
if (res == true) {
uType = "Admin";
//return env->NewStringUTF(uType.c_str());
}
else
uType = "NotFound!!!";
}
else if (uUser == "User") {
hr = ADsOpenObject(L"LDAP://test.local /CN=Vimaluser,OU=Users,OU=Vimal,DC=test,DC=local",
szUsername,
szPassword,
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);
if (SUCCEEDED(hr))
{
// Use the object.
res = true;
// Release the object.
pObject->Release();
}
if (res == true) {
uType = "User";
}
else {
uType = "NotFound!!!";
}
}
return env->NewStringUTF(uType.c_str());
}
below is the error I'm getting...
Build started...
1>------ Build started: Project: Adsi-JNI, Configuration: Release x64 ------
1>Adsi-JNI.cpp
1> Creating library C:\Users\Vimal\Desktop\VS Code\Adsi-JNI\x64\Release\Adsi-JNI.lib and object C:\Users\Vimal\Desktop\VS Code\Adsi-JNI\x64\Release\Adsi-JNI.exp
1>Adsi-JNI.obj : error LNK2019: unresolved external symbol ADsOpenObject referenced in function Java_com_library_ldap_JNIHelper_authenticateUser
1>Adsi-JNI.obj : error LNK2001: unresolved external symbol IID_IADs
1>E:\Java Program\Library System\src\main\java\com\library\ldap\lib\ActiveDS.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>E:\Java Program\Library System\src\main\java\com\library\ldap\lib\adsiid.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>C:\Users\Vimal\Desktop\VS Code\Adsi-JNI\x64\Release\Adsi-JNI.dll : fatal error LNK1120: 2 unresolved externals
1>Done building project "Adsi-JNI.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And I wrote a code for server connection, but i don't know whether the code is correct for server connection or not. Please help me to solve this problem and also share what are all the things related to this. Thanks in advance.

Fatal error when using jni

I'm trying to use jni to call java methods from c++. actually more of a callback (java -> c++ -> java)
I've checked the c++ program for errors by testing it in .exe (c++ -> java)
The program works perfect in visual studio. but fails and crashes when I convert it to dll and use it in java.
I think it's related to jvm.dll because I had to include it to my visual studio project.
c++:
#include <stdio.h>
#include <jni.h>
#include <string.h>
#include "Inter.h"
JNIEnv* create_vm(JavaVM ** jvm) {
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
options.optionString = "-Djava.class.path=C:\\Users\\SolidSnake\\workspace\\Test\\bin"; //Path to the java source code
vm_args.version = JNI_VERSION_1_8;
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;
}
void callMethod() {
JNIEnv *env;
JavaVM * jvm;
env = create_vm(&jvm);
jclass m;
jmethodID test;
m = env->FindClass("Main");
test = env->GetStaticMethodID(m,"callbackFromC","()V");
env->CallStaticVoidMethod(m,test);
}
java:
public final class Main {
public native int callToC();
public static void callbackFromC() {
System.out.println("Hello from C!");
}
public static void main(String[] args) {
System.loadLibrary("Test");
new Main().callToC();
}
}
crash:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007f9aed32ff8, pid=4016, tid=8228
#
# JRE version: Java(TM) SE Runtime Environment (8.0_11-b12) (build 1.8.0_11-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.11-b03 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [Test.dll+0x1a08] JNIEnv_::FindClass+0x28
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
Here's how the program goes (j:callToC) -> (c:callMethod) -> (j:callbackFromC)
I had the same problem a couple of days ago and I have found a solution (maybe not the best one) of the problem and now I proud to share it (this one probably would be useful for somebody in the future).
Here is my code that worked well when I had only two projects (C++->Java), that is, the C++-project invoking Java-methods:
void JVM:: Init( const std:: string& javaClassesPath ) {
std:: string optionString = ( std:: string("-Djava.class.path=") + javaClassesPath );
JavaVMOption options_;
options_.optionString = const_cast<char*>( optionString.c_str() );
// initializing of JVM initial arguments:
JavaVMInitArgs arguments_;
memset( &arguments_, 0, sizeof( arguments_ ) );
arguments_.version = JNI_VERSION_1_6;
arguments_.nOptions = 1;
arguments_.options = &options_;
arguments_.ignoreUnrecognized = 0;
// creating JVM:
long status = JNI_CreateJavaVM( &jvm_, (void**)&env_, &arguments_ );
if ( status == JNI_ERR )
throw std:: exception("Error: unable to create Java Virtual Machine.\n");
FindClass("ChartBuilder");
}
This code was being invoked the following way:
JVM JChartBuilder( "D:\\Java Src\\Chart\\Chart.jar" ); // the path that will be used as classpath when creating VM (unless this path is specified it will be unable to find the class ChartBuilder)
Then (Java->C++Java)
When I got the third project (Java) that must invoke methods from that C++-project using JNI, I faced the problem that I cannot create VM when one has already run in the current process. But we can attach to existing VM! So, the code listed above can be changed to suit this requirements:
void JVM:: Init( const std:: string& javaClassesPath )
{
// initializing of JVM options:
std:: string optionString = ( std:: string("-Djava.class.path=") + javaClassesPath );
JavaVMOption options_;
options_.optionString = const_cast<char*>( optionString.c_str() );
// initializing of JVM initial arguments:
JavaVMInitArgs arguments_;
memset( &arguments_, 0, sizeof( arguments_ ) );
arguments_.version = JNI_VERSION_1_6;
arguments_.nOptions = 1;
arguments_.options = &options_;
arguments_.ignoreUnrecognized = 0;
// creating JVM or attaching to JVM:
long status;
/* is there any running VMs in the process? */
JavaVM* createdVMs[1] = { nullptr };
jsize numberOfVMs;
status = JNI_GetCreatedJavaVMs(createdVMs, 1, &numberOfVMs);
/* END OF is there any running VMs in the process? */
if( numberOfVMs != 0 )
{ // if VM already runs:
jvm_ = createdVMs[0]; // get the VM
jvm_->AttachCurrentThread((void**)&env_, NULL); // attach to the VM
}
else
{ // if VM does not run:
status = JNI_CreateJavaVM( &jvm_, (void**)&env_, &arguments_ );
}
if ( status == JNI_ERR )
throw std:: exception("Error: unable to create Java Virtual Machine.\n");
FindClass("ChartBuilder");
}
But that's not all yet. When running main Java-project (the first in Java->C++->Java), I got the following problem: unable to find specified class ChartBuilder. That is I attached to existing VM, but this virtual machine does not know about the path to this class. Remember, that when I created VM in C++, I specified the path explicitly. To solve this problem I have now to specify the classpath additionaly when running main Java-project.
That is all. The only thing I don't like is the need to specify the classpath to ChartBuilder when running main Java-project, but this one is not critical for me, though I'd like to find out how to avoid this need. Maybe somebody knows?

JNI JVM Invocation Classpath

I am writing a small C program using Cygwin that launches a Java Virtual Machine (libraries I am using require POSIX environment). So far, I have been able to get it to work as long as I place all of my classes in the same folder as the executable. However, I want to specify an actual JAR file that contains the application I want to run. This does not seem to work though, FindClass simply returns a null. I've narrowed it down to a problem with the classpath setting, like I said, because I can extract my jar file in the same directory as the executable and it will work. Here is a subset of my code:
I've loosely been following this guide: http://www.inonit.com/cygwin/jni/invocationApi/
int main( int argc, char *argv[] )
{
void* jvmDllHandle;
JNIEnv* jenv;
JavaVM* jvm;
JavaVMInitArgs args;
JavaVMOption options[1];
jclass cls;
jmethodID mainMethod;
jobjectArray appArgs;
jstring arg0;
assert( cygwin_internal( CW_SYNC_WINENV ) != 1UL );
jvmDllHandle = LoadLibrary( "c:\\Path\\To\\Application\\jre\\bin\\server\\jvm.dll" );
createJavaVM = dlsym( jvmDllHandle, "JNI_CreateJavaVM" );
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=c:\\Path\\To\\Application\\TheJarFile.jar";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
createJavaVM( &jvm, (void **) &jenv, &args );
cls = (*jenv)->FindClass( jenv, "some/package/MainClass" );
assert( cls != NULL ); // This fails.
/// Omitted...
return 0;
}
Tried using -classpath and -cp
int main( int argc, char *argv[] )
{
void* jvmDllHandle;
JNIEnv* jenv;
JavaVM* jvm;
JavaVMInitArgs args;
JavaVMOption options[1];
jclass cls;
jmethodID mainMethod;
jobjectArray appArgs;
jstring arg0;
assert( cygwin_internal( CW_SYNC_WINENV ) != 1UL );
jvmDllHandle = LoadLibrary( "c:\\Path\\To\\Application\\jre\\bin\\server\\jvm.dll" );
createJavaVM = dlsym( jvmDllHandle, "JNI_CreateJavaVM" );
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-classpath c:\\Path\\To\\Application\\TheJarFile.jar";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
createJavaVM( &jvm, (void **) &jenv, &args );
cls = (*jenv)->FindClass( jenv, "some/package/MainClass" );
assert( cls != NULL ); // This fails.
/// Omitted...
return 0;
}
How am I specifying the classpath incorrectly?
On x86-64, the Oracle Windows JDK headers define jint as long. This is 32 bits with Microsoft compilers (which the Oracle JDK is written for) but 64 bits with Cygwin gcc. Since JavaVMInitArgs contains some fields of this type, its binary layout is changed by this discrepancy.
I worked around this by providing a local jni.h header:
#include "stdint.h"
#define __int64 int64_t
#define long int32_t
#include "jni_md.h"
#undef long
#include_next "jni.h"
I'm including only jni_md.h within the scope of the long redefinition because it doesn't include any other headers, whereas jni.h includes a couple of standard headers which we would not want to be affected as well.
To ensure this is always included ahead of the Oracle header, use the -I compiler option to add its directory to the #include path.

Loading dynamic libraries using JNI and C++ with linux

Recently I'm learning JNI to execute C code. Of course I did basic examples that were on the web. Now Im trying to load a C library that makes dynamic library loading (dlopen). But Im fighthing with an error. Im posting my Java code, C ++ code and the error.
My Java Class
/**
*
* #author glassfish
*/
public class MediationJniWeb {
public String library ;
static {
System.loadLibrary("-core-web");
}
/**
*
* #param library name of mediation core library [32]
* #param method name of method to be executed [128]
* #param parameters parameters of method [10240]
* [partype1,value1,...,valuen]...[partypen,value1,..,valuen]
* #return
*/
private native String execute();
public static void main(String args[]) {
//new MediationJniWeb().callToFunction(null, null, null) ;
MediationJniWeb jniWeb = new MediationJniWeb();
jniWeb.library = "libtest.so" ;
System.out.println(jniWeb.execute());
}
}
I generate .class file with
javac MediationJniWeb
and I generate .h File with
javah -jni MediationJniWeb
my MediationJniWeb.h file is
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MediationJniWeb */
#ifndef _Included_MediationJniWeb
#define _Included_MediationJniWeb
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: MediationJniWeb
* Method: execute
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_MediationJniWeb_execute
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
and my MediationJniWEb.cpp file
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <dlfcn.h>
#include <iostream>
#include "MediationJniWeb.h"
using namespace std ;
typedef void (*test)(string);
/*
* Class: MediationJniWeb
* Method: execute
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_MediationJniWeb_execute
(JNIEnv * env, jobject obj){
const char * str_library ;
jfieldID fid_library ;
jstring jstr_library ;
jboolean isCopy ;
jclass cls = env->GetObjectClass( obj) ;
fid_library = env->GetFieldID( cls,"library", "Ljava/lang/String;");
jstr_library = ( jstring )env->GetObjectField( obj,fid_library);
str_library = env->GetStringUTFChars( jstr_library, &isCopy) ;
void* handle = dlopen(str_library, RTLD_NOW); // open libtest.so
if ( 0 == handle ) {
cout << "failed to load 'libtest.so'. " << dlerror () <<endl;
exit(1);
}
test t = (test)dlsym(handle, "_Z8testfuncSs"); // run default method
if ( 0 == t ) {
cout << "failed to load 'testfunc()'." << endl;
exit(1);
}
t("Hello, World!");
dlclose(handle);
/*
*/
return env->NewStringUTF( str_library); // I just return library name just for fun
}
}
I compile with
g++ -shared -fpic -I//include/ -I//include/linux/ MediationJniWeb.cpp -o lib-core-web.so MediationJniWeb.cpp -ldl
this generate lib-core-web.so file. Then I copy this to $HOME/lib and I configure
LD_LIBRARY_PATH=$HOME/lib
Now I create my library libtest.so that is going to be executed by lib-core-web.so
I create file for shared libray mylib.cpp
#include <iostream>
#include <string>
using namespace std;
void testfunc(string s)
{
cout << s << endl;
}
I compile this that is going to work as a shared library with
g++ -shared -fpic -o libtest.so mylib.cpp
This command generates libtest.so file.. and also, I copy it to $HOME/lib
That's all I do to call from JNI to a C++ library to load a dynamic library.
when I execute MediationJniWeb java class I'm having this error
failed to load. libtest.so: cannot open shared object file: No such
file or directory
What do I have to do with libtest.so ?? where do I have to put it ?
I have on mind that configuring only LD_LIBRARY_PATH variable with right path, JVM should know where to find all needed libraries to be loaded.
Please help with your comments and let me know where my mistakes are.
Thanks in advance !
A simple thing I have done
instead of
jniWeb.library = "libtest.so"
library parameter to be loaded I declared
jniWeb.library = "/home/myuser/lib/libtest.so"
And it worked !

calling java function from c using jni

I'm writing a simple program to call a Java function from my C program.
Following is my code:
#include <jni.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
JNIEnv* create_vm() {
JavaVM* jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString - "-Djava.class.path=/home/chanders/workspace/Samples/src/ThreadPriorityTest";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = JNI_FALSE;
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
return env;
}
void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
helloWorldClass = (*env)->FindClass(env, "InvocationHelloWorld");
mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");
applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);
(*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
}
int main() {
JNIEnv* env = create_vm();
invoke_class(env);
}
I'm compiling the above program using:
gcc -o invoke -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux -L$JAVA_HOME/jre/lib/amd64/server/ ThreadPriorityTest.c
and i'm getting the following error:
/tmp/ccllsK5O.o: In function `create_vm': ThreadPriorityTest.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM' collect2: ld returned 1 exit status
I'm not really sure what is causing this problem
UPDATE 1
Included the -ljvm in the command line and then got a undefined reference to FUNCTION_NAME
I'm running it on Rhel 6.2
You've got the path to the Java library (the -L option), but not the library itself. You need to include -ljvm on the link line as well.

Categories