From: Antonio T. <ant...@il...> - 2009-01-16 18:15:20
|
hi, i'd like to use text-similarity from a java program. So from this program I call text-similarity and then I capture its output. This is the java code: String command = "text_similarity.pl --type=Text::Similarity::Overlaps --string \"" + string1 + "\" \"" + string2 + "\""; String result = ""; try { Process p = Runtime.getRuntime().exec(command); int command_exit = p.waitFor(); System.err.println("Command ended with value: " + command_exit); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); String s = null; while ((s = stdInput.readLine()) != null) { System.out.println("\treading result: " + s); result += s; } while ((s = stdError.readLine()) != null) { System.out.println("\tERROR: " + s); } } catch (IOException e) { e.printStackTrace(); System.exit(-1); } catch (InterruptedException i) { i.printStackTrace(); System.exit(-2); } However it does not work! I just get the string "0". If I run text-similarity from the command line it works fine. If I call from my java program a shell command (like "ls") or a "hello world" perl script then I can capture its output, so I guess the problem is somehow related to the way text-similarity buffers its output. I've read of people having this kind of issues when calling perl scripts from java and someone proposes as a solution to put this at the beginning of perl scripts: use IO::Handle; STDOUT->autoflush(1); STDERR->autoflush(1); [http://forums.sun.com/thread.jspa?threadID=189595&forumID=31] however, I've also tried to put this at the beginning of text_similarity.pl but without luck! Does anyone know how should I do? Thanks in advance, Antonio Toral |