Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / February 2004

Tip: Looking for answers? Try searching our database.

Calling C from Java

Thread view: 
csharpdotcom - 23 Feb 2004 07:13 GMT
Hello,

This is my very first posting on this group.  As a test I'm trying to
find out how to call a C program from Java by using the "Hello World"
example given on SUN's website at
http://java.sun.com/docs/books/tutorial/native1.1/ .  I have an IBM
ThinkPad laptop computer running Windows 2000, and have installed
Visual C++6 and JDK 1.4.2_03 on it.

I copied the Java code HelloWorld.java from SUN's website that calls
the native method and compiled it successfully generating the file
HelloWorld.class.  I then invoked javah -jni to generate the header
file HelloWorld.h; so far so good.  The next step was to copy the
native C program HelloWorldImp.c, which has the three #includes:

#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>

then compile it to a DLL file, and it was at this point I was unable
to go further.  On attempting to compile it in C, I kept getting the
error message that the compiler was unable to find the file jni.h.  I
replaced <jni.h> by the full path "\j2sdk1.4.2_03\include\jni.h" which
the compiler found, but then came up with the message that the file
jni_md.h could not be found.  A link to this file is in jni.h, and
contains machine dependent typedefs.  It is in another folder called
win32, and I edited the jni.h to put in the full path to jni_md.h and
recompiled, but it failed again with an error message that jni.h again
could not be found, which seems rather curious.

It looks as if for some reason the C/C++ compiler is having problems
finding the Java header files, and most appreciate someone's kind help
in getting this resolved.

On a related matter, I am using Textpad to edit and run my Java
programs, but cannot find a way of including javah (and presumably
javadoc) with the Textpad tools.  As it happens I cannot run javah, or
any of the other Java utilities, in the command window in the folder
containing the java programs, and have to copy over the file I want to
operate on into the bin folder of the Java developer kit.  This could
possibly be related to the more serious problem above, so would be
grateful on any advice on this.

Sincerely yours,

Christopher Sharp
Gordon Beaton - 23 Feb 2004 07:38 GMT
> It looks as if for some reason the C/C++ compiler is having problems
> finding the Java header files, and most appreciate someone's kind
> help in getting this resolved.

Don't provide the full path when you include files. Write it just like
in the example:

 #include <jni.h>

The C compiler uses a search path to find the included header files,
and there is a compiler option that lets you add additional paths.
When you compile, use -I to add each of the two directories to the
header search path:

-I \j2sdk1.4.2_03\include -I \j2sdk1.4.2_03\include\win32

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

csharpdotcom - 23 Feb 2004 19:12 GMT
> > It looks as if for some reason the C/C++ compiler is having problems
> > finding the Java header files, and most appreciate someone's kind
[quoted text clipped - 13 lines]
>
> /gordon

Hi Gordon,

Many thanks for your help - I restored the include message as advised,
set up the paths, then compiled, then ran the Java program, which
works.

Christopher


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.