Lookup for a constant from a native library
09:12 30 Jan 2026

I'm writing a demo Java app that interacts with libpq. For this, I'm using java.lang.foreing package. My problem is, I can easily pick up functions but not integer enum constants:

System.load("path/to/the/lib/pq")
var lookup = SymbolLookup.loaderLookup();

// works fine
MemorySegment PQclear = lookup.find("PQclear").get()

// but this fails (not found)
MemorySegment CONN_OK = lookup.find("CONNECTION_OK").get()

The CONNECTION_OK value is just a enum defined as follows in C:

typedef enum {
  CONNECTION_OK,
  CONNECTION_BAD,
  ...
}

I wonder how can I reach these enum values in Java? I wouldn't like to copy-paste them from C sources.

java java-native-interface libpq