Solved

Linking problem... URGENT

Posted on 2003-11-17
24
623 Views
Last Modified: 2012-06-27
Hi,
 i am trying to link a file, c:\java\1421\bin\jvm.lib to my program, called batchuser.c. Here is the makefile:

BATCH=C:\Java\1421\bin\jvm.lib

batchuser.exe: batchuser.obj $(BATCH)
    ilink32 /Tpe/ap/c/x/Gn c0x32 batchuser surscf,batchuser,, \
         noeh32 $(BATCH) import32 cw32

batchuser.obj: batchuser.c
    bcc32 -IC:\Borland\Bcc55\Include -c -RT- -x- batchuser.c


the surscf.obj file is in the working directory. it never changes, so i did not include compilation instructions in the makefile (just background). I am trying to run JNI in my C program. in order to do this, i need to properly link the jvm.lib program to my file. this compiles, but when i go to run the JVM, i get an error. i have tried all kinds of examples that work, so that leads me to believe that there is a problem with the linking of this file. One possible source of error is that i have to use the COFF2OMF utility supplied by borland to make jvm.dll compatible with my borland compiler. i then used the implib program to generate jvm.lib. It didn't work. I am compiling with Borland 5.5 compiler from the command line. I am in Windows 2000. Any ideas of how to get these files to link properly? if it compiles without error, does that mean they are linked correctly(that sounds like a stupid question now that i write it, but i will ask nonetheless). PLEASE HELP! THIS IS URGENT! Thanks. I will give the points to anyone who can help me enough to get this program up and running.
0
Comment
Question by:pedron
  • 8
  • 7
  • 4
  • +1
24 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 9765842

Hi pedron,

1)  Are you trying to run C programs from JAVA or the other way around?

2)  What error are you getting?

3)  Here's a link to a zip file that has several programs that utilize both JAVA and C.  The zip file also contains several make files:

http://66.136.157.99/webmaster/sigs/c/files/jniexamples.zip


Kent
0
 

Author Comment

by:pedron
ID: 9765911
Kent,

1)  Are you trying to run C programs from JAVA or the other way around?
 i have a program, batchuser.exe, that i am trying to run a java class from using JNI. C calls JAVA
2)  What error are you getting?
basically, i start up the program, it runs until i get to the part where i initialize the JVM. the JNI_CreateJavaVM function is always returning -1. I am almost 100% positive that it isn't the parameters i pass to the function or how i call it. i went to forums.java.sun in the 'Native Methods' forum and tried pasting all the working JNI initializations, but not even the simplest worked. I even tried the initialization on the JNI tutorial. I have used JNI a lot in the past, but on OS/2. I am porting my program over to windows. I just need to get the JVM to iniialize properly.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9766046

Here's the JVM initialization code from a sample provided by Sun.  Does you code come pretty close to this?

Kent

