Click here to Skip to main content
15,880,037 members
Home / Discussions / Java
   

Java

 
Questionfull answer with code pls Pin
Member 159126962-Feb-23 12:06
Member 159126962-Feb-23 12:06 
AnswerRe: full answer with code pls Pin
jschell6-Feb-23 6:21
jschell6-Feb-23 6:21 
AnswerRe: full answer with code pls Pin
Dave Kreskowiak6-Feb-23 6:32
mveDave Kreskowiak6-Feb-23 6:32 
GeneralRe: full answer with code pls Pin
Andre Oosthuizen8-Feb-23 4:30
mveAndre Oosthuizen8-Feb-23 4:30 
AnswerRe: full answer with code pls Pin
Richard MacCutchan6-Feb-23 6:37
mveRichard MacCutchan6-Feb-23 6:37 
AnswerRe: full answer with code pls Pin
Gerry Schmitz6-Feb-23 6:39
mveGerry Schmitz6-Feb-23 6:39 
QuestionUsing JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding25-Jan-23 20:38
JohnCodding25-Jan-23 20:38 
AnswerRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
Richard MacCutchan25-Jan-23 22:31
mveRichard MacCutchan25-Jan-23 22:31 
Yes, this is the way I do it, but you can modify it any way you like to provide the path to the library:
C++
/*
 ******************************************************************************
 *
 *    Name     : cpptojava.cpp
 *
 *    Function : Basic Console application to call a Java class.
 *
 *    Created  : 09 Jun 2022
 *
 ******************************************************************************
 */

#include <Windows.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <filesystem> // requires -std:c++17

#include "jni.h"

//
// Find the path to the jvm.dll given the base path to the Java runtime (or JDK if installed)
//
HMODULE LoadDll(
    const char* jrePath
)
{
    const char* pszjvmname = "bin\\server\\jvm.dll";   // Java VM name
    HMODULE hJVMDLL = 0;

    std::filesystem::path jvmpath(jrePath);
    jvmpath /= pszjvmname;
    if (!std::filesystem::exists(jvmpath))
    {
        std::cerr << "JVM library " << jvmpath << " not found." << std::endl;
        // throw an exception 
    }

    // load the Java VM DLL into our address space
    // NB filesystem::path is Unicode
    hJVMDLL = LoadLibraryW(reinterpret_cast<PCWSTR>(jvmpath.c_str()));
    if (hJVMDLL != NULL)
    {
        std::clog << "jvm.dll loaded from: " << jvmpath << std::endl;
    }
    return hJVMDLL; // a Windows HANDLE to the DLL
}


//
// A function to load the Java VM and initialise the JNI interface
// There are a number of diagnostic messages to show progress
//
JNIEnv* AttachJVM(
    const char* jrePath,
    JavaVM* jvm,
    std::string strcwd
)
{
    HMODULE     hJVMDLL = LoadDll(jrePath);
    if (hJVMDLL == 0)
    {
        return nullptr;
    }
    typedef jint(JNICALL* fpCJV)(JavaVM**, void**, void*);
    fpCJV JNI_CreateJavaVM = (fpCJV)::GetProcAddress(hJVMDLL, "JNI_CreateJavaVM");

    // tell the JVM where to find the class
    JavaVMOption    options[2];
    std::stringstream ssoptions;
    ssoptions << "-Djava.class.path=";
    ssoptions << strcwd;
    std::string stropts = ssoptions.str();
    options[0].optionString = const_cast<char*>(stropts.c_str());
    options[1].optionString = const_cast<char*>("-verbose:jni");   // print JNI-related messages

    JavaVMInitArgs  vm_args; // JDK/JRE 6 VM initialization arguments
    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 1; // change to 2 for verbose output
    vm_args.ignoreUnrecognized = false;
    vm_args.options = options;
    for (int i = 0; i < vm_args.nOptions; ++i)
    {
        std::clog << "Options[" << i << "]: " << options[i].optionString << std::endl;
    }
    JNIEnv* env;            // pointer to native method interface
    jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if (res != 0)
    {
        std::cerr << "C++: JNI_CreateJavaVM returned: " << res << std::endl;
    }
    jint version = env->GetVersion();
    std::clog << "JVM Version: " << (version >> 16) << "." << (version & 0xffff) << std::endl;

    return env; // the pointer to the jvm dll
}


