aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes problem with JNI( compiling .C file) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "problem with JNI( compiling .C file)" Watch "problem with JNI( compiling .C file)" New topic
Author

problem with JNI( compiling .C file)

zius oram
Greenhorn

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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][AskingGoodQuestions]
zius oram
Greenhorn

Joined: Aug 10, 2008
Posts: 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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Aug 10, 2008
Posts: 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 and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Great!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with JNI( compiling .C file)