Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Importing contant values from C headers into Java

Reply
Thread Tools

Importing contant values from C headers into Java

 
 
Alain-Pierre BRUNEL
Guest
Posts: n/a
 
      07-23-2003
Hi, I use a native SDK written in C from a Java code (thanks to JNI).
An issue I have is that the C API contains some constants and enum
structs.
I have to redefine these constants on the Java side, because I don't
know how to import these "enum" & "#define".
It makes a coherence issue between the C Headers & the Java const.
If the C Headers constants values change, I've to change the Java
classes.

Do you know a way to import native defined constants into Java code ?

Regards,
Alain-Pierre.

PS: please reply on the news or at http://www.velocityreviews.com/forums/(E-Mail Removed).
(replace "1brunel1" by "brunel" to get my valid address)
 
Reply With Quote
 
 
 
 
Marc Rochkind
Guest
Posts: n/a
 
      07-23-2003
On 23 Jul 2003 06:33:39 -0700, Alain-Pierre BRUNEL <(E-Mail Removed)>
wrote:

> Hi, I use a native SDK written in C from a Java code (thanks to JNI).
> An issue I have is that the C API contains some constants and enum
> structs.
> I have to redefine these constants on the Java side, because I don't
> know how to import these "enum" & "#define".
> It makes a coherence issue between the C Headers & the Java const.
> If the C Headers constants values change, I've to change the Java
> classes.
>
> Do you know a way to import native defined constants into Java code ?
>
> Regards,
> Alain-Pierre.
>
> PS: please reply on the news or at (E-Mail Removed).
> (replace "1brunel1" by "brunel" to get my valid address)
>



You define a static final field and initialize it with a call to a native
function that returns the value of the C macro. I had hundreds of these in
Jtux (www.basepath.com/aup/jtux), so I built a table-driven native function
that takes the name of the constant as an argument string.

--Marc
 
Reply With Quote
 
 
 
 
Bent C Dalager
Guest
Posts: n/a
 
      07-23-2003
In article <(E-Mail Removed) >,
Alain-Pierre BRUNEL <(E-Mail Removed)> wrote:
>
>Do you know a way to import native defined constants into Java code ?


If your C header files are relatively straight forward, you could just
bundle the header sources (I assume they are headers) with your Java
app and parse them as text files when the app launches.

Alternatively, there are tools out there for converting from C source
to Java. You could just use such a tool to convert the files with the
constants/enums in them as part of your build process and put the
resulting class files into the JAR for all your Java sources to use.

And then there's JNI of course.

Cheers
Bent D
--
Bent Dalager - (E-Mail Removed) - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
Brad BARCLAY
Guest
Posts: n/a
 
      07-23-2003
Alain-Pierre BRUNEL wrote:
> Hi, I use a native SDK written in C from a Java code (thanks to JNI).
> An issue I have is that the C API contains some constants and enum
> structs.
> I have to redefine these constants on the Java side, because I don't
> know how to import these "enum" & "#define".
> It makes a coherence issue between the C Headers & the Java const.
> If the C Headers constants values change, I've to change the Java
> classes.
>
> Do you know a way to import native defined constants into Java code ?


The best way I know of is to use some sort of intermediate form to
represent these values that you can easily parse (XML, or a simple
properties file can work well for this, but whatever you're happy
parsing will work). Then create a script or program that will take this
data and construct a .h file and a .java file at the beginning of your
build process.

Thus, if your data file contains:

VALUE1 = 10
VALUE2 = 15
VALUE3 = 30

...it would generate a .h file with:

#define VALUE1 = 10
#define VALUE2 = 15
#define VALUE3 = 30

...and a Java file with:

public static final int VALUE1 = 10;
public static final int VALUE2 = 15
public static final int VALUE3 = 30

If you keep these elements in their own .h/.java files and import them
as necessary into other files, then you can make them completely dynamic
in this manner.

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with wsgiref.headers.Headers Phil Python 4 01-17-2010 04:47 PM
Server cannot clear headers after HTTP headers have been sent Ian ASP .Net Security 2 03-20-2007 09:00 AM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Reading 'received' headers: Email Headers Parsing dont bother Python 0 03-03-2004 08:18 PM
Contant Problems with Paradise & this Web site.. Gregory Parker NZ Computing 4 11-26-2003 04:19 AM



Advertisments