Reputation: 63
Good morning everyone,
I'm finding trouble to make a SWIG interface work for C++. I have several .cpp and .h files, of all of them I only want to create an interface for a few (which I will use in my Java code), hence my .i file looks like:
/* File : AlgoritmoElectrico.i */
%module alg
/* Header files that are referred in the ones I want to create the interface with */
%{
#include "AlgoritmoElectrico.h"
#include "Proyecto.h"
#include "Indice.h"
/* ... I skipped a few to make it shorter ... */
#include "ParserTime.h"
%}
/* Header files of classes I want to use in Java */
%include "AlgoritmoElectrico.h"
%include "AlgoritmoElectrico.h"
So I ran swig -c++ -java AlgoritmoElectrico.i
and got several .java files, plus the .cxx wrapper, I compiled all the .java files with javac *.java
and created the .so library with the native and wrapper code.
My Java code looks like this:
package mr;
/* ... Stuff ... */
public class MRAlgoritmo {
public static class Map extends Mapper<LongWritable, Text, IntWritable, Text> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Obtiene instante y circulaciones
Pattern pattern = Pattern.compile("\t[ ||| ]"); // FIXME revisar regex
String[] info = pattern.split(value.toString());
// Captura datos de proyecto
System.loadLibrary("algoritmo");
Proyecto proyecto = new Proyecto("Proyecto1");
proyecto.ReadFile("infraestructura");
proyecto.getParametros().setIntervalo(1);
// Ejecuta algortimo con datos de circulaciones
AlgoritmoElectrico algoritmo = new AlgoritmoElectrico(proyecto);
String [] resultados = algoritmo.Ejecutar(info);
/* ... stuff ... */
}
}
public static void main(String[] args) throws Exception {
/* ... stuff not related with the above, working with Hadoop MR ... */
}
}
Proyecto
and AlgoritmoElectrico
are C++ classes and they are not being found. Ideas?
Thanks!!
Upvotes: 1
Views: 322
Reputation: 63
I found the issue, it was a packaging error. I forced swig to add a package statement to the .java files and then the import worked fine
I used swig -c++ -java -package <package_name> <files>
, then I compiled and built the jar preserving the package structure. After that I could refer to its contents as usual and it worked fine.
Upvotes: 1