TrainBeaser for iPhone
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Other JSE/JEE APIs
 
RSS feed
 
New topic
Author

problem with JNI( compiling .C file)

zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

am binding java with C, problem is in compiling hello.c file. where and how to compile ?.C file, i m using win-XP command prompt and tried with command 'cl', and also 'gcc' with cygwin both gives error ' command not found' ..
Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

Well, first question: do you have CL (Visual C++, a commercial product) or gcc (available in Cygwin as an optional install) actually installed? Neither will come with Windows itself.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

first of all thanks for your reply,
i dont have CL but i used intall now link on cygwin.com to download. it is installed.. i typed gcc command in cygwin's bash.

This message was edited 6 times. Last update was at by zius oram

Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

The default cygwin install doesn't include gcc and the other tools you'll need. You have to run the setup.exe program again, find gcc under the "Development" category, and install it -- it should install the other things automatically.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

i have downloaded the appropriate enviorment supporting 'gcc'. i tried to compile .C file as
gcc --mno-cygwin -D __int64="long long" -I jdk/include/ -I jdk/include/win32 -shared -Wl,--stdcall-alias -o HelloNative.dll Hellonative.c
where jdk is directory containing jdk, it is giving error no such file or directory.
refrence-- core java2 volume2 advanced features.. page 854

This message was edited 1 time. Last update was at by zius oram

Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

WHat file or directory is not being found? The JDK includes directory?

In Cygwin you have to use Cygwin paths; if your Windows JDK is at

c:\Program Files\Java\jdk1.5.0_11

then in Cygwin you'd call that

/cygdrive/c/Program\ Files/Java/jdk1.5.0_11

Note how I've "escaped" the space with a backslash.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

thank you,
now the only error is gcc: HelloNative.C : no such file or directory.
i have HelloNative.java, HelloNativeTest.java, HelloNative.C(written in notepad), HelloNative.h in different directory(e:\).
Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

Hmmm. Are you sure that HelloNative.C is not actually named HelloNative.C.txt ? NotePad will do that sometimes!

If you use cygwin's "ls" command to list the files in a directory, you'll see the true name of the file. If it's got that .txt on the end you could say

mv HelloNative.C.txt HelloNative.C

and then try again.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

i checked it is HelloNative.c , i am confused how gcc will come to know about my C file? as no where i am mentioning its location.
Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

zius oram wrote:i checked it is HelloNative.c , i am confused how gcc will come to know about my C file? as no where i am mentioning its location.


Ah. The command you showed assumes that the .c and .h files are both in the same place -- the current directory -- when you type the command. Put the files in the same directory if they're not, and then use "cd" to change to that directory.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

i did it, now error has changed to
cc1: error:unrecogniszed command line option "-fmno-cygwin"

when i removed one hifen (-) shown above(--mno)and now (-mno) it gives me error
HelloNative.c :1:24: HelloNative.h: no such File or Directory
HelloNative.c:4:error: syntax error before "void"
HelloNative.c:4:error: parse error before '*' token

my HelloNative.c file is as...




HelloNative.H file is....

Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

I think you need to change the include in the C file to

#include "HelloNative.h"

otherwise the compiler won't look in the current directory for the header, but rather in the system include directories.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

did it, now it is
In file included from HelloNative.c:1:
HelloNative.h:2:17: jni.h:no such file or directory
In file included from HelloNative.c:1:
HelloNative.h:15:error: syntax error before "void"
HelloNative.h:16:error: parse error before '*' token
HelloNative.h:16:warning: data defination has no type or storage class
HelloNative.c:4:error: syntax error before "void"
HelloNative.c:4:error: parse error before '*' token



Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

jni.h should be in that "jdk/include" directory. You might not have the path quite right. Make sure you can use "ls" to see the contents of the path you include there.

[Jess in Action][My Art][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Messages: 20

Thank You its working........
i followed all the steps specified by you, but the mistake i made was, I Omitted The Space in " \ Files", from the command you gave above.

$gcc -mno-cygwin -D __int64="long long" -I /cygdrive/c/Program\ File/java/jdk1.6.0/include/
-I /cygdrive/c/Program\ Files/java/jdk1.6.0/include/win32 -shared -Wl, --add-stdcall-alias -o HelloNative.dll HelloNative.c


Thank you once again.
Ernest Friedman-Hill
author
Sheriff

Joined: Jul 08, 2003
Messages: 21095

Great!

[Jess in Action][My Art][AskingGoodQuestions]
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Other JSE/JEE APIs
 
RSS feed
 
New topic
JProfiler
Get rid of your performance problems and memory leaks!

.