如何通过在java中运行tcl脚本从jatcl的标准输出中读取数据 [英] How to read the data from standard output in jatcl by running tcl script in java

查看:35
本文介绍了如何通过在java中运行tcl脚本从jatcl的标准输出中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有两个问题.我正在使用 JACL 解释器在 JAVA 中运行 TCL 脚本.

I have two questions basically. I am using JACL interpreter to run TCL script in JAVA.

我的问题是:

  1. 如何在运行 tcl 文件后从标准输出中获取数据?

  1. How to get the data from standard output after running tcl file?

如何在使用 jatcl 执行时为 tcl 脚本传递参数?

How to pass arguments for tcl script when executing using jatcl?

例如:
样本.tcl:

E.G:
sample.tcl:

`puts "Hi this is from tcl"  `

当使用java如下运行时:

When ran using java as follows:

`Interp i = new Interp();
 i.eval("source sample.tcl");`

然后输出将写入 JAVA 的 stdout 控制台.我想将该输出读取到诸如 x[] = i.eval("sample.tcl") 之类的变量,然后 x 应该包含 Hi this is from tcl.

then the output will be written to stdout console of JAVA. I want to read that output to some variable like x[] = i.eval("sample.tcl") then x should contain Hi this is from tcl.

  1. 如何将一些参数传递给sample.tcl

推荐答案

实现标准输出有点困难 — 您必须做很多工作来创建自己的 tcl.lang.channel.Channel 然后用 TclIO.registerChannel() 插入它,这对于刚开始的人来说并不是真的——但如果你只是想交流,你通常不需要这样做使用 Tcl 程序.脚本的结果(不是标准输出,而是脚本中最后一个命令的结果)将通过Interp可用eval 完成后的 getResult() 方法.

Getting standard out is a bit difficult — you have to do a lot of work creating your own instance of a tcl.lang.channel.Channel and then plugging that in with TclIO.registerChannel(), which isn't really for someone just starting out — yet you often don't need to do that if you are just wanting to communicate with a Tcl program. The result of the script (which is not the standard output, but rather the result of the last command in the script) will be available via the Interp's getResult() method after the eval finishes.

Interp interp = new Interp();
interp.eval("source sample.tcl");
String result = interp.getResult().toString();

更有可能的是,一旦您sourced 脚本,您就会在同一个解释器中调用命令并检查它们的结果.这样效果更好.

More likely, once you've sourced the script, you'll then invoke commands in the same interpreter and inspect their results. That works much better.

Interp interp = new Interp();
interp.eval("source sample.tcl");
String arg = "abc";
interp.eval("sampleCommand " + arg);
String result = interp.getResult().toString();

如果你想传递一些更复杂的东西,最简单的方法是在调用脚本之前将你想传递的值存储在一个 Tcl 变量中,然后它可以在需要时从变量中取出值.

If you want to pass something more complex, the simplest way is to store the value you want to pass in a Tcl variable prior to invoking the script, which can then pull the value out of the variable when it needs it.

interp.setVar("theVar", "the value, which may have spaces in it", 0);
interp.eval("sampleCommand $theVar");

<小时>

如果你坚持阅读脚本标准输出,你可以试试这个:


If you're insisting on reading script stdout, you can try this:

Interp interp = new Interp();
TclByteArrayChannel tbac = new TclByteArrayChannel(interp);

// WARNING untested, but I think you have to unregister the old stdout first
TclIO.unregisterChannel(interp, TclIO.getStdChannel(StdChannel.STDOUT));
TclIO.registerChannel(interp, tbac);

interp.eval("source sample.tcl");

// The channel will have had *bytes* written to it, so that's what we ought to get
byte[] bytes = TclByteArray.getBytes(interp, tbac.getTclByteArray());

我真的不推荐这个.如果您使用任何其他语言(例如 C 或 C++)与 Tcl 交互,我也不推荐它.Tcl 被设计用于在我之前描述的解释器结果级别进行集成;你会发现处理复杂的事情要容易得多.

I truly do not recommend this. I also wouldn't recommend it if you were interacting with Tcl from any other language either (e.g., C or C++). Tcl is designed to integrate at the interpreter-result level that I described earlier; you'll find it much easier to make work for anything complex.

这篇关于如何通过在java中运行tcl脚本从jatcl的标准输出中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