Importing contant values from C headers into Java

  • Thread starter Alain-Pierre BRUNEL
  • Start date
A

Alain-Pierre BRUNEL

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 address removed).
(replace "1brunel1" by "brunel" to get my valid address)
 
M

Marc Rochkind

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 address 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
 
B

Bent C Dalager

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
 
B

Brad BARCLAY

Alain-Pierre BRUNEL said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top