Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-07
10
Medium Priority
?
705 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

715 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