gpt4 book ai didi

java - 使用 Java 运行 C++ 程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:58 24 4
gpt4 key购买 nike

我创建了一个 Java 程序,它将使用 C++ 程序处理文件。这是调用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;    }

当我取消注释打印来自 C++ 程序的输入流的部分时,出现乱码文本。我怎样才能正确地做到这一点?谢谢!

添加:

这是执行命令的示例:

cat ncr-sample.osm | ../gosmore rebuild ncr-sample.pakBuilding 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 foundIcon public.png not foundIcon religion/synagogue.png not foundIcon religion/mosque.png not foundIcon rendering/landuse/cemetery.png not foundIcon wlan.png not foundIcon rendering/landuse/cemetery.png not found../gosmore is in the public domain and comes without warranty

最佳答案

我看过 this before . Runtime.exec() 不会生成 shell,因此无法处理 shell 重定向字符,例如 |。您正在运行的唯一命令是初始 cat。要解决此问题,请将 XML 文件传递​​到子流程的标准输入中,或者将链包装到 shell 脚本中。

关于java - 使用 Java 运行 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5221588/

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