main() {
    JNIEnv *env;
    JavaVM *jvm;
    JDK1_1InitArgs vm_args;
    jint res;
    jclass cls;
    jmethodID mid;
    jstring jstr;
    jobjectArray args;
    char classpath[1024];


    /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
    vm_args.version = 0x00010001;

    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    /* Append USER_CLASSPATH to the end of 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);
    if (res < 0) {
        fprintf(stderr, "Can't create Java VM\n");
        exit(1);
    }
0
 

Author Comment

by:pedron
ID: 9766153
i am using the JVM version 1.4, this is for version 1.1 which is totally different and really antequated. the only reason i bring this up is that in order to get this to work i would have to link to javai.lib which is deprecated. Do you have some default code for version 1.3 or 1.4? thanks again...
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9766229

There is a macro/define called JNI_VERSION_1_4.  Try setting vm_args.version to that.


kent
0
 

Author Comment

by:pedron
ID: 9766557
here are my initialization arguments:
these are the arguments i started with, they worked in OS/2, but they won't in windows (look below for more):
      jint rc = 0;
    JavaVMInitArgs  vm_args;
    JavaVMOption options[4];
   // {
       char jnipath[230] = "PATH=";
       char *path;
       //putenv("PATH=C:\\java131\\jre\\bin");
       putenv("PATH=C:\\Java\\1421\\bin");
       path = getenv("PATH");
       printf("path: %s\n",path);
       if (path != NULL)
       {
          strcat(jnipath, path);
          putenv(jnipath);
       }
    }

    options[0].optionString = "-Djava.class.path=c:\surs\program\practice";
    options[1].optionString = "-Xms100m";
    options[2].optionString = "-Xmx100m";
    options[3].optionString = "-Xss50m";
    //memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_4;
    vm_args.nOptions = 4;
    vm_args.options = options;
printf("1\n");

    rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);


Then, i got this piece on the JNI tutorial on sun.com; it doesn't work either. i even tried taking out the heap and stack size arguments. nothing. the arguments seem right, that is why i believe i am not linking correctly.

    int rc = 0;
    JavaVMOption options[3];
      JavaVMInitArgs vm_args;
      JavaVM *jvm;
      JNIEnv *env;

      options[0].optionString = strdup("-Djava.compiler=NONE");
      options[1].optionString = strdup("-verbose:jni");
      options[2].optionString = strdup("-Djava.class.path=c:\surs\program\practice");
    options[3].optionString = strdup("-Xms100m");
    options[4].optionString = strdup("-Xmx100m");
    options[5].optionString = strdup("-Xss50m");
      vm_args.version=JNI_VERSION_1_2;
      vm_args.options = options;
      vm_args.nOptions = 6;
      vm_args.ignoreUnrecognized = JNI_FALSE;
printf("1\n");
    rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9766616

When you set up your "PATH" variable, you omit all of the current "PATH" settings and just set it to the (presumed) path to your JAVA engine.  Is this intentional?  Maybe the following will set PATH to a "more usable" value?

       char oldpath[1024]";
       char newpath[1024];
       char *path;

       oldpath = getenv("PATH");
       strcpy (newpath, "PATH=");
       strcat (newpath, oldpath);
       strcat (newpath, ";C:\\Java\\1421\\bin");
       putenv(newpath);
       printf("path: %s\n",newpath);


Kent

0
 

Author Comment

by:pedron
ID: 9766720
i tried that. It still did not initialize. Here is the output

path: PATH=c:\java\1421\bin;c:\borland\CBUILD~1\Projects\Bpl;c:\borland\CBUILD~1\Bin;C:\Inprise\vbroker\bin;C:\PROGRA~1\Borland\CBUILD~2\Projects\Bpl;C:\PROGRA~1\Borland\CBUILD~2\Bin;C:\WINNT\SYSTEM32;C:\WINNT;C:\WINNT\SYSTEM32\WBEM;C:\WINNT\SYSTEM32\NLS;C:\WINNT\SYSTEM32\NLS\ENGLISH;C:\PROGRA~1\ULTRAE~1;C:\JDK1.3.0_02\BIN;C:\VALAR\MOVEFILES_VALAR;C:\PROGRAM FILES\BORLAND\CBUILDER3\BIN;C:\cygnus\cygwin~1\h-i586~1\bin;;w:\IMNnq_NT;w:\upistaff\peter\ibmcpp\bin;c:\program files\devstudio\sharedide\bin\ide;c:\program files\devstudio\sharedide\bin;c:\program files\devstudio\vc\bin;C:\Program Files\Microsoft Visual Studio\VSS\Win32;C:\Java1412\bin;Z:.;;C:\Java\1421\bin
1
RC = -1
Problems creating JVM! RC = -1, env = 0

from this code:

har oldpath[1024];
             char newpath[1024];
             char *path;

             strcpy(oldpath, getenv("PATH"));
             strcpy (newpath, "PATH=");
             strcat (newpath, oldpath);
             strcat (newpath, ";C:\\Java\\1421\\bin"); //this path is used to find .dll files, right?
             putenv(newpath);
       printf("path: %s\n",newpath);

    options[0].optionString = "-Djava.class.path=c:\surs\program\practice"; //this path is ONLY used to find JAVA Classes, right?
    options[1].optionString = "-Xms100m";
    options[2].optionString = "-Xmx100m";
    options[3].optionString = "-Xss50m";
    //memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_4;
    vm_args.nOptions = 4;
    vm_args.options = options;
   printf("1\n"); //for debugging purposes

    rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

   printf("RC = %i\n",rc);


    if(rc != 0) {
            printf("Problems creating JVM! RC = %i, env = %i \n",rc,(int)env);
            exit(6);
      }//end if


ps-most of the questions I ask in the comments i know the answers too, i just want to see if i can find an assumption i might be making that could be throwing this out of whack. it is SOO strange that it won't work!
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9766916

Your path statement already contains "C:\Java\1421\bin" so all of the getenv()/putenv() code is unnecessary (for now).  I do find it curious that two of the three ...\Java\... paths use subdirectory "1421" and one uses "1412".


At this point I have to defer to someone with more experience than I.  I don't know either.
Kent
0
 

Author Comment

by:pedron
ID: 9767121
Well, i appreciate your help. I will check that problem with the path. Probably just a stupid mistake. My path is ugly. i would like to clean it up, but don't really have the time to. Thanks anyways! Any other takers?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:jinij
ID: 9777934
Hey..try using only one option...and your PATH should be right.....please check it up.

 
                int rc=0;
      JavaVM* jvm;
      JNIEnv* env;
      JavaVMInitArgs args;
      JavaVMOption options[1];
      
      args.version = JNI_VERSION_1_4;
      args.nOptions = 1;
      options[0].optionString = "-Djava.class.path=c:\\surs\\program\\practice";
      args.options = options;
      args.ignoreUnrecognized = JNI_FALSE;

      rc  = JNI_CreateJavaVM(&jvm, (void **)&env, &args);
0
 

Author Comment

by:pedron
ID: 9779986
MAN! it just REFUSES to work! frustration++!

ok, i want just a basic JVM to start. here is my totally basic program:

#include "C:\Java\1421\include\jni.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
                   int rc=0;
           JavaVM* jvm;
           JNIEnv* env;
           JavaVMInitArgs args;
           JavaVMOption options[1];

           args.version = JNI_VERSION_1_4;
           args.nOptions = 1;
           options[0].optionString = "-Djava.class.path=c:\\surs\\program\\practice";
           args.options = options;
           args.ignoreUnrecognized = JNI_FALSE;

     rc  = JNI_CreateJavaVM(&jvm, (void **)&env, &args);

      printf("RC = %i \n env = %i\n",rc,env);
 }


here is my current PATH:
PATH=c:\java\1421\jre\bin;c:\borland\CBUILD~1\Projects\Bpl;c:\borland\CBUILD~1\Bin;C:\Inprise\vbroker\bin;C:\PROGRA~1\Borland\CBUILD~2\Projects\Bpl;
C:\PROGRA~1\Borland\CBUILD~2\Bin;C:\WINNT\SYSTEM32;C:\WINNT;C:\WINNT\SYSTEM32\WBEM;C:\WINNT\SYSTEM32\NLS;C:\WINNT\SYSTEM32\NLS\ENGLISH;
C:\PROGRA~1\ULTRAE~1;C:\JDK1.3.0_02\BIN;C:\VALAR\MOVEFILES_VALAR;C:\PROGRAM FILES\BORLAND\CBUILDER3\BIN;C:\cygnus\cygwin~1\h-i586~1\bin;
;w:\IMNnq_NT;w:\upistaff\peter\ibmcpp\bin;c:\program files\devstudio\sharedide\bin\ide;c:\program files\devstudio\sharedide\bin;c:\program files\devstudio\vc\bin;
C:\Program Files\Microsoft Visual Studio\VSS\Win32;C:\Java\1421\bin;Z:.;

I have jvm.dll in the folder:c:\java\1421\bin, and I created a JVM.lib using implib.exe and it resides in c:\java\1421\lib

my makefile is:
BATCH=C:\Java\1421\lib\jvm.lib

batchuser.exe: batchuser.obj $(BATCH)
    ilink32 /Tpe/ap/c/x/Gn c0x32 batchuser surscf,batchuser,, \
         noeh32 $(BATCH) import32 cw32

batchuser.obj: batchuser.c
    bcc32 -IC:\Borland\Bcc55\Include -Lc:\java\1421\lib -c -RT- -x- batchuser.c

surscf.obj is in the current working directory. If you can help me just get the JVM rolling for this ever-so-basic example i will give you the points. I am
just scratching my head, no idea why it won't start up! thanks for your time!
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9780151

Hi pedron,

I've never used ilink32 (I'm and old Borland fan so tlink is my friend :) ) so forgive me if this sounds pretty stupid.

On the ilink32 statement, the first parameter is "/Tpe/ap/c/x/Fn c0x32 batchuser surfsc".  Does ilink32 change the separator so that the forward slash becomes a backward slash or is "/Tpe/ap/c/x/Fn" not a path/file name?


Kent
0
 

Author Comment

by:pedron
ID: 9780416
that is a parameter, a long, crazy parameter, to ILINK. i was given that parameter by a guy on Borland's newsgroups. He is one of those king daddys who knows EVERYTHING about
borland, so i trust that parameter, plus my stuff wouldn't compile until i put that in. It is not a hard path
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9786070
Are you getting errors while linking or during execution!?
Check if the lib is present in that path. Also, check if you have enough permissions to access that path+lib.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 100 total points
ID: 9788445

Hi pedron,

Sorry that this is taking so long.....  (And I hope that your boss is patient!)

Borland maintains a lot of newsgroups for users.  Since I've not been enough help to solve this, perhaps posting to the Borland group is appropriate.  They have several hundred groups, but I think that this link will get you to the right place.

http://newsgroups.borland.com/cgi-bin/dnewsweb?cmd=xover&group=borland.public.cpp.borlandcpp&utag=

If it's not the right place, one of the Team-B moderators will tell you where to post.  (Please don't cross post.  They do get quite upset about the same question simultaneously appearing on multiple forums.)


Kent
0
 

Author Comment

by:pedron
ID: 9789327
i appreciate all the help. I figured it out on my own. The solution was as follows:
I had my Jvm.dll in a directory c:\java\1421\bin, but it needed to be in <jre>/bin/classic for some reason. I found the hint on a small FAQ on java.sun.com. it was obscure, but i finally got it! once i switched all my linking to link to the jvm.dll in a <jre>/bin/classic
directory on my c: drive. (who knows where it came from, but it was there) and reran and it worked! I HATE JNI! what a pain! i could swear that sun actually wants it to fail, since they have such an anemic amount of documentation online. Anyways. I appreciate all the help!






MODERATORS: can I please give 100 points to KDO? thanks
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10400523
>MODERATORS: can I please give 100 points to KDO? thanks
The questioner in his last comment has indicated that he wants to give points to Kdo.
After looking at all the discussions, I think Kdo deserves the points and this can be PAQed after giving points to Kdo.

-ssnkumar
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10400580
Then, in your recommendation, it is told that points will be refunded! Then where is it told that Kdo will get his due points!?

-ssnkumar
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10400681
I didn't know about the tool!!
Anyway, thanks for considering my comments also as deserving a response:-))

-ssnkumar
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

17 Experts available now in Live!

Get 1:1 Help Now