获取java程序执行系统命令返回的字符串

比如在dos下执行 java,dos控制台会显示对应的执行内容,那么在java程序中,怎么获取命令执行后的内容呢,具体代码如下:

	public static String exec(String command) {
		BufferedReader br = null;
		InputStreamReader reader = null;
		StringBuffer bur = new StringBuffer();
		try {
			Runtime runtime = Runtime.getRuntime();
			Process proc = runtime.exec(command);
			reader = new InputStreamReader(proc.getInputStream());
			
			br = new BufferedReader(reader);
			String line= null;
			while ((line=br.readLine()) != null) {
				System.out.println(line);// 换行输出
				bur.append(line);
			}
			proc.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return bur.toString().trim();
	}

 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Test.exec("java"));// 执行java命令,一行输出
		
	}

 执行之后,会得到如下内容:

Usage: java [-options] class [args...]

           (to execute a class)

   or  java [-options] -jar jarfile [args...]

           (to execute a jar file)

 

where options include:

    -client  to select the "client" VM

    -server  to select the "server" VM

    -hotspot  is a synonym for the "client" VM  [deprecated]

                  The default VM is client.

                  

    -cp <class search path of directories and zip/jar files>

    -classpath <class search path of directories and zip/jar files>

                  A ; separated list of directories, JAR archives,

                  and ZIP archives to search for class files.

    -D<name>=<value>

                  set a system property

    -verbose[:class|gc|jni]

                  enable verbose output

    -version      print product version and exit

    -version:<value>

                  require the specified version to run

    -showversion  print product version and continue

    -jre-restrict-search | -jre-no-restrict-search

                  include/exclude user private JREs in the version search

    -? -help      print this help message

    -X            print help on non-standard options

    -ea[:<packagename>...|:<classname>]

    -enableassertions[:<packagename>...|:<classname>]

                  enable assertions

    -da[:<packagename>...|:<classname>]

    -disableassertions[:<packagename>...|:<classname>]

                  disable assertions

    -esa | -enablesystemassertions

                  enable system assertions

    -dsa | -disablesystemassertions

                  disable system assertions

    -agentlib:<libname>[=<options>]

                  load native agent library <libname>, e.g. -agentlib:hprof

                    see also, -agentlib:jdwp=help and -agentlib:hprof=help

    -agentpath:<pathname>[=<options>]

                  load native agent library by full pathname

    -javaagent:<jarpath>[=<options>]

                  load Java programming language agent, see java.lang.instrument

    -splash:<imagepath>

                  show splash screen with specified image

Usage: java [-options] class [args...]           (to execute a class)   or  java [-options] -jar jarfile [args...]           (to execute a jar file)where options include:    -client  to select the "client" VM    -server  to select the "server" VM    -hotspot  is a synonym for the "client" VM  [deprecated]                  The default VM is client.                      -cp <class search path of directories and zip/jar files>    -classpath <class search path of directories and zip/jar files>                  A ; separated list of directories, JAR archives,                  and ZIP archives to search for class files.    -D<name>=<value>                  set a system property    -verbose[:class|gc|jni]                  enable verbose output    -version      print product version and exit    -version:<value>                  require the specified version to run    -showversion  print product version and continue    -jre-restrict-search | -jre-no-restrict-search                  include/exclude user private JREs in the version search    -? -help      print this help message    -X            print help on non-standard options    -ea[:<packagename>...|:<classname>]    -enableassertions[:<packagename>...|:<classname>]                  enable assertions    -da[:<packagename>...|:<classname>]    -disableassertions[:<packagename>...|:<classname>]                  disable assertions    -esa | -enablesystemassertions                  enable system assertions    -dsa | -disablesystemassertions                  disable system assertions    -agentlib:<libname>[=<options>]                  load native agent library <libname>, e.g. -agentlib:hprof                    see also, -agentlib:jdwp=help and -agentlib:hprof=help    -agentpath:<pathname>[=<options>]                  load native agent library by full pathname    -javaagent:<jarpath>[=<options>]                  load Java programming language agent, see java.lang.instrument    -splash:<imagepath>                  show splash screen with specified image

跟在dos看到的内容一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值