[英]How to call a CUDA C app in Java through ProcessBuilder?
我有一个非常复杂的应用程序,它使用CUDA并用C语言编写。该应用程序仅是命令行,现在我想使用Java Swing构建GUI。
我不想重写C代码,所以我要使用ProcessBuilder对象调用命令行版本。 这样,我可以从中读取消息并在GUI内的控制台上显示。
这是我正在使用的代码:
String command = "myApp";
pb = new ProcessBuilder("bash", "-c",command);
pb.redirectErrorStream(true);
Process shell;
try {
shell = pb.start();
InputStream shellIn = shell.getInputStream();
Drawer.writeToLog(convertStreamToStr(shellIn));
shellIn.close();
} catch (IOException e) {
e.printStackTrace();
}
public String convertStreamToStr(InputStream is) throws IOException{
if(is != null){
Writer writer = new StringWriter();
char[] buffer = new char[1024];
Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
int n;
while((n = reader.read(buffer)) != -1){
writer.write(buffer,0,n);
}
is.close();
return writer.toString();
}else{
return "";
}
}
如果我尝试使用类似“ ls”命令的方法,则效果很好。 但是,对于我的应用程序,我收到此错误:
./myApp: error while loading shared libraries: libcudart.so.5.0: cannot open shared object file: No such file or directory
CUDA已在我的计算机中安装并正确配置,我可以从终端正确执行此应用程序。 我认为错误是来自Java虚拟机。
我该如何解决?
这段代码对我有用,我刚刚对其进行了测试:
System.out.printf("[Starter#main] !\n");
final ProcessBuilder builder = new ProcessBuilder("absolute/path_to/your/executable");
builder.redirectErrorStream(true);
builder.environment().put("LD_LIBRARY_PATH",
"/usr/local/cuda/lib64:/usr/local/cuda/lib");
final Process start = builder.start();
final InputStream outputStream = start.getInputStream();
final BufferedReader reader = new BufferedReader(new InputStreamReader(outputStream));
String s;
while ((s = reader.readLine()) != null) {
System.out.printf("[Starter#main] %s\n", s);
}
陷阱:
另请注意:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.