• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with JNI( compiling .C file)

 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zius oram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic