运行TestLinuxCommand.java
 
 
代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
/**
*
* @author nathan
*/
public class TestLinuxCommand {
     public static void main(String[] args) {
         String logDir = System.getProperty("log.dir");
        
         //windows
//         String commands = "cmd.exe /c c: && dir";
        
//         String commands = "ls -l";
//         String commands = "sh -c cd /data/log/daemon/commonprocess && ls -lt";
         String[]   commands   =   new   String[]   {"/bin/sh", "-c", "/bin/ls -lt /data/log/daemon/commonprocess"};
//         String[] commands = new String[]{"cmd","dir"};
        
         java.lang.Process process = null;        
     try
          {
              process = Runtime.getRuntime().exec(commands);
              System.out.println("exec commands success");
              InputStreamReader inputStream = new InputStreamReader(process.getInputStream());
              BufferedReader input = new BufferedReader(inputStream);
              String line;
              System.out.println("print inputStream start");
              while ((line = input.readLine()) != null){
                   System.out.println(line);
              }
              System.out.println("print inputStream over");
             
              InputStreamReader errorStream = new InputStreamReader(process.getErrorStream());
              input = new BufferedReader(errorStream);
              System.out.println("print errorStream start");
              while ((line = input.readLine()) != null){
                   System.out.println(line);
                   input.close();
              }
              System.out.println("print errorStream over");
        
          } catch (IOException e)
          {
               System.out.println("have ioexception");
               e.printStackTrace();
          } finally{
               try
               {
                    System.out.println(process.waitFor());
               } catch (InterruptedException e)
               {
                    e.printStackTrace();
               }
          }
     }
}
 
 
 
命令

java -classpath .:/usr/java/jdk1.6.0_26/lib/* TestLinuxCommand

javac TestLinuxCommand.java

命令行运行java的更多相关文章

  1. 命令行运行Java程序时出现错误

    在命令行运行Java程序时出现下面错误 Error: Could not create the Java Virtual Machine. Error: A fatal exception has o ...

  2. Java基础教程——命令行运行Java代码

    视屏讲解:https://www.bilibili.com/video/av48196406/?p=4 命令行运行Java代码 (1)使用记事本新建文本文件[Test.java]. 注意,默认状态下W ...

  3. 通过命令行运行java出现"错误: 找不到或无法加载主类 "解决办法

    首先在命令行运行不需要写package路径, 而在ide中一般是有路径的 so举例说明 例如程序名为HelloWorldTest.java,程序中含有package helloWorld语句,而该包位 ...

  4. Java 命令行运行java程序,出现“找不到或无法加载主类 ”的注意事项

    引用:http://blog.chinaunix.net/uid-27106528-id-5209914.html 要在CMD命令行中使用java 运行java程序,关于出现 “找不到或无法加载主类 ...

  5. 在命令行运行java代码

    因为尝试将运行结果通过管道命令保存,所以尝试在命令行(不借助lde来运行java代码,结果折腾了半天) 仿照的是eclipse创建文件目录的方式 最终解决方法是: #/bin/bash root_di ...

  6. java 学习 命令行运行java程序

    之前学习python,由于公司需要.现在不得已转java. 1.命令运行java程序. 程序名字:input.java code: /** * Created by liumeide on 2017/ ...

  7. dos命令行运行.class源文件错误解决办法

    dos命令行运行java源文件 public static void main(String[] args) throws IOException { // TODO Auto-generated m ...

  8. 含有package关键字的java文件在命令行运行报错

    程序中含有package关键字,使用命令行运行程序时出现"找不到或无法加载主类",而使用Eclipse软件可以正常运行程序的可能解决办法. 在包下的类,在Java源文件的地方编译后 ...

  9. Java带包结构调用命令行运行编译

    原文: https://www.toutiao.com/i6491809562037846542/ 带包结构调用命令行运行编译. 记事本编写两个简单的类 文件结构目录 启动DOS,进入文件所在目录 分 ...

随机推荐

  1. windows部分快捷键及terminal命令

    切换任务: win+tab terminal命令 pwd:打印当前工作目录 hostname:获取我的计算机的网络名称 mkdir:创建目录 cd:更改目录 ls/ dir -R:列出目录下的文件 r ...

  2. git filter-branch应用

    1.修改author和committer git filter-branch --commit-filter ' export GIT_AUTHOR_EMAIL=me@example.com; exp ...

  3. 0-Broadcast机制原理简要介绍

    Broadcast机制简要介绍 来源: http://blog.csdn.net/luoshengyang/article/details/6730748 导语 广播机制在Android系统中,也不算 ...

  4. Codeforces Round #364 (Div. 2) A 水

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. 【HDOJ5956】The Elder(树形DP,斜率优化)

    题意:有一棵n个点的有根树,每条边上有一个边权.给定P,从i跳到它的祖先j的费用是距离的平方+P,问所有点中到根节点1的总花费最大值 n<=1e5,p<=1e6,w<=1e2 思路: ...

  6. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  7. layui 的Tab选项卡

    http://www.layui.com/doc/element/tab.html <#--start--> <div class="layui-tab layui-tab ...

  8. phpexcel--导入excel表格

    最近在做一个小项目,就是一个管理信息的小系统:要求有导入和导出的信息为excel的功能,研究过导入导出功能的肯定知道导出要比导入的简单多了,导入用的phpexcel,当时对phpexcel是完全不了解 ...

  9. mysql5.7 mysql库下面的user表没有password字段无法修改密码

    如题所述,mysql5.7  mysql库下面的user表没有password字段无法修改密码, 5.7版本已经不再使用password来作为密码的字段了  而改成了authentication_st ...

  10. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...