how to create DLL?

HI Guys,I am new to JNI,I am working on some tutorial stuff for using Native code.I have cretaed a c++ source code Hello.cpp and compiled it to Hello.exe.Can somebody tell me how to cretae a DLL of it to be used in Java.Thnx in advance
[270 byte] By [rahul_yadava] at [2007-11-27 4:23:36]
# 1
You should just ask your compiler to create DLL.
Michael.Nazarov@sun.coma at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thnx, i am now able to generate dll with VC++ compiler.but getting exception java.lang.UnsatisfiedLinkError.I have set the java.library.path and copied dll there.any hints!!!
rahul_yadava at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
> Thnx, i am now able to generate dll with VC++> compiler.> but getting exception > >java.lang.UnsatisfiedLinkError.> have set the java.library.path and copied dll> there.> ny hints!!!Post the full error
Niceguy1a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Did you load the library?
jschella at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

solved this problem also....i had to put the dll in PATH.

now the new problem is

Exception in thread "main" java.lang.UnsatisfiedLinkError: print

"print" is the native method in java class Hello.java

Hello.h has method declaration as :

JNIEXPORT void JNICALL Java_Hello_print

(JNIEnv *, jobject);

and C++ file has method defined as :

JNIEXPORT void JNICALL

Java_Hello_print(JNIEnv *env, jobject obj)

{

printf("Hello World!\n");

return;

}

please help me out...it has been 3 days banging against JNI ...

rahul_yadava at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

> solved this problem also....i had to put the dll in

> PATH.

> now the new problem is

>

> xception in thread "main"

> java.lang.UnsatisfiedLinkError: print

>

> "print" is the native method in java class

> Hello.java

Is Hello.java in a package? If so, your header file is incorrect. The 'javah' tool expects a fully qualified class name and will generate an incorrect header if you don't include the package name:

javah mypackage.MyClass

Niceguy1a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
I create the .pj file already bt don' noe how to link with visual basic?Is it really need to create dll file?
waynefcwa at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 8

Hello,

I just don't get the tips how to compile a dynamic object library on WinXP from this tutorial:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html#sol

This code here:

Win32/WinNT/Win2000

cl -Ic:/jdk1.2/include

-Ic:/jdk1.2/include/win32

-LD nativelib.c -Felibnative.dll

Is cl a compiler or something? I don't have such a program on my disk. Only Visual studio (some old one) has cl.exe but I get some error that a *.dll is missing when I try to run it. Sorry but ... I need help so please explain how to compile a *.dll :\.

I need to check windows title of a windows application (not java app that I wrote). Actually I need to get the window title of the curent focused window. I didn't find a java class to do this. That's why I need to use JNI (to use local win32 api).

Thanks!

Funky_1321a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 9
Yes, cl is compiler.If you need more help you need to post more details."I get some error that a *.dll is missing when I try to run it." is not helpful description of your problem.
Michael.Nazarov@sun.coma at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 10

Oh. I googled for cl and didn't find nothing interesting so I didn't know that that's the right compiler :).

Actually the VS I have installed is Visual studio 8. If i run cl.exe i get the folowing error:

title:"cl.exe - Unable To Locate Component"

message:"This application has failed to start because mspdb80.dll was not found. Re-installing the appliaction may fix this problem."

Should I download and manually install the cl compiler? Now i'll google for cl compiler.

Funky_1321a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 11

Well, if you have installed VS then you can try to use it to compile without all this command line issues.

But it's good practice, so try one (or both) of the following:

1. Possibly IDE created Start menu item called like "Visual bah-blah environment" - command line with all required staff like path to headers, libs and dlls.

2. Create such environment by youself. Local mspdb80.dll - hope one presents in your system, specify path and start cl.exe again. I bet you'll see another one message, so there will be next step. Go on ;)

Michael.Nazarov@sun.coma at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 12

I tried both. Now I set the PATH properly and placed the missing *.dll file into the %WINDIR%\system32 folder. So the compiler is working.

Okay I can't just copy into the command prompt this:

cl -Ic:/jdk1.2/include

-Ic:/jdk1.2/include/win32

-LD nativelib.c -Felibnative.dll

So I tried with:

cl -Ic:/"Program Files"/Java/jdk1.6.0_01/include

I got this: cl : Command line error D8003 : missing source filename

I really don't know how to use this compiler because this tutorial (http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html#sol) doesn't tell any precise usage informations.

What now :)?

And thanks!

Funky_1321a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 13
Why did you drop part of string? It should be something like:cl -Ic:/jdk1.2/include -Ic:/jdk1.2/include/win32 -LD nativelib.c -Felibnative.dll
Michael.Nazarov@sun.coma at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 14

