I am trying to run mathtext from a java program using apache-commons-exec. The problem is I am getting different output when I run the same command from a java program and when I run it through shell. so if run mathtext like this in the shell:

./mathtext test.png "\$\frac{{\left( {{p^2} - {q^2}} \right)}}{2}\$"

in a shell I get the perfect png but when I run the same thing using apache-commons-exec

Map map = new HashMap();    

        map.put("target", new File(trgtFileName));
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        Executor exec = new DefaultExecutor();
        exec.setWorkingDirectory(/*I set the working directory where the mathtext is*/);
        CommandLine cl = new CommandLine("./mathtext"); 
        cl.addArgument("${target}");
        cl.addArgument(latex);
        cl.setSubstitutionMap(map);
//      Logger.log4j.info("command is:::"+cl.toString());
        ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
        exec.setWatchdog(watchdog);
        exec.execute(cl,EnvironmentUtils.getProcEnvironment(),resultHandler);
        resultHandler.waitFor();

I get the image, not the equation but the raw TeX string :(

Can somebody please help me in solving the issue? I want to get the exact output. Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

I figured out where the problem was:

$ is a special character for the unix shell and not for java. So even if in the command line the input needs to escape $ like:

"\$\frac{{\left( {{p^2} - {q^2}} \right)}}{2}\$"

inside the java program I dont need to escape the '$' or put " (double quotes) at the beginning and at the end.I had to put the command like:

$\frac{{\left( {{p^2} - {q^2}} \right)}}{2}$

Hope this helps somebody :)

--Shankhoneer