Solved

Help with a simple JNI example: MSC++, Win2K

Posted on 2003-11-07
10
689 Views
Last Modified: 2013-11-23

Hi.

I have tried numerous examples with JNI Visual C++ 6.0, Windows 2000 and java version "1.4.2" but the don't work.

They don't compile:

Linking...
Test.obj : error LNK2001: unresolved external symbol __imp__JNI_CreateJavaVM@12
Test.obj : error LNK2001: unresolved external symbol __imp__JNI_GetDefaultJavaVMInitArgs@4

I have another example that compilies yet when I try to run the Java program it fails.

Does anyone have a veru simple example I can BUILD and USE to get me started?

Thanks
0
Comment
Question by:COMRappa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:COMRappa
ID: 9703904
Ok.
So I compiled the next example in C not C++ and everything went well.

Now....I have a comipled JNI compatible C exe and the associated java example.
Without touching any of the code and having compiled both the Java and C programs the Java program will not run.

See below....

D:\Java\JNI\MyImpOfPrompt>java Prompt
Exception in thread "main" java.lang.UnsatisfiedLinkError: no MyImpOfPrompt in j
ava.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)
        at java.lang.Runtime.loadLibrary0(Runtime.java:788)
        at java.lang.System.loadLibrary(System.java:834)
        at Prompt.<clinit>(Prompt.java:9)

There must be some setup issue?
Or is it they way I'm holding my tounge....?

been dazed and confused for so long its not true.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 43 total points
ID: 9703979
>>MyImpOfPrompt

Is meant to be in java.library.path. Test what this path is by running this with the parameter "java.library.path":

public class Props {

      public static void main(String[] args){
            if(args.length > 0){
             for(int i = 0;i < args.length;i++){
                  System.out.println(System.getProperty(args[0]));
             }
            }
            else {
                  System.getProperties().list(System.out);
            }
      }
}
0
 

Assisted Solution

by:vitallis
vitallis earned 41 total points
ID: 9704700
There may be a number of problems why you get this error. If OS you use is MS Windows then the main mistakes are
- DLL functions are not export  type:(should have modifier "__declspec(dllexport)"
- functions in C++ code must have C call: get those function signatures in Curves
#ifdef __cplusplus
extern "C" {
#endif
.......
.......
#ifdef __cplusplus
}#endif

How to load Native Library?
Get this code sample. It finds the path from which the class loaded. For example you want your application JAR or Class to be in the same dir. as Native Code DLL.

java.net.URL url = <some class in your java application>.class.getProtectionDomain().getCodeSource().getLocation();
String location = url.getFile();
                int ind = location.lastIndexOf(java.io.File.separator);

So here you have got the location of the class. If this class resides in JAR file then at the end of the string in location var. will be JAR name, Remove it

                if(ind > 0 && ind + 1 < location.length())
                    location = location.substring(1, ind + 1);
                else
                    location = location.substring(1);

Append your dll name (including file extension). Let var. contains the full path to your DLL
          location += "mynative.dll";

Load it
          Runtime.getRuntime().load(location);


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:lwinkenb
lwinkenb earned 41 total points
ID: 9704959
>>Now....I have a comipled JNI compatible C exe and the associated java example.
Compile it as an object file or a dll instead, and then try running it.  If you post what compiler you are using, I can probably help you with the command line.

>>DLL functions are not export  type:(should have modifier "__declspec(dllexport)"
Not for JNI.  The correct JNI function signature is:
JNIEXPORT <return type> JNICALL Java_<class>_<method name>(JNIEnv *env, jobject obj <params>)
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9704968
Ahh, I see from your topic title you are using the microsoft compiler.  Try using the following to compile to a shared library.

cl -Ic:\<java>\include -Ic:\<java>\include\win32 -LD NativeFile.c -FeNativeFile.dll
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9706373
>Exception in thread "main" java.lang.UnsatisfiedLinkError: no MyImpOfPrompt in j
>ava.library.path
  just place your .dll to the folder, where your classfiles resides
0
 

Expert Comment

by:vitallis
ID: 9707013
in MS VC++
"JNIEXPORT" is defined as "__declspec(dllexport)"
"JNICALL" is defined as "__stdcall"
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9707726
actually it is defined as that in jni_md.h in the windows version of the JRE.  For other JRE platforms it is defined differently.  But yes you're right, since he is running windows, then he could use JNIEXPORT and __declspec(dllexport) interchangably.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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