OOoh sorry.

It confused me. I don't have path like this: c:/jdk1.2/include and file separators are turned around. Actually I saw now that the implementation file explained in the tutorial has to be named nativelib.c. That isn't mentionet in the tutorial in section 'native method implementation'.

Now I have to locate the *.h library files. I think then it'll work.

Thanks alot!

Funky_1321a at 2007-7-12 9:31:15 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 15

God no! I found and placed to proper location all the needed headers and then finally I run the compiler and got all those error messages:

cl -Ic:/jdk1.2/include -Ic:/jdk1.

2/include/win32 -LD nativelib.c -Felibnative.dll

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86

Copyright (C) Microsoft Corporation. All rights reserved.

nativelib.c

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(22) : error C

2061: syntax error : identifier 'cygwin32_winpid_to_pid'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(22) : error C

2059: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(22) : error C

2059: syntax error : 'type'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(34) : error C

2061: syntax error : identifier 'cygwin_winpid_to_pid'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(34) : error C

2059: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/cygwin.h(34) : error C

2059: syntax error : 'type'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(47) : error C207

9: 'shm_perm' uses undefined struct 'ipc_perm'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(49) : error C206

1: syntax error : identifier 'pid_t'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(50) : error C206

1: syntax error : identifier 'shm_cpid'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(50) : error C205

9: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(52) : error C206

1: syntax error : identifier 'shm_atim'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(52) : error C205

9: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(53) : error C206

1: syntax error : identifier 'shm_dtim'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(53) : error C205

9: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(54) : error C206

1: syntax error : identifier 'shm_ctim'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(54) : error C205

9: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(61) : error C205

9: syntax error : '}'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(100) : error C21

46: syntax error : missing ')' before identifier 'key'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(100) : error C20

61: syntax error : identifier 'key'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(100) : error C20

59: syntax error : ';'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(100) : error C20

59: syntax error : ','

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\sys/shm.h(100) : error C20

59: syntax error : ')'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\unistd.h(24) : error C2054

: expected '(' to follow '__CRT_INLINE'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\unistd.h(25) : error C2082

: redefinition of formal parameter 'ftruncate'

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\unistd.h(25) : error C2143

: syntax error : missing ';' before '{'

nativelib.c(12) : error C2065: 'caddr_t' : undeclared identifier

nativelib.c(12) : error C2146: syntax error : missing ';' before identifier 'm'

nativelib.c(12) : error C2065: 'm' : undeclared identifier

nativelib.c(13) : error C2275: 'jbyteArray' : illegal use of this type as an exp

ression

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\jni.h(89) : see de

claration of 'jbyteArray'

nativelib.c(13) : error C2146: syntax error : missing ';' before identifier 'jb'

nativelib.c(13) : error C2065: 'jb' : undeclared identifier

nativelib.c(14) : error C2275: 'jboolean' : illegal use of this type as an expre

ssion

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\jni.h(39) : see de

claration of 'jboolean'

nativelib.c(14) : error C2146: syntax error : missing ';' before identifier 'isc

opy'

nativelib.c(14) : error C2065: 'iscopy' : undeclared identifier

nativelib.c(15) : error C2143: syntax error : missing ';' before 'type'

nativelib.c(16) : error C2143: syntax error : missing ';' before 'const'

nativelib.c(18) : error C2143: syntax error : missing ';' before 'type'

nativelib.c(20) : error C2065: 'fd' : undeclared identifier

nativelib.c(21) : error C2065: 'mfile' : undeclared identifier

nativelib.c(23) : error C2065: 'finfo' : undeclared identifier

nativelib.c(24) : warning C4022: 'mmap' : pointer mismatch for actual parameter

1

nativelib.c(24) : error C2143: syntax error : missing ')' before 'constant'

nativelib.c(24) : error C2198: 'mmap' : too few arguments for call

nativelib.c(24) : warning C4047: '=' : 'int' differs in levels of indirection fr

om 'void *'

nativelib.c(30) : error C2224: left of '.st_size' must have struct/union type

nativelib.c(30) : error C2198: 'function through pointer' : too few arguments fo

r call

nativelib.c(30) : warning C4047: '=' : 'int' differs in levels of indirection fr

om 'jbyteArray'

nativelib.c(31) : warning C4047: 'function' : 'jbyteArray' differs in levels of

indirection from 'int'

nativelib.c(31) : warning C4024: 'function through pointer' : different types fo

r formal and actual parameter 2

nativelib.c(32) : error C2224: left of '.st_size' must have struct/union type

nativelib.c(32) : warning C4047: 'function' : 'jsize' differs in levels of indir

ection from 'jbyte *'

nativelib.c(32) : warning C4024: 'function through pointer' : different types fo

