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 6617831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:45:04+00:00 2026-05-25T20:45:04+00:00

I am trying to create a Java virtual machine from C++ and invoke the

  • 0

I am trying to create a Java virtual machine from C++ and invoke the main method passing a String argument to the main method of the Java program. I am following this example found on Sun’s website: http://java.sun.com/docs/books/jni/html/invoke.html#11202

Here is the simple Java Program:

public class TestJNIInvoke
{
    public static void main(String[] args)
    {
    System.out.println(args[0]);
    }
}

Here is the C++ program I am using to (try to) invoke the JVM:

#include <jni.h>
#include <cstdlib>

using namespace std;

int main() 
{
     JNIEnv *env;
     JavaVM *jvm;
     jint res;
     jclass cls;
     jmethodID mid;
     jstring jstr;
     jclass stringClass;
     jobjectArray args;

     JavaVMInitArgs vm_args;
     JavaVMOption* options = new JavaVMOption[1]; //LINE 18 ERROR
     options[0].optionString = 
           (char*)&"-Djava.class.path=C:\\Program Files\\Java\\jdk1.7.0\\bin";
     vm_args.version = JNI_VERSION_1_6;
     vm_args.nOptions = 1;
     vm_args.options = options;
     vm_args.ignoreUnrecognized = false;

     /* load and initialize a Java VM, return a JNI interface
      * pointer in env */
     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); //LINE 26 ERROR

     if (res < 0) 
       {
         fprintf(stderr, "Can't create Java VM\n");
         exit(1);
       }
     cls = env->FindClass("TestJNIInvoke");
     if (cls == NULL) 
       {
         goto destroy;
       }

     mid = env->GetStaticMethodID(cls, "main",
                  "([Ljava/lang/String;)V");
     if (mid == NULL) 
       {
         goto destroy;
       }
     jstr = env->NewStringUTF(" from CPP!");
     if (jstr == NULL) 
       {
         goto destroy;
       }
     stringClass = env->FindClass("java/lang/String");
     args = env->NewObjectArray(1, stringClass, jstr);
     if (args == NULL) 
       {
         goto destroy;
       }
     env->CallStaticVoidMethod(cls, mid, args);

 destroy:
     if (env->ExceptionOccurred()) 
       {
          env->ExceptionDescribe();
       }
     jvm->DestroyJavaVM();
}

Anyway If I just compile the file with:

gcc -I"c:\Program Files\Java\jdk1.7.0\include" 
    -I"c:\Program Files\Java\jdk1.7.0\include\win32" -c TestJNIInvoke.cpp

It compiles fine, but when I try to compile and link:

gcc -I"c:\Program Files\Java\jdk1.7.0\include" 
    -I"c:\Program Files\Java\jdk1.7.0\include\win32" -g TestJNIInvoke.cpp

I get two weird errors that I don’t understand:

TestJNIInvoke.cpp:18: undefined reference to `operator new[](unsigned int)'
TestJNIInvoke.cpp:26: undefined reference to `_imp__JNI_CreateJavaVM@12'
collect2: ld returned 1 exit status

I marked the lines in the above code where the error is occuring, has anyone encountered this problem before?

Any ideas/links would be great

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-25T20:45:05+00:00Added an answer on May 25, 2026 at 8:45 pm

    First, don’t use gcc. By default, it assumes the code it’s handling is written in C. When you want it to compile or link C++ code, you should run g++. This will bring in the C++ standard headers and libraries.

    Second, you need to include the java libraries. Section 7.2.1 in the page you linked discusses this.

    Your command line should look more like this:

    g++ -I"C:\Program Files\Java\jdk1.7.0\include" -L"C:\Program Files\Java\jdk1.7.0\lib" -lthread -ljava TestJNIInvoke.cpp
    

    Note that you might have to add additional include (-I) or linker (-L) directories.

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

Sidebar

Related Questions

I am trying to create Java Virtual Machine in a cplusplus program using the
I'm trying to create a Qt main windows from Java using JNI directly and
I am trying to create a Response class in Java which has a method
I am trying to create a simple notepad in Java. This is the code
i am trying to create an RssFeed using java this is the class i
I am trying to create a java program that will take in a directory
I am using java trying to create a program. What I am trying to
I'm trying to create a Java program that is a front-end for iptables. To
Hey all I was trying to create a java program which will remain logged
I am trying to create XML from Java and am having problems with indenting.

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.