Solved

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

Posted on 2003-11-07
10
700 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

630 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