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

  • 相关阅读:
    Docker 镜像
    Docker 安装命令
    Docker 基本概念
    Redis 高可用之"持久化"
    Git 安装和使用
    oracle角色
    oracle权限
    审计
    手动创建数据库
    oracle口令文件认证
  • 原文地址:https://www.cnblogs.com/zhengchunhao/p/5742161.html
Copyright © 2011-2022 走看看