Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:30:42+00:00 2026-06-17T08:30:42+00:00

I am embedding a JVM in an existing C++ application and need to register

  • 0

I am embedding a JVM in an existing C++ application and need to register implementation of native Java functions with a class.

Consider this simple class with native functions:

class Native {
  static {
    System.out.println("Class 'Native' static initializer called.");
  }

  public native int f(int i);
}

Inside the JVM I am running OSGi which is the reason I need to get the classes using Java code (using the correct class loader) instead of loading them from JNI. However, to keep this example simple, I have left out OSGi.

I have these four different Java methods for getting a jclass value in C++:

class Bridge {
  public Class<?> getNativeClass1() throws ClassNotFoundException {
    return getClass().getClassLoader().loadClass("org.example.Native");
  }

  public Class<?> getNativeClass2() throws ClassNotFoundException {
    return Class.forName("org.example.Native", false, getClass().getClassLoader());
  }

  public Class<?> getNativeClass3() throws ClassNotFoundException {
    final Class<?> clazz = getClass().getClassLoader()
                                     .loadClass("org.example.Native");
    clazz.getMethods();
    return clazz;
  }

  public Class<?> getNativeClass4() throws ClassNotFoundException {
    return Class.forName("org.example.Native", true, getClass().getClassLoader());
  }
}

To register the native function implementation in C++ for Native.f() I call:

JNIEnv* env = ...
jclass clazz = ...; // Calling one of the four methods above.
JNINativeMethod nativeMethod = {
  (char*) "f",      // Method name 'f'.
  (char*)  "(I)I;", // Signature 'int --> int'.
  (void*) f         // Pointer to C++ implementation of function.
};
env->RegisterNatives(clazz, &nativeMethod, 1);

Depending on which method I use for getting the Class<?> instance, I get different results:

  • getNativeClass1(): The static initializer is not executed in class Native when loading the class (but of course when creating an instance of the class) and the native implementation is not bound correctly. (Calling the native function in Java gives incorrect results or crashes the JVM.)
  • getNativeClass2(): Same as above.
  • getNativeClass3(): The static initializer is still not called in class Native when the class is loaded but the native implementation is bound correctly and I can call f() successfully.
  • getNativeClass3(): The static initializer is called in class Native when the class is loaded and the native implementation is bound correctly.

So it seems that ClassLoader.loadClass() loads the class in a way so it is not properly initialized and JNIEnv::RegisterNatives() will not work properly. However, calling Class.getMethods() will somehow initalize the class (without invoking the static initializer) so that binding the native methods works.

On the other hand, Class.forName(clazz, false, classLoader) seems to work exactly like Class.loadClass() returning an uninitialized Class instance.

Can anybody explain the difference between

  1. an uninitialized class like the one returned by getNativeClass1() and getNativeClass2()
  2. a partially initialized class like the one returned by getNativeClass3()
  3. a fully initialized class like the one returned by getNativeClass4()

and what is the most portable way to load a class before calling JNIEnv::RegisterNatives()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:30:42+00:00Added an answer on June 17, 2026 at 8:30 am

    So it seems that ClassLoader.loadClass() loads the class in a way so it is not properly initialized

    According to the documentation:

    loadClass(String): “Invoking this method is equivalent to invoking loadClass(name, false).”

    loadClass(String,boolean) (emphasis added): “If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(Class) method on the resulting Class object”

    Those two methods are intended for internal use by classloaders that need to do something between loading and linking. I’m not sure why loadClass(String) is marked public, but it arguably shouldn’t be.

    and what is the most portable way to load a class before calling

    Class.forName(), which uses the context classloader and ensures that the class is ready for use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the requirements for embedding help in a Java desktop application (or applet): Single
Embedding Jetty webserver into a Java application is supposedly easy; examples abound. No examples
I am embedding Lua in my C++ application. I have a class Foo which
When embedding a JavaScript interpreter (Rhino) into a Java application (to be able to
I'm embedding JRuby in Java, because I need to call some Ruby methods with
I'm embedding a VTK window in a Qt Application with QVTKRenderWindowInteractor. In this window,
Suppose I'm embedding Sun's JVM in a C++ application. Through JNI I call a
I'm embedding a JRE in an existing C application using the invocation API, and
I am embedding Tomcat 7 into my Java application. In the constructor I set
I'm embedding a flash swf into an html page and setting wmode=transparent. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.