I am trying to call a DLL from Java.
The class is declared as
package knapsacproject;
public class algorithm {
public native int[] geneticAlgorithm(int[] cost,int[] profit, int gens ,int turns , int cmax );
static{
System.loadLibrary("Project3");
System.out.println("ok");
}
protected int[] cost,profit, result;
protected int gens, turns, cmax;
public algorithm(int[] cost,int[] profit, int gens ,int turns , int cmax) {
this.cost=cost;
this.profit=profit;
this.gens=gens;
this.turns=turns;
this.cmax= cmax;
}
int[] getResult() {
return geneticAlgorithm(cost, profit, gens, turns, cmax);
}
public static void main (String[] args ) {
}
}
I have a button on a frame which resolve any methods
Interface.java
public int[] getTableData(DefaultTableModel model, Integer colIndex)
{
int nRow = model.getRowCount();
int[] tableData = new int [nRow];
for(int i = 0; i< nRow; i++) {
tableData[i] = Integer.parseInt((String) model.getValueAt(i,colIndex));
}
return tableData;
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int nRow = model.getRowCount();
try{
algorithm algo = new algorithm(getTableData(model, 1),getTableData(model, 2),
Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()));
int[]result = algo.getResult();
for(int i = 0; i <nRow;i++) {
if(result[i]==1)
model.setValueAt("take", i , 3);
else
model.setValueAt("leave", i, 3);
}
jTable1.getColumnModel().getColumn(3).setCellRenderer(new StatusColumnCellRenderer());
jTextField4.setText(Integer.toString(result[nRow-1]));
jTextField5.setText(Integer.toString(result[nRow-2]));
}catch (Exception e ) {
e.printStackTrace();
}}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Interface().setVisible(true);
}
});
}
my main class
package knapsacproject;
public class KnapSacProject {
public static void main (String[]args){
Interface in = new Interface();
in.setVisible(true);
}}
The program crashes at
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError:knapsacproject.algorithm.geneticAlgorithm([I[IIII)[I
at knapsacproject.algorithm.geneticAlgorithm(Native Method)
at knapsacproject.algorithm.getResult(algorithm.java:41)
The library should be fine.
library in C
void ga_solve(int * result, int * CostT, int * ProfitT, int Cmax, int GENS, int TURNS, int size) {
int i, j, k, gen, t;
time_t start, stop;
time(&start);
crom pop[POP], children[CHILDREN], temp[POP + CHILDREN], best, best_turn;
best.fitness = 0;
for (t = 0; t < TURNS; ++t) {
best_turn.fitness = 0;
for (i = 0; i < POP; ++i) {
for (j = 0; j < size; ++j) {
pop[i].cromo[j] = random(0, 2);
}
}
gen = 0;
......etc. code is long
Do not load my library?So where is the problem in my code? Or is wrong somethng in native method.
When you have an idea here with him, because I am looking for a solution for a long time.
What I have tried:
I tried to load the library through JNI or load from JAR but I still shows the same error.