Java跳过代码行

时间:2015-04-23 15:02:13

标签: java jython

我有一个JNI包装器将数组从Fortran传递给Java。现在,我要求Java将此数组传递给Python并返回值。

objFuncType.java如下:

public interface objFuncType {
    public double calculateobjFunc(double[] pos);
}

objFunc.java如下:

import java.io.*;
import java.util.*;
import org.plyjy.factory.JythonObjectFactory;

public class objFunc {

public static double evaluate(double[] position) {
    double result;
    for (int i=0;i<position.length;i++)
       System.out.println("position ="+position[i]);

    JythonObjectFactory factory = JythonObjectFactory.getInstance();
    System.out.println("JythonObjectFactory created");
    objFuncType func=(objFuncType)factory.createObject(objFuncType.class, "CostCalculator");
    result = func.calculateCost(position);
    System.out.println("Result computed! Result = "+result);
    return result;

    }
}

CostCalculator.py如下:

import objFuncType

class CostCalculator(objFuncType, object):

def __init__(self):
    print 'Initializing'
    pass

def calculateCost(self, chromosome):
    fitness = 0
    for i in range(len(chromosome)-1):
        fitness += 100*((chromosome[i]**2)-chromosome[i+1])**2+\
        (1-chromosome[i])**2
    return fitness

当我从外部程序调用Java函数时,输出如下:

position =1.3653333187103271
position =0.0
position =-1.8204443454742432
position =-1.8204443454742432

正在打印数组,但跳过了print语句后的所有语句。 请帮忙!

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我使用JNI从C调用Java,看起来它不知道在哪里查找jar文件。我在我的C程序中添加了以下代码行来调用Java。它按预期运行。

JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[3];
args.version = JNI_VERSION_1_6;
args.nOptions = 3;
options[0].optionString = "-Djava.class.path=./";
options[1].optionString = "-Djava.class.path=<path_to_class>:<path_to_jars>";
options[2].optionString = "-Djava.class.path=<path_to_class>:<path_to_jars>";
args.options = options;
args.ignoreUnrecognized = 0;
int status;
status = JNI_CreateJavaVM(jvm, (void**)&env, &args);
if (status < 0 || !env)
    printf("Unable to Launch JVM %d\n",rv);
else
    printf("Launched JVM successfully\n");
return env;