Solved

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

Posted on 2003-11-07
10
660 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
10 Comments
 

Author Comment

by:COMRappa
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
 
LVL 5

Assisted Solution

by:lwinkenb
lwinkenb earned 41 total points
Comment Utility
>>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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:lwinkenb
Comment Utility
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
Comment Utility
>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
Comment Utility
in MS VC++
"JNIEXPORT" is defined as "__declspec(dllexport)"
"JNICALL" is defined as "__stdcall"
0
 
LVL 5

Expert Comment

by:lwinkenb
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now