jni jdk1.2.2 classpath and env->FindClass

Posted on 1999-10-20
Medium Priority
Last Modified: 2013-11-23
Hi all
I am writing a cpp app using vc++6 on nt that calls java metode
using jni
the code is:
#include "stdafx.h"
#include <jni.h>
#include <string.h>
#include <windows.h>
JNIEnv *env;
JavaVM *jvm;
jmethodID mid;
jint ret;
jclass clazz;
JDK1_1InitArgs vm_args;
HANDLE          hLib = NULL;
typedef jint (*P_JNI_GetDefaultJavaVMInitArgs)(void *args);
int main(int argc, char* argv[])
      //char* classpath;
      char szClassPath [14];

      vm_args.version =JNI_VERSION_1_2; //0x00010002;
      if(JNI_GetDefaultJavaVMInitArgs (&vm_args))
           fprintf(stderr,"version not soported");
            return 1;

      // Tell the JVM where to find the application class files
            wsprintf  (szClassPath,"c:\\jdk1.2.2\\jre\\lib\\rt.jar;D:\\JavaClasses");
      vm_args.classpath = szClassPath;
      // Attempt to create a JVM instance.
      if ((ret = JNI_CreateJavaVM (&jvm, (void**)&env, &vm_args)) < 0)
            fprintf (stderr, "Can't create JVM.  Error: %ld\n", ret);
            return 1;

      if ((clazz = env->FindClass ("HelloWorld")) == 0) //this metoden is faild
            fprintf (stderr, "Can't locate the HelloWorld class.  Exiting...\n");
            return 1;
            // Attempt to locate the  class main method.

      if ((mid = env->GetStaticMethodID (clazz, "main", "([Ljava/lang/String;)V")) == 0)
            fprintf (stderr, "Can't locate the main method.  Exiting...\n");
            return 1;

      // Launch main method.

      env->CallStaticVoidMethod (clazz, mid);

      // Destroy the JVM instance.

      jvm->DestroyJavaVM ();
      return 0;

But I get the cant find class HelloWorld error (added remark above to show metode that field)
I think its somthing whith the classpath
in the documentation and samples they set the classpath to
classes.zip but in jdk1.2.2 this class was replaced with rt.jar I tried to set path to it (as shown above) but it didnt solved the prob(I tried all so whith no path to it and path "." allso no result)
well if someon can tell me what I am doing rong
(please no examp in jdk1.1.. and c class there planty of them on the web but cpp and jdk1.2.2 are in lack even in sun specs and site)
Question by:sgold

Author Comment

ID: 2142728
Edited text of question.

Accepted Solution

jasbro earned 800 total points
ID: 2143014
You need to set your classpath...it is assumed that if you are starting a JVM from within a program, you will know the classpath that you want to give to the JVM...I worked around this problem myself, by using getenv to get the system classpath, then pass it to the JVM.  Similar to:

   JavaVMInitArgs vm_args;         /*VM intialization arguments structure*/
   JavaVMOption options[3];      /*Array of options for use by vm_args*/
   jint res;                  /*result from VM creation*/
   char jvmclspth[2048];         /*class path to start jvm with*/

   strcpy(jvmclspth,"-Djava.class.path=");   /*This tells jvm that it is getting the class path*/
   strcat(jvmclspth,getenv("CLASSPATH"));   /*Concatenate the system classpath*/

   /*------Set Options for virtual machine*/
   options[0].optionString = "-Djava.compiler=NONE";   /*JIT compiler*/
   options[1].optionString = jvmclspth;            /*CLASSPATH*/

   /*------Set argument structure components*/
   vm_args.options = options;
   vm_args.nOptions = 2;
   vm_args.ignoreUnrecognized = JNI_TRUE;
   /*------Create VM*/
   res = JNI_CreateJavaVM(&G_JVM,(void**)&G_ENV,&vm_args);
   if (res < 0) {
      printf("Couldn't create JVM");

According to my documentation, the call to get the default vm init arguments, doesn't set the classpath for you.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question