Calling Java from VB6

Posted on 2009-02-17
Last Modified: 2012-05-06
I have some Java applications that I want to call from VB6.  I can write additional code in Java to make the interface easy, but will call my Java functions, passing it strings or other parameters if possible, from VB6 (not VB.NET).

Does anyone know what is easy to pass across to a Java application?  Can I make a COM object in Java, or is that too problematic?  Or just some pipe to talk to it?

I am very familiar with VB and C++, and marginally familiar with Java.  All suggestions will be highly appreciated!
Question by:amp834
    LVL 86

    Assisted Solution

    You can CreateProcess using the java executable and the classname as the chief parameter (possibly classpath too) and use its stdin to give it input
    LVL 92

    Accepted Solution

    you should be able to create a DLL that creates a vm and allows the calls you need using JNI
    you can then call that DLL from your code
    LVL 3

    Assisted Solution

    do you want to exchange data between a VB6 application and java application, or do you just want to start a java application with some parameters ?

    in the first case, take a look at
    you can start an xmlrpc server in your java application, and use a xmlrpc client (com component) in your VB6 app.

    in the second case, the easiest way is to use CreateProcess to start your java app with all required parameters.
    LVL 92

    Expert Comment

    JNI would allow you to call your java methods directly (via JNI DLL)


    Author Comment

    Thanks, objects.  I am looking at JNI.

    lilian-arnaud, thanks for the xmlrpc reference.  the site is too complex, as if i'm building an airplane!  is there a simple intro?

    cehj, thanks.  If JNI doesn't work out, I will try your suggestion.

    I will try JNI first, then the other suggestions if necessary, and will report back in a few days.  Thanks for all the suggestions.
    LVL 86

    Expert Comment

    >>cehj, thanks.  If JNI doesn't work out, I will try your suggestion.

    Good luck

    Author Comment

    objects, I'm having trouble finding enough info about JNI.  Any good references?  (I downloaded jni.pdf from Sun)

    For example, I want to call a java routine and pass it a few strings, one of them a large unicode string (BSTR from VB).  Is there an efficient way of doing this?

    I am calling it from VB, but can write an intermediary in C/C++ if necessary.

    If these are difficult, I may be able to batch everything into a file and have the Java app read from a file.
    LVL 92

    Expert Comment


    Author Comment

    I'm sorry, I should have been more specific.  It's from VB6.  The C++ article was too convoluted!  (argggg.  I wish people would write more clearly!).  If you know of simpler articles, please let me know.  I'm doing a web search.
    LVL 92

    Expert Comment


    Author Comment

    I'm trying the JNI sample app, I create a class file HelloWorldApp.class, copy it to the directory for the C debug output, it doesn't find the class file in the "cls = ..." line of the c program.

    Does anyone have any ideas how to fix it?

    Java Code:
    //package helloworldapp;

    public class HelloWorldApp {
         * @param args the command line arguments
        public static void main(String[] args) {
            System.out.println("Hello World!  Woohoo!" + args[0]);


    C code:
    // CallJava2.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    for opening items:
    make sure env PATH has "C:\Program Files\Java\jre6\bin\client"
      so can find jvm.dll
    #include <jni.h>  
    //C:\Program Files\Java\jdk1.6.0_12\include
    //C:\Program Files\Java\jdk1.6.0_12\include\win32
    //lib:  "C:\Program Files\Java\jdk1.6.0_12\lib\jvm.lib"
    #define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
    #define USER_CLASSPATH "." /* where Prog.class is */
    //#define USER_CLASSPATH "C:\\Users\\amp\\Docs\\NetBeansProjects\\HelloWordApp\\dist\\HelloWordApp.jar"
    //#define USER_CLASSPATH "C:/Users/amp/Docs/NetBeansProjects/HelloWordApp/dist/HelloWordApp.jar"
    //#define USER_CLASSPATH "C:\\Users\\amp\\Docs\\NetBeansProjects\\HelloWordApp\\build\\classes" //\helloworldapp
    //#define USER_CLASSPATH "C:\\Users\\amp\\Docs\\NetBeansProjects\\HelloWordApp\\build\\classes\\helloworldapp"
     /* Win32 version */
    #include <windows.h>
     void *JNU_FindCreateJavaVM(char *vmlibpath)
         HINSTANCE hVM = LoadLibrary(vmlibpath);
         if (hVM == NULL) {
             return NULL;
         return GetProcAddress(hVM, "JNI_CreateJavaVM");
    void main1() {
         JNIEnv *env;
         JavaVM *jvm;
         jint res;
         jclass cls;
         jmethodID mid;
         jstring jstr;
         jclass stringClass;
         jobjectArray args;
    	 //typedef int MyType1; //t_CreateJavaVM1(int arg1, int arg2);
    	 //typedef int MyType2;
    	 //typedef jint (*t_CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
     #ifdef JNI_VERSION_1_2
         JavaVMInitArgs vm_args;
         JavaVMOption options[1];
    	 //char * s1 = getenv("PATH");
         options[0].optionString =
             "-Djava.class.path=" USER_CLASSPATH;
         vm_args.version = 0x00010002;
         vm_args.options = options;
         vm_args.nOptions = 1;
         vm_args.ignoreUnrecognized = JNI_TRUE;
         /* Create the Java VM */
    	 //t_CreateJavaVM f1 = (t_CreateJavaVM)JNU_FindCreateJavaVM("C:\\Program Files\\Java\\jre6\\bin\\client\\jvm.dll");
    	 //res = (*f1)(&jvm, (void**)&env, &vm_args);
         res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
         JDK1_1InitArgs vm_args;
         char classpath[1024];
         vm_args.version = 0x00010001;
         /* Append USER_CLASSPATH to the default system class path */
         sprintf(classpath, "%s%c%s",
                 vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
         vm_args.classpath = classpath;
         /* Create the Java VM */
         res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
     #endif /* JNI_VERSION_1_2 */
         if (res < 0) {
             fprintf(stderr, "Can't create Java VM\n");
         //cls = (*env)->FindClass(env, "Prog");
    	 cls = (*env)->FindClass(env, "HelloWorldApp");
    	 //(*env)->GetStringUTFChars(env, "Hi There", NULL);
         if (cls == NULL) {
             goto destroy;
         mid = (*env)->GetStaticMethodID(env, cls, "main",
         if (mid == NULL) {
             goto destroy;
         jstr = (*env)->NewStringUTF(env, " from C!");
         if (jstr == NULL) {
             goto destroy;
         stringClass = (*env)->FindClass(env, "java/lang/String");
         args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
         if (args == NULL) {
             goto destroy;
         (*env)->CallStaticVoidMethod(env, cls, mid, args);
         if ((*env)->ExceptionOccurred(env)) {
    int main(int argc, char* argv[])
    	printf("Hello World!\n");
    	return 0;

    Open in new window

    LVL 92

    Expert Comment

    try moving your class into a package.
    and make sure the class is available in the classpath


    Author Closing Comment

    Thanks, everyone, for your suggestions.  I am going with JNI, I managed to get it working.  I will be posting some followup questions about Java, JNI, etc.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now