Importing contant values from C headers into Java

Discussion in 'Java' started by Alain-Pierre BRUNEL, Jul 23, 2003.

  1. 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 .
    (replace "1brunel1" by "brunel" to get my valid address)
    Alain-Pierre BRUNEL, Jul 23, 2003
    #1
    1. Advertising

  2. On 23 Jul 2003 06:33:39 -0700, 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 ?
    >
    > Regards,
    > Alain-Pierre.
    >
    > PS: please reply on the news or at .
    > (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
    Marc Rochkind, Jul 23, 2003
    #2
    1. Advertising

  3. In article <>,
    Alain-Pierre BRUNEL <> 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 - - http://www.pvv.org/~bcd
    powered by emacs
    Bent C Dalager, Jul 23, 2003
    #3
  4. Alain-Pierre BRUNEL

    Brad BARCLAY Guest

    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
    
    Brad BARCLAY, Jul 23, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apc
    Replies:
    1
    Views:
    556
    Roedy Green
    Jun 2, 2004
  2. dont bother
    Replies:
    0
    Views:
    770
    dont bother
    Mar 3, 2004
  3. plb
    Replies:
    2
    Views:
    341
  4. Phil
    Replies:
    4
    Views:
    663
    Gabriel Genellina
    Jan 17, 2010
  5. Ian
    Replies:
    2
    Views:
    1,914
Loading...

Share This Page