Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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 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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 2142728
Edited text of question.

Accepted Solution

jasbro earned 800 total points
ID: 2143014
You need to set your 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 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