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

  • SEARCH
  • Home
  • 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 7527867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:13:03+00:00 2026-05-30T04:13:03+00:00

I am trying to bypass the call JNIEnv->DefineClass method with all necessary parameter but

  • 0

I am trying to bypass the call JNIEnv->DefineClass method with all necessary parameter but it always returns java.lang.NoClassDefFoundError: ls.Hello (wrong name: ls/Hello) error when I try to call the method from java. I am not sure what is wrong.
Please see the JNI Code below:

#include<jni.h>
#include<ls_TestClassLoader.h>
#include<iostream>

using namespace std;
//JNIEnv *, jobject, jstring, jbyteArray, jint
JNIEXPORT jclass JNICALL Java_ls_TestClassLoader_defineClassX(JNIEnv *env, jobject obj, jstring name, jbyteArray data, jint len){
    JNIEnv &e=*env;
    jboolean isCopy;
    jclass cls= e.DefineClass(e.GetStringUTFChars(name,&isCopy),obj,e.GetByteArrayElements(data,&isCopy),500);
    return cls;
}

Here is the TestClassLoader code which calls the native

Also contents of txt file is binary class file

public class TestClassLoader extends ClassLoader {

    static {
        System.loadLibrary("TestClassLoader");
    }
    private native Class defineClassX(String name, byte[] b,  int len);
    public TestClassLoader() {
        super(TestClassLoader.class.getClassLoader());
    }
    @Override
    public Class<?> findClass(String className) {
        byte classByte[];
        Class result = null;
        result = (Class) classes.get(className);
        if (result != null) {
            return result;
        }
        try {
            return findSystemClass(className);
        } catch (Exception e) {
        }
        try {
            classByte = loadData(className);
            result = defineClassX(className, classByte, classByte.length);
            classes.put(className, result);

            return result;
        } catch (Exception e) {
           Logger.getLogger(TestClassLoader.class.getName()).log(Level.SEVERE, null, e);
           return null;
        }
    }

    private byte[] loadData(String name) {
        try {
            String res =  "/"+name.replace(".", "/") + ".txt";
            InputStream is = TestClassLoader.class.getResourceAsStream(res);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            int d = 0;
            while ((d = is.read()) != -1) {
                os.write(d);
            }
            return os.toByteArray();
        } catch (IOException ex) {
            Logger.getLogger(TestClassLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    private Hashtable classes = new Hashtable();
}

Any help is greatly appreciated.

Thanks

  • 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-05-30T04:13:04+00:00Added an answer on May 30, 2026 at 4:13 am

    The last parameter for DefineClass is length of the array, but you specified constant value. Although JNI docs not states that DefineClass throws NoClassDefFoundError, look at this bugreport.

    ClassLoader.defineClass(String name, byte[] b, int off, int len)
    throws NoClassDefFoundError if name does not match the name derived from
    the class data array.

    Maybe it is really something wrong with the name of class you specified. It is unclear what class name you passing. What acts as package name separator? "." or "/"?

    UPD.

    I’ve tried your code. The problem is in line below (Java code):

    result = defineClassX(className, classByte, classByte.length);
    

    The className you passing is "ls.Hello". But JNI requires all class names to be in the form "aaa/bbb/ccc/ClassName", not the "aaa.bbb.ccc.ClassName". Just replace . with / and it will load your class.

    Also note that you call GetStringUTFChars and GetByteArrayElements, but not balance them with ReleaseStringUTFChars and ReleaseByteArrayElements. It may lead to memory leak and unpredictable behavior.

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

Sidebar

Related Questions

Trying to keep all the presentation stuff in the xhtml on this project and
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I know the title is a bit vague. But what I'm trying to achieve
I'm trying to use the Accelerate framework on iOS to bypass the fact that
I'm trying to run an application with java 1.7.2 and its complaining it needs
I am trying to add a custom throws clause to a method definied by
I am trying to call a webservice from Websphere Portal server, however I Notice
I need help with trying to bypass/ignor a self signing certificate.. I have looked
I am trying to write a method to return the current location. While I'm
I am trying to bypass the use of a database table. Is there away

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.