gpt4 book ai didi

Java 跳过代码行

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:56 24 4
gpt4 key购买 nike

我有一个 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

数组打印正常,但打印语句之后的所有语句都被跳过。请帮忙!

最佳答案

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

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;

关于Java 跳过代码行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com