• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 861
  • Last Modified:

Calling Java from VB6

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!
  • 5
  • 5
  • 2
  • +1
3 Solutions
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
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

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 http://www.xmlrpc.com/
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

amp834Author Commented:
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.
>>cehj, thanks.  If JNI doesn't work out, I will try your suggestion.

Good luck
amp834Author Commented:
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.
amp834Author Commented:
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.
amp834Author Commented:
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

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

amp834Author Commented:
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.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now