As some of you have read, Ive been working with JNI for these days. Ive found that JNA may be easier to manage and also allows some other things.

I have this very simple program:

The .cpp:

#include <stdio.h>
#include "PruebasDeJNA.h"

int suma(int a,int b)
{
	return a+b;
}

The .h:

#include <stdio.h>

int suma(int,int);

All it does is add two numbers and return them. Nothing more.

In my .java program:

package hola;

import com.sun.jna.*;

public class Mundo 
{
	public interface PruebasDeJNA extends Library 
	{ 
		PruebasDeJNA INSTANCE = (PruebasDeJNA)Native.loadLibrary((Platform.isWindows() ? "PruebasDeJNA" : "c"),PruebasDeJNA.class);
		int suma(int a,int b); 

	}

	public static void main(String[] args) 
	{		
			System.out.println(PruebasDeJNA.INSTANCE.suma(5, 7));
         }
}

The error that it gives me is that it cannot find the function "suma" in the library.

As you can see, avoiding porting the code from C++ to Java is neccesary as there are too many lines to port over and the program is alot of math so one miscalulation could throw away alot of work.


Thanks for the help

I don't know about JNA but there isn't a suma or sum method in Java. Usually you need to create a sum integer variable and calculate sum = a+b and print out the variable, System.out.println(sum). You also need to initiate and assign a and b values...

I don't know about JNA but there isn't a suma or sum method in Java. Usually you need to create a sum integer variable and calculate sum = a+b and print out the variable, System.out.println(sum). You also need to initiate and assign a and b values...

I made that function in C++ and read it from a library in C++ from Java. Its a personalized function :)

That's cool. Never heard of this being done before.
From not knowing anything about JNI/JNA, I would say to check that it's connecting to the C files properly, the problem isn't with the suma function, it just can't find it. So you could create a print statement in the C files on connection to them just to test that they're connectig properly. Possibly try to publicise int suma.
I can't offer much more advise sorry!

That's cool. Never heard of this being done before.
From not knowing anything about JNI/JNA, I would say to check that it's connecting to the C files properly, the problem isn't with the suma function, it just can't find it. So you could create a print statement in the C files on connection to them just to test that they're connectig properly. Possibly try to publicise int suma.
I can't offer much more advise sorry!

Thank you for trying anyways :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.