【问题标题】:How to access linux command such as sed,awk from java如何从 java 访问 sed、awk 等 linux 命令
【发布时间】:2014-03-14 14:51:11
【问题描述】:

自上个月以来我一直在研究 JNI(Java 本地接口)。这真的很有趣。我的意思是从 C 调用 Java 函数,反之亦然。现在我有一个意图调用我上面提到的 linux 命令,如 sed,awk 从Java side.also 我对 linux 中的 Sell Script 有一点了解。请给我一些提示如何做到这一点。

【问题讨论】:

  • ProcessBuilder、Runtime.exec() 等
  • 运行单独的二进制文件(相对于库函数)不需要 JNI。

标签: java linux bash


【解决方案1】:

您应该使用 Runtime.exec() 或 ProcessBuilder,具体取决于您在本地系统中执行进程所需的内容。

这些问题可能会有所帮助:

【讨论】:

    【解决方案2】:

    你可以使用这个 java sn-p 来运行一个 shell 命令:

    Process process = Runtime.getRuntime().exec("echo This is a test"); //Start the process
    process.waitFor(); //Wait for the process to terminate
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //Get the output
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 2015-12-26
      • 2014-08-22
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多