9 Replies Latest reply: Jun 24, 2011 10:38 AM by 868282 RSS

    Starting with JNI using Eclipse

    868282
      Hi, I've been having tremendous problems with starting to use JNI.

      So far, I've tried using C++ with JNI, but it became too difficult because the tutorial I had was for C with JNI.
      I then got the eclipse C/C++ IDE then tried using JNI again... But I head to tell eclipse where to locate the JNI.H
      I went to my project->properties->C/C++ Build->Settings.
      Then I selected Directories which was under GCC C Comiler.
      Then I added the two directories:

      *1. JAVA_HOME/jdk1.6.0_25\include*
      *2. JAVA_HOME/jdk1.6.0_25\include\win32*
      where JAVA_HOME is the path to get to where I store my jdk.

      Anyways, I first of all wanted to see if JNI would work. And it seemed that It had. However I was having problems with the following function:

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

      I'm not quite sure what this function does, but I do know that it plays a role in implementing a JVM using JNI.

      When I try to build my code, I get an error: undefined reference to `_imp__JNI_CreateJavaVM@12'

      Well, to be honest, since I am new to all of this JNI coding, I really do not know anything about how I can fix this problem, probably because I've been having so much trouble getting started, and that I've never programmed in C. (I have in C++ though).
      __________________________________________________________________________________________________________________________________________________


      Basically, I want to ask:
      -How can I configure JNI with Eclipse C/C++ IDE?
      -How can I configure anything with JNI?
      -How can I configure a C compiler if none of the above will work?
      -How can I get rid of the error I am having? I did look into it and I was seeing I might need a "Jvm.dll". The only Jvm.dll I found were in my jre\bin in the client and server folders.
      -How can I successfully implement the JVM using JNI, so that I can create a custom launcher for program deployment?

      I'm sorry I really do not know anything about JNI, and to be honest I wish I did. I came to this forum after several hours of no luck configuring my computer to be able to program using JNI correctly. I really do appreciate any feedback, and I hope that this forum will actually have some traffic, compared to the Java.net forums I normally use.
      Thank you in advance, I hope I gave you enough details to the problems I am having with. And I do apologize if I further complicate the issue.

      Regards.

      Edited by: Nick on Jun 18, 2011 7:39 AM
        • 1. Re: Starting with JNI using Eclipse
          jschellSomeoneStoleMyAlias
          Nick wrote:
          So far, I've tried using C++ with JNI, but it became too difficult because the tutorial I had was for C with JNI.
          If you do not already have more than a novice experience level with C/C++ then you must start learning that.

          And that has nothing to do with Java nor JNI.
          I then got the eclipse C/C++ IDE then tried using JNI again... But I head to tell eclipse where to locate the JNI.H
          FIRST you figure out how to compile C/C++ code into a shared library.
          SECOND you figure out how to do the same in your IDE.
          • 2. Re: Starting with JNI using Eclipse
            868282
            Thanks for your help! I will be looking into C, as it seems that is more cross-platform than C++. If I'm wrong about that please say so.

            Just wanted to clarify. A shared library contains compiled code inside of a .dll or .so? depending on the platform.



            But anyways I'm one step closer to figuring this all out. Thanks again.
            • 3. Re: Starting with JNI using Eclipse
              jschellSomeoneStoleMyAlias
              Nick wrote:
              Thanks for your help! I will be looking into C, as it seems that is more cross-platform than C++. If I'm wrong about that please say so.
              I doubt that for platforms that you would normally be using Java and JNI on.

              Certainly isn't true for Windows, Linux, Solaris, AIX, OS-X.
              Just wanted to clarify. A shared library contains compiled code inside of a .dll or .so? depending on the platform.
              Correct.
              • 4. Re: Starting with JNI using Eclipse
                handat
                What are you really trying to do? Write C/C++ code to be compiled into a shared library which is called from Java through JNI, or wite a C/C++ launcher program that starts the JVM with custom parameters and settings? Your questions jump between the two things, so I question whether you may have picked the wrong technology (JNI) for what you want to do since you mention that you don't really understand what JNI is in the first place.
                • 5. Re: Starting with JNI using Eclipse
                  868282
                  Okay, to makes things more clear I am trying to make a custom luancher using C/C++ to invoke the JVM.
                  I did take a look at the Invocation API. (which I believe is a subgroup of JNI as a whole). So my guess was to come to the JNI forum.

                  The reason I had posted all those questions is because I was willing to take answer for any, as they would help me come closer to figuring out how to make a custom launcher.
                  • 6. Re: Starting with JNI using Eclipse
                    gimbal2
                    Nick wrote:
                    Okay, to makes things more clear I am trying to make a custom luancher using C/C++ to invoke the JVM.
                    I did take a look at the Invocation API. (which I believe is a subgroup of JNI as a whole). So my guess was to come to the JNI forum.

                    The reason I had posted all those questions is because I was willing to take answer for any, as they would help me come closer to figuring out how to make a custom launcher.
                    The invocation API it is then. Usually it is mentioned in conjunction with JNI, but they are really two separate things on opposite ends of the spectrum. JNI = use a native module in a Java application, invocation API = invoke the JVM from a native application.

                    http://download.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html


                    Because you use the invocation API in your native application, its a bit difficult to really get help for it here as it is kind of a rare requirement. Probably most questions you may have belong in a C/C++ forum.
                    • 7. Re: Starting with JNI using Eclipse
                      868282
                      Yea that probably is true, as I have been asking how to set up a C/C++ environment. But still, I was receiving errors from the JNI_CreateJavaVM function. I'm not saying that I only want to fix this error, because I probably have something set up wrong with my C compiler that is saying this won't work.

                      Thanks for the advice! I did not know there was a separate forum for the Invocation API. (As you can see I've been a member with this forum for a short period of time).
                      • 8. Re: Starting with JNI using Eclipse
                        gimbal2
                        Nick wrote:
                        Thanks for the advice! I did not know there was a separate forum for the Invocation API. (As you can see I've been a member with this forum for a short period of time).
                        Huh? Where did I say there was a separate forum for the invocation API?

                        Your problem is not with the function because you cannot even get it to build yet; your problem is that you don't know how to setup your compiler so it has all the required resources to compile/link your code. Basic C/C++ stuff that belongs in a C/C++ forum.

                        In any case, the invocation API will be in an external module, like a DLL or a shared library. Before the linker can find elements (functions for example) inside this external module you need to add a platform specific element to your project that tells the linker where it can find these elements. On windows for example you'll include a .lib file which holds all the bookkeeping of the DLL. Take a look in the 'lib' subdirectory of your JDK.
                        • 9. Re: Starting with JNI using Eclipse
                          868282
                          Oh my bad, I had misread what you posted. (about there being a separate forum).

                          Thanks again! So I suppose I should get things correct configured for C/C++ programming ;)