Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-07
10
Medium Priority
?
710 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
8 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 172 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 164 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Assisted Solution

by:lwinkenb
lwinkenb earned 164 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

824 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