繁体   English   中英

不同的输出-在命令行中运行mathtext时以及使用apache-commons-exec从Java程序执行命令时

[英]Different output — when running mathtext in command line and when the command is executed from a java program using apache-commons-exec

我正在尝试使用apache-commons-exec从Java程序运行mathtext。 问题是,当我从Java程序运行相同的命令并通过外壳运行它时,我得到的输出不同。 因此,如果在shell中像这样运行mathtext:

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

在外壳中,我得到了完美的png,但是当我使用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();

我得到的是图像,而不是方程式,而是原始的TeX字符串:(

有人可以帮我解决问题吗? 我想获得确切的输出。 谢谢。

我弄清楚问题出在哪里:

$是unix shell的特殊字符,而不是java的特殊字符。 因此,即使在命令行中输入也需要转义$,例如:

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

在Java程序内部,我不需要转义'$'或在开头和结尾处都加上双引号。我必须将命令放在:

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

希望这对某人有帮助:)

-山可尼

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM