zoukankan      html  css  js  c++  java
  • java如何调用shell

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class TestShell
    {
    
        public static void main(String[] args) throws IOException
        {
            try
            {
                String commands = "echo $PATH >> 1.txt";
                Process process = Runtime.getRuntime().exec(new String[] { "sh", "-c", commands });
    
                // for showing the info on screen
                InputStreamReader ir = new InputStreamReader(process.getInputStream());
                BufferedReader input = new BufferedReader(ir);
                String line;
                while ((line = input.readLine()) != null)
                {
    
                    System.out.println(line);
                }
            } catch (java.io.IOException e)
            {
    
                System.err.println("IOException " + e.getMessage());
    
            }
        }
    }

    参考 http://wangbaoaiboy.blog.163.com/blog/static/52111910201111892938552/

    java使用Runtime.exec执行linux命令重定向或管道问题: http://my.oschina.net/hetiangui/blog/133981

  • 相关阅读:
    3/10
    2/10
    福大软工 · 最终作业
    福大软工 · 第十二次作业
    Beta 冲刺(7/7)
    Beta 冲刺(6/7)
    Beta 冲刺(5/7)
    Beta 冲刺(4/7)
    Beta 冲刺(3/7)
    Beta 冲刺(2/7)
  • 原文地址:https://www.cnblogs.com/zhengchunhao/p/5742161.html
Copyright © 2011-2022 走看看