Native code

S

sandeep

Hello friends

I have a Java application and I need to use a C function for efficiency.

My Java text book says I should declare the function as native and use a
semicolon with no body.

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?

I am using Eclipse on Windows ME and BorLand C if it is relevant.

Thanks for your help ASAP.
 
K

Kenny McCormack

Hello friends

I have a Java application and I need to use a C function for efficiency.

My Java text book says I should declare the function as native and use a
semicolon with no body.

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?

I am using Eclipse on Windows ME and BorLand C if it is relevant.

Thanks for your help ASAP.

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

--
Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language
 
K

Keith Thompson

sandeep said:
I have a Java application and I need to use a C function for efficiency.

My Java text book says I should declare the function as native and use a
semicolon with no body.

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?

I am using Eclipse on Windows ME and BorLand C if it is relevant.

As it happens, the C language doesn't say anything about interfacing
between C and Java. The Java language probably does. You'll
probably get much better information in comp.lang.java.programmer.
But search for a FAQ for that group first.

<OT>Windows ME? Really?</OT>
 
B

Ben Bacarisse

sandeep said:
I have a Java application and I need to use a C function for efficiency.

My Java text book says I should declare the function as native and use a
semicolon with no body.

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?

You need to ask in a Java group. If there are several groups (sorry I
don't know any) pick one that deals with your version of Java since I
suspect (though I am guessing) that the detail vary between Java
implementations.
 
I

ImpalerCore

As it happens, the C language doesn't say anything about interfacing
between C and Java.  The Java language probably does.  You'll
probably get much better information in comp.lang.java.programmer.
But search for a FAQ for that group first.

<OT>Windows ME?  Really?</OT>

I'm going to use Windows XP until it doesn't boot anymore ;)
 
B

BGB / cr88192

sandeep said:
Hello friends

I have a Java application and I need to use a C function for efficiency.

My Java text book says I should declare the function as native and use a
semicolon with no body.

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?

I am using Eclipse on Windows ME and BorLand C if it is relevant.

WinME... hrrm...

anyways, as others have noted, it is OT here.

however, this much will be said:
http://en.wikipedia.org/wiki/JNI
 
B

BGB / cr88192

As it happens, the C language doesn't say anything about interfacing
between C and Java. The Java language probably does. You'll
probably get much better information in comp.lang.java.programmer.
But search for a FAQ for that group first.

<OT>Windows ME? Really?</OT>

<--
I'm going to use Windows XP until it doesn't boot anymore ;)
-->

well, at least XP doesn't suck...

although, age is catching up to it...
 
B

BGB / cr88192

Richard Harter said:
Good choice. I have a variety of sleeping iron. Win95, Win98,
Win ME, and vista. Then there's the one I use, Win XP.

I have Win 7 on my laptop, but am using XP64 on my main computer...

I would use Win7 on my main computer as well, but alas I only have a single
liscense key, and opted to have working Win7 on my laptop than on my main
comp (installed, but non-activated...).

although, luckily, with Win7 when activation expires it is simply annoying
(black desktop and watermark, ...), rather than disabling...

and, hell, there is still XP64...
 
B

blmblm

You need to ask in a Java group. If there are several groups (sorry I
don't know any) pick one that deals with your version of Java since I
suspect (though I am guessing) that the detail vary between Java
implementations.

I'd actually be surprised if there's much variation among
implementations for a particular platform -- in general Java
seems to spell out details that other languages leave to the
implementation to decide about -- but I also am guessing.

But I'll second the recommendations made by other posters to
consult the Wikipedia article on JNI (Java Native Interface)
and/or to ask in comp.lang.java.programmer.
 
B

BGB / cr88192

I'd actually be surprised if there's much variation among
implementations for a particular platform -- in general Java
seems to spell out details that other languages leave to the
implementation to decide about -- but I also am guessing.

But I'll second the recommendations made by other posters to
consult the Wikipedia article on JNI (Java Native Interface)
and/or to ask in comp.lang.java.programmer.

yeah, Java is fairly explicit about all this, and in general, C code written
against one VM on one arch will work against another VM on another arch.

all this is mostly because the details of JNI are specified against C, vs it
being some binary interface...
(it just requires writing a big pile of horrid-looking boilerplate, typical
of many VM FFI's...).


so, the main difference between VMs is in terms of which interfaces are
supported.
JNI is the main standardized interface, and is supported by nearly all VM's.
JNA is newer, and supported by some VM's (Sun's included), which differs in
that mostly the interfacing cruft is moved into Java (allowing many raw
DLL's to be used absent a JNI wrapper).

CNI exists (mostly specific to GCJ), which can link Java against the C++
ABI.
....
 
M

Malcolm McLean

But it doesn't explain how to associate this function to a C function.
Also how do the type map to C types? Eg primitive types are Fixed Width
in Java but in theory could vary in C. And how would a Java class carry
across to C?
I can't answer all your questions.
However a Java int is 4 bytes, a C int is usually but not always 4
bytes, and almost certainly will be on your platform. (Until we get
ANSI to declare that int should be 64 bits, heh, heh).

Java long is 64 bits. On your C compiler this type may be long but is
far more likely to be long long. Just use sizeof() to check.


The main thing you need to know is that Java

var = new myclass(x, y, z);

is equivalent to the C

typedef struct
{
int x;
int y;
int z;
} mystruct;

mystruct * mystructconstructor(int x, int y, int z)
{
mystruct *answer = malloc(sizeof(mystruct));
answer.x = x;
answer.y = y;
answer.z = z;
return answer;
}

Java treats everything as a pointer to a malloced C struct, under the
bonnet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top