[英]Run C++ program using Java
I created a Java program that will process files using a C++ program. 我创建了一个Java程序,该程序将使用C ++程序处理文件。 This is the part of the code that calls the C++ program: 这是调用C ++程序的代码的一部分:
public static boolean buildPAKFile(String OSMFile){ log("Starting building PAK file for " + OSMFile + "..."); // get name of OSMFile String[] OSMFileName = OSMFile.split("\\."); try { Runtime rt = Runtime.getRuntime(); Process pr = rt.exec("cat " + OSMFile + " | ../gosmore rebuild " + OSMFileName[0]); /* BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line=null; while((line=input.readLine()) != null) { System.out.println(line); }*/ int exitVal = pr.waitFor(); System.out.println("Exited with error code "+exitVal); } catch(Exception e) { System.out.println(e.toString()); e.printStackTrace(); } log("Done building PAK File for " + OSMFile); return true; }
When I uncomment the part the prints the input stream from the C++ program, gibberish text appears. 当我取消注释零件时,将打印来自C ++程序的输入流,出现乱码。 How can I do this properly? 如何正确执行此操作? Thanks! 谢谢!
Added: 添加:
This is an example of the command executed: 这是执行的命令的示例:
cat ncr-sample.osm | ../gosmore rebuild ncr-sample.pak Building ncr-sample.pak using style /usr/local/share/gosmore/elemstyles.xml... ../gosmore is in the public domain and comes without warranty ... 484 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 485 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 486 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 487 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 488 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 489 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 490 for (pairs = 0; pairs < PAIRS && s2grp < S2GROUP (0) + S2GROUPS; ) 491 for (ndType *ndItr = ndBase; ndItr < ndBase + hashTable[bucketsMin1 + 1]; ndItr++) 492 qsort (&lseg[0], lseg.size () / 2, sizeof (lseg[0]) * 2, (int (*)(const void *, const void *))HalfSegCmp) 493 for (int i = 0; i < IDXGROUPS; i++) Icon public.png not found Icon public.png not found Icon religion/synagogue.png not found Icon religion/mosque.png not found Icon rendering/landuse/cemetery.png not found Icon wlan.png not found Icon rendering/landuse/cemetery.png not found ../gosmore is in the public domain and comes without warranty
I've seen this before . 我以前看过这个 。 Runtime.exec()
does not spawn a shell, so cannot process shell redirection characters such as |
Runtime.exec()
不会生成外壳程序,因此无法处理诸如|
外壳程序重定向字符|
. 。 The only command you are running is the initial cat
. 您正在运行的唯一命令是初始cat
。 To fix this, either pass the XML file into the standard input of the subprocess, or wrap the chain into a shell script. 要解决此问题,请将XML文件传递到子流程的标准输入中,或将链包装到Shell脚本中。
I found out with the help of @Adrian Cox that the correct implementation should be like this: 我在@Adrian Cox的帮助下发现正确的实现应如下所示:
String gosmoreCmd = " cat " + OSMFile + " | ../gosmore rebuild"
+ OSMFileName[0] + ".pak";
String[] cmds = {"/bin/bash", "-c", gosmoreCmd};
Process pr = rt.exec(cmds);
/bin/bash runs the shell, which with the -c parameter, you can able to pass a new command to this shell. / bin / bash运行外壳程序,使用-c参数,您可以将新命令传递到该外壳程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.