Skip to main content

native code

4 replies [Last post]
azurianarcher
Offline
Joined: 2006-02-17

i was making a native code application, and i coudnt get it to work, so i made a really simple one:

java:

<br />
public class cprint<br />
{<br />
	public static void main(String args[])<br />
	{<br />
		cprint a = new cprint();</p>
<p>		a.print();<br />
	}</p>
<p>	public native void print();</p>
<p>	static<br />
	{<br />
		System.loadLibrary("libdll");<br />
	}<br />
}<br />

C:

<br />
#include "cprint.h"<br />
#include<br />
#include </p>
<p>JNIEXPORT void JNICALL Java_cprint_print(JNIEnv *env, jclass that)<br />
{<br />
    printf("Hello World");<br />
    return;<br />
}<br />

this also doesnt work, i get the same error:

<br />
Exception in thread "main" java.lang.UnsatisfiedLinkError: print<br />
        at cprint.print(Native Method)<br />
        at cprint.main(cprint.java:7)<br />
Press any key to continue...<br />

plz help, why does it do that

~Amx

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
srotman
Offline
Joined: 2003-06-16

Well, it has been a while since I have been working with JNI, but if I do recall correctly your C method should have a jobject as second argument, and not a jclass, which would make it look like:
[code]JNIEXPORT void JNICALL Java_cprint_print(JNIEnv *env, jobject that)[/code]

Perhaps it would also be wise to take a look at the JNI tutorial: http://java.sun.com/docs/books/tutorial/native1.1/

An explanation of the reason you get an UnsatisfiedLinkError is given in Step 6: http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/step6.html

Message was edited by: srotman

azurianarcher
Offline
Joined: 2006-02-17

i tried changing it to jobject in the header file, and in the main src, and it still gives me the same error. I consulted the tutorial and the exception thrown there is differet from the exception mine throws, theirs is thrown because the library can't be found, mine is thrown because something is wrong with the method(or so it seems)

~Amx

srotman
Offline
Joined: 2003-06-16

> i tried changing it to jobject in the header file,
You shouldn't have to edit the header file, you just generate it with the javah command.

I'm afraid I can't help you any further with this, but perhaps you can find an answer somewhere in the [i]Native Methods[/i] forum: http://forums.java.sun.com/forum.jsp?forum=52

azurianarcher
Offline
Joined: 2006-02-17

ya, sry i didnt kno that existed

~Amx

p.s. the new topic can be found at

http://forum.java.sun.com/thread.jsp?forum=52&thread=471335