从Java连接到STDIN,STDOUT和STDERR以外的流

时间:2014-11-26 10:29:15

标签: java shell process

在由本机进程(Yate电话引擎的extmodule)通过shell脚本启动的Java进程中,可以连接到STDIN和STDOUT以与进程通信并将日志消息写入STDERR。

但是Yate的extmodule的描述提到了另外两个文件描述符3和4:

The external user application or script comunicates with the module trough several file descriptors:

    0 (stdin) - Carries commands and notifications from engine to application
    1 (stdout) - Carries commands and answers from application to the engine
    2 (stderr) - Has the usual meaning of reporting errors and is directed to the engine's stderr or logfile
    3 (optional) - Transports audio data from the engine to the application
    4 (optional) - Transports audio data from the application to the engine 

File descriptors 3 and 4 are open only for audio capable applications. 

是否可以连接到那些可选的文件描述符,如果可以,怎么样?我知道我可以将System.inSystem.outSystem.err用于0,1和2但是其他的呢?

1 个答案:

答案 0 :(得分:1)

如果Yate是另一个进程,并且你在Unix / Linux / Mac OS上运行(对于Windows它也可以运行,我根本就不知道),你可以重定向Yate' s文件描述符3和4到管道,然后使用Java来访问这些管道。

如果您有/proc文件系统或类似文件,它也可以通过/proc文件系统访问文件。

你说java是从Yate开始(即分叉)。在这种情况下,另一种可能性是" hacking" FileDescriptor。目前,FileDescriptor是一个伪枚举,只有三个实例inouterr,对应01和分别为2。 API本身可以创建其他有效的FileDescriptor实例,但使用本机代码来执行此操作。我不知道通过普通Java API打开数字文件描述符已知的文件的方法(这很难过,Oracle,你在听吗?!)。您可以编写自己的本机代码,但请注意,这基本上就像是心内直视手术。