void CallClass(
    JNIEnv* jniEnv,
    const char* pszClassname,
    const char* pszMethodname,
    const char* pszSignature
)
{
    jclass jvmclass = jniEnv->FindClass(pszClassname);
    if (jvmclass != NULL)
    {
        std::cout << "C++: Found the class: " << pszClassname << std::endl;
        jmethodID jmid = jniEnv->GetStaticMethodID(jvmclass, pszMethodname, pszSignature);
        if (jmid != NULL)
        {
            std::cout << "C++: Found the method: " << pszMethodname << std::endl;
            jniEnv->CallStaticVoidMethod(jvmclass, jmid, NULL);
            std::cout << "C++: env->CallStaticVoidMethod complete" << std::endl;
        }
        else
        {
            std::cerr << "C++: Failed to find the method: " << pszMethodname << std::endl;
        }
    }
    else
    {
        std::cerr << "C++: Failed to find the class: " << pszClassname << std::endl;
    }
}


int main(
    int argc,
    const char** argv
)
{
    const char* jreDir = nullptr;

    // suppress debug messages
    std::clog.setstate(std::ios::badbit);
    for (argv++; argc > 1; ++argv, --argc)
    {
        if (*argv[0] == '-')
        {
            // handle any execution options ...
            if (strstr(*argv, "-d"))
                std::clog.clear();
        }
        else
        {
            jreDir = *argv;
        }
    }
    if (jreDir == nullptr)
    {
        std::cerr << "No root path provided for JVM." << std::endl;
        return 1;
    }
    JNIEnv* jniEnv = NULL;
    std::filesystem::path cwd = std::filesystem::current_path();
    JavaVM jvm;
    jniEnv = AttachJVM(jreDir, &jvm, cwd.string());
    if (jniEnv == NULL)
    {
        std::cerr << "C++: Failed to get Java environment" << std::endl;
        return 1;
    }
        CallClass(jniEnv, "CppToJava", "main", "([Ljava/lang/String;)V");

    // clean up
    jint jvmres = jvm.DestroyJavaVM(); // never returns ???
    std::clog << "C++: DestroyJavaVM and terminate: " << jvmres << std::endl;

}

GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding25-Jan-23 22:47
JohnCodding25-Jan-23 22:47 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
Richard MacCutchan26-Jan-23 0:02
mveRichard MacCutchan26-Jan-23 0:02 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding26-Jan-23 0:16
JohnCodding26-Jan-23 0:16 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding25-Jan-23 23:27
JohnCodding25-Jan-23 23:27 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
Richard MacCutchan26-Jan-23 0:05
mveRichard MacCutchan26-Jan-23 0:05 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding26-Jan-23 0:20
JohnCodding26-Jan-23 0:20 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
Richard MacCutchan26-Jan-23 0:28
mveRichard MacCutchan26-Jan-23 0:28 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding26-Jan-23 0:45
JohnCodding26-Jan-23 0:45 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
JohnCodding26-Jan-23 0:52
JohnCodding26-Jan-23 0:52 
GeneralRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
Richard MacCutchan26-Jan-23 1:44
mveRichard MacCutchan26-Jan-23 1:44 
AnswerRe: Using JNI without having to add jvm.dll path inside Environment Variables (User or System) Pin
jschell26-Jan-23 11:33
jschell26-Jan-23 11:33 
QuestionMemory usage for requested rows from database Pin
Valentinor13-Jan-23 0:18
Valentinor13-Jan-23 0:18 
AnswerRe: Memory usage for requested rows from database Pin
jschell15-Jan-23 8:27
jschell15-Jan-23 8:27 
GeneralRe: Memory usage for requested rows from database Pin
Valentinor16-Jan-23 20:44
Valentinor16-Jan-23 20:44 
GeneralRe: Memory usage for requested rows from database Pin
jschell17-Jan-23 3:24
jschell17-Jan-23 3:24 
AnswerRe: Memory usage for requested rows from database Pin
RedDk17-Jan-23 7:15
RedDk17-Jan-23 7:15 
QuestionHow to found a file by utilizing a variable for 'Startswith' in JAVA? Pin
MOHAMMAD ZAINUL ABIDEEN3-Jan-23 0:32
MOHAMMAD ZAINUL ABIDEEN3-Jan-23 0:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.