命令行运行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的更多相关文章
- 命令行运行Java程序时出现错误
在命令行运行Java程序时出现下面错误 Error: Could not create the Java Virtual Machine. Error: A fatal exception has o ...
- Java基础教程——命令行运行Java代码
视屏讲解:https://www.bilibili.com/video/av48196406/?p=4 命令行运行Java代码 (1)使用记事本新建文本文件[Test.java]. 注意,默认状态下W ...
- 通过命令行运行java出现"错误: 找不到或无法加载主类 "解决办法
首先在命令行运行不需要写package路径, 而在ide中一般是有路径的 so举例说明 例如程序名为HelloWorldTest.java,程序中含有package helloWorld语句,而该包位 ...
- Java 命令行运行java程序,出现“找不到或无法加载主类 ”的注意事项
引用:http://blog.chinaunix.net/uid-27106528-id-5209914.html 要在CMD命令行中使用java 运行java程序,关于出现 “找不到或无法加载主类 ...
- 在命令行运行java代码
因为尝试将运行结果通过管道命令保存,所以尝试在命令行(不借助lde来运行java代码,结果折腾了半天) 仿照的是eclipse创建文件目录的方式 最终解决方法是: #/bin/bash root_di ...
- java 学习 命令行运行java程序
之前学习python,由于公司需要.现在不得已转java. 1.命令运行java程序. 程序名字:input.java code: /** * Created by liumeide on 2017/ ...
- dos命令行运行.class源文件错误解决办法
dos命令行运行java源文件 public static void main(String[] args) throws IOException { // TODO Auto-generated m ...
- 含有package关键字的java文件在命令行运行报错
程序中含有package关键字,使用命令行运行程序时出现"找不到或无法加载主类",而使用Eclipse软件可以正常运行程序的可能解决办法. 在包下的类,在Java源文件的地方编译后 ...
- Java带包结构调用命令行运行编译
原文: https://www.toutiao.com/i6491809562037846542/ 带包结构调用命令行运行编译. 记事本编写两个简单的类 文件结构目录 启动DOS,进入文件所在目录 分 ...
随机推荐
- MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一。
MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一. 但是,如<InnoDB,5项最佳实践>所述,如果查询没有命中索引,也将退化为表锁. InnoDB的细粒度锁,是实现在索引记录 ...
- iOS指向函数的指针和block
一:block基础知识 block基础知识 基本概念:block是用来保存一段代码的:^:是block得标志 好比*:是指针的标志 特点:1:保存一段代码: 2:可以有参数和返回值: 3:可以作 ...
- git 以及 工作区 版本库 暂存区
https://www.jianshu.com/p/a308acded2ce 这个博客介绍的比较简单 https://blog.csdn.net/qq_31828515/arti ...
- 关于设置组件的state时遇到的一些问题
在使用react-bootstrap的时候设置showModel的值来控制Model的显示与隐藏,但是setState这个函数是异步的. 当你进行数据更新的时候,接着执行函数获取这个模态框的dom是获 ...
- Springboot - 在启动完成后执行特定方法
1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...
- BZOJ 4500: 矩阵
4500: 矩阵 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 326 Solved: 182[Submit][Status][Discuss] De ...
- codechef AUG17 T5 Chef And Fibonacci Array
Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ...
- Windows下C++多线程同步与互斥简单运用(转)
1. 互斥量,Mutex #include <Windows.h> #include <iostream> using namespace std; DWORD WINAPI ...
- win10下怎么配置以KDiff3作为merge tool和diff tool
系统环境: OS: Windows 10 Git 2.6.1.windows.1 KDiff3 0.9.98 (64 bit) 具体代码如下: git config --global --add me ...
- NGINX配置获取CloudFlare 下的访客真实IP并记录到日志
我用的是lnmp.org的环境 /usr/local/nginx/conf/nginx.conf 在 http { } 部分增加 map $HTTP_CF_CONNECTING_IP $clientR ...