开发者

Run C++ program using Java

I created a Java program that will process files using a C++ program. This is the part of the code that calls the C++ program:

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. 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 |. The only command you are running is the initial cat. To fix this, either pass the XML file into the standard input of the subprocess, or wrap the chain into a shell script.


I found out with the help of @Adrian Cox that the correct implementation should be like this:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