# Using JNI to talk to C struct

**URL:** <https://discourse.processing.org/t/using-jni-to-talk-to-c-struct/21381>\
**Category:** Coding Questions\
**Created:** [May 28, 2020, 7:38pm UTC](https://discourse.processing.org/t/using-jni-to-talk-to-c-struct/21381 "2020-05-28T19:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [May 28, 2020, 7:38pm UTC](https://discourse.processing.org/t/using-jni-to-talk-to-c-struct/21381/1 "2020-05-28T19:38:31Z")

</div>

I barely understand what a struct is other than sorta like a class in java. I want to use processing and java to provide the input and then get the result back to java. I went to this link and found some code I want to use: [celestial navigation](https://arachnoid.com/celestial_navigation/index.html) I tried the code and like how simple it is to use. I know its out of date, but I can fix that eventually. I then went to here: [https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html](https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html) and learned a little about JNI . I also found an example of using JNI with processing: [https://processing.org/discourse/beta/num\_1147602703.html](https://processing.org/discourse/beta/num_1147602703.html)

I got that example to work!

The problem I have is what do I put in my java class if I want to use this C struct. I know I have to declare static native, etc. but I’m lost with what to do with a struct.

Here’s the C struct:

```auto
struct COMPLX getlatlng(struct COMPLX *x,int prompt)
{
	int i = 0;
	double a;
	if(prompt)
	{
		if(!comline)
			myputs(
		"Enter estimated position as (lat) dd mm.mm [N/S] (lng) ddd mm.mm [E/W]\n:");
	}
	getsptr = mygets(inbuf,buflen);
	split(inbuf);
	a = x->lat;
	i = llscan(i,&a);
	x->lat = a;
	a = x->lng;
	i = llscan(i,&a);
	x->lng = a;
}

```

Edit: I think I’ll try something easier in that C program to bridge for a first try.

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [May 29, 2020, 3:10am UTC](https://discourse.processing.org/t/using-jni-to-talk-to-c-struct/21381/2 "2020-05-29T03:10:07Z")

</div>

Yeah, not going to happen. There’s just too much I don’t understand about C, JNI and java. 🙁

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [May 31, 2020, 3:18am UTC](https://discourse.processing.org/t/using-jni-to-talk-to-c-struct/21381/3 "2020-05-31T03:18:16Z")

</div>

I guess I don’t give up as easily as I thought. I was playing with AS and found myself creating a new C++ project. I took the full C file I have and stuck it into the project. The compiler accepted it. I just need to learn to use JNI.

I guess my question at this point is what can I bridge with JNI? I imagine using java to accept some input and then pass that variable to JNI which translates it, then the C code uses it and the result goes back to JNI to get passed back to java.

Is the C code running throughout all this? It’s all so vague to me.