r formal and actual parameter 4

nativelib.c(32) : error C2198: 'function through pointer' : too few arguments fo

r call

nativelib.c(34) : warning C4047: 'function' : 'const char *' differs in levels o

f indirection from 'int'

nativelib.c(34) : warning C4024: 'function through pointer' : different types fo

r formal and actual parameter 3

nativelib.c(35) : warning C4047: 'return' : 'jbyteArray' differs in levels of in

direction from 'int'

I'm lost. What now ?

Funky_1321a at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 16
> I create the .pj file already bt don' noe how to link> with visual basic?> Is it really need to create dll file?On windows you need a dll file.
jschella at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 17

> God no! I found and placed to proper location all the

> needed headers and then finally I run the compiler

> and got all those error messages:

>

Trying to learn C/C++ using JNI probably isn't a good idea.

The compiler is telling you you have C/C++ errors.

The errors indicate that there is a problem with your header files.

You are using MS compiler correct? Then why are you using cygwin.h?

jschella at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 18

I made the C++ exam very well last year on Faculty of computer science. Actually till now I learned only fundamentals of C++. But that's not the question. I have problems using the cl compiler. And yes, I also figured out that there are problems with header files.

This compiler was installed with VS 2005. When I run it for the first time with the commands I got from the java jni tutorial, header files were missing. I collected them from other locations and put them into the VS's include dir.

I asked before where can I get only the compiler so it would work properly. I thought that this one which came with VS should work out-of-the-box. Do you have some suggestions maybe?

And once again ... at the moment I dont need to programm in c++ at all if I just want to use some win32 apis in my java app.

Thanks

Funky_1321a at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 19
> And once again ... at the moment I dont need to> programm in c++ at all if I just want to use some> win32 apis in my java app.So you can use a generic wrapper like JNative to do this.--Marc ( http://jnative.sf.net)
mdentya at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 20
> I collected them from other locations and put them into the VS's include dir.No no! This is wrong way!You should remove these copies and specify paths to your original include files in the command line instead.
Michael.Nazarov@sun.coma at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 21

> I made the C++ exam very well last year on Faculty of

> computer science. Actually till now I learned only

> fundamentals of C++. But that's not the question. I

> have problems using the cl compiler. And yes, I also

> figured out that there are problems with header

> files.

To program in C/C++ one must understand the compilation process.

>

> This compiler was installed with VS 2005. When I run

> it for the first time with the commands I got from

> the java jni tutorial, header files were missing. I

> collected them from other locations and put them into

> the VS's include dir.

>

The following are the only headers that you should be using.

- jni.h, which comes with java

- The headers that correspond to your C/C++ compiler.

You must set the correct include directories on the command line to include both of the above.

> I asked before where can I get only the compiler so

> it would work properly. I thought that this one which

> came with VS should work out-of-the-box. Do you have

> some suggestions maybe?

>

Myself I just use the Visual Studio IDE for these sorts of projects. Recompilation of the shared library portion has never been a concern in terms of the overall java project, so I don't concern myself with the command line.

> And once again ... at the moment I dont need to

> programm in c++ at all if I just want to use some

> win32 apis in my java app.

To do JNI you must write C or C++ code.

If you aren't programming in C/C++ then what is the point of this thread?

jschella at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 22

Okay I really don't know much about compilation process of this c++ compiler :).

So how do I specify the include dir on the command line?

I remember that years ago I wrote some visual basic application in which I use some getWindowTitle method or something like that. And I would like to use this method in my java app. Is it possible to include some existing win api that includes an implemented kind of method to get the title of curent focused window with use of jni? I think that I don't need to implement by myself a method to reach that. However now I found a work-around so now actually I don't need to do that but anyway I'd like to learn how to use native methods in java.

Over the weekend I can't use my primary computer because I'm testing the java app I wrote and the process wouldn't stop until sunday evening so I'll test your suggestions later.

Sorry folx :).

Funky_1321a at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 23

> I remember that years ago I wrote some visual basic

> application in which I use some getWindowTitle method

> or something like that. And I would like to use this

> method in my java app. Is it possible to include some

> existing win api that includes an implemented kind of

> method to get the title of curent focused window with

> use of jni?

Presumably you meant without jni.

See reply #19 (which still uses JNI but does it in a way so that you don't have to write code yourself.)

jschella at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 24
Yes this is what I need! I tried an example with this wrapper. Cool.Many thanks! :)Tilen
Funky_1321a at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 25
LoadProlog = LoadP(Command, Desire, Encoding) What is the error from the code above?Please help!!!!
waynefcwa at 2007-7-21 21:05:46 > top of Java-index,Java HotSpot Virtual Machine,Specifications...