Execute a system command in Java

Execute a system command – Rosetta Code.

Java

Works withJava version 1.5+
import java.util.Scanner;
import java.io.*;
 
public class Program {
    public static void main(String[] args) {    	
    	try {
    		Process p = Runtime.getRuntime().exec("cmd /C dir");//Windows command, use "ls -oa" for UNIX
    		Scanner sc = new Scanner(p.getInputStream());    		
    		while (sc.hasNext()) System.out.println(sc.nextLine());
    	}
    	catch (IOException e) {
    		System.out.println(e.getMessage());
    	}
    }
}
Works withJava version 1.4+

There are two ways to run system commands. The simple way, which will hang the JVM (I would be interested in some kind of reason). — this happens because the the inputStream buffer fills up and blocks until it gets read. Moving your .waitFor after reading the InputStream would fix your issue (as long as your error stream doesn’t fill up)

import java.io.IOException;
import java.io.InputStream;
 
public class MainEntry {
    public static void main(String[] args) {
        executeCmd("ls -oa");
    }
 
    private static void executeCmd(String string) {
        InputStream pipedOut = null;
        try {
            Process aProcess = Runtime.getRuntime().exec(string);
            aProcess.waitFor();
 
            pipedOut = aProcess.getInputStream();
            byte buffer[] = new byte[2048];
            int read = pipedOut.read(buffer);
            // Replace following code with your intends processing tools
            while(read >= 0) {
                System.out.write(buffer, 0, read);
 
                read = pipedOut.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } finally {
            if(pipedOut != null) {
                try {
                    pipedOut.close();
                } catch (IOException e) {
                }
            }
        }
    }
 
 
}

And the right way, which uses threading to read the InputStream given by the process.

import java.io.IOException;
import java.io.InputStream;
 
public class MainEntry {
    public static void main(String[] args) {
        // the command to execute
        executeCmd("ls -oa");
    }
 
    private static void executeCmd(String string) {
        InputStream pipedOut = null;
        try {
            Process aProcess = Runtime.getRuntime().exec(string);
 
            // These two thread shall stop by themself when the process end
            Thread pipeThread = new Thread(new StreamGobber(aProcess.getInputStream()));
            Thread errorThread = new Thread(new StreamGobber(aProcess.getErrorStream()));
 
            pipeThread.start();
            errorThread.start();
 
            aProcess.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }
}
 
//Replace the following thread with your intends reader
class StreamGobber implements Runnable {
 
    private InputStream Pipe;
 
    public StreamGobber(InputStream pipe) {
        if(pipe == null) {
            throw new NullPointerException("bad pipe");
        }
        Pipe = pipe;
    }
 
    public void run() {
        try {
            byte buffer[] = new byte[2048];
 
            int read = Pipe.read(buffer);
            while(read >= 0) {
                System.out.write(buffer, 0, read);
 
                read = Pipe.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(Pipe != null) {
                try {
                    Pipe.close();
                } catch (IOException e) {
                }
            }
        }
    }
}


10款实用的Ajax/JavaScript编码工具推荐 – Web开发 – 软件研发频道 – CSDN.NET

10款实用的Ajax/JavaScript编码工具推荐 – Web开发 – 软件研发频道 – CSDN.NET.

JavaScript和Ajax(Asynchronous JavaScript and XML,异步JavaScript和XML)可以为HTML页面添加各种交互和动态效果,让你的网站更加引人注目。本文为你整理了10款Ajax和JavaScript相关的编码工具,非常实用!

1.  Clean AJAX

这是一个开源的、跨浏览器的Ajax引擎,灵感来自Java Message Service(Java消息服务),使用消息控制请求。

2.  Aptana Studio

这是一个完整的Web开发环境,整合了针对HTML、CSS和JavaScript的强大的编码工具,拥有数千款由社区开发的附加插件。

3.  Spket IDE

这是一个用于JavaScript和XML开发的工具包,其中JavaScript编辑器提供了代码补全、语法高亮和内容大纲等功能,帮助开发者高效地编写JavaScript代码。该工具对于非商业用途是免费的。

4.  Komodo Edit

支持PHP、Python、Ruby、Perl、Tcl、JavaScript、CSS、HTML以及模板语言(比如RHTML、Template-Toolkit、HTML-Smarty 和Django等)。

5.  WaveMaker – Development Platform

WaveMaker(前身为ActiveGrid)是一个开源的软件开发平台,可以使Java Web和云应用程序的创建过程更加自动化。

6.  Qooxdoo

这是一个用于创建富互联网应用(RIA)的框架,包括一个平台独立的开发工具链、一个高水准的GUI工具以及一个先进的客户端-服务器通信层。

7.  Aptana Jaxer

允许开发者使用他们的AJAX、HTML、JavaScript和DOM技能来创建服务器端Web应用。

8.  Script# – AJAX and JavaScript Authoring Tool

Script#把C#开发者的体验(编程和工具)带到了JavaScript/Ajax世界中,使用该工具你可以先编写C#源码,然后编译成脚本,即可在所有的现代浏览器中运行。

9.  Jx – JavaScript Library

Jx是一个JavaScript库,用于创建针对MooTools库的图形用户界面(GUI)。

10.  JQuery UI – ThemeRoller

jQuery UI是基于jQuery的用户界面库,它提供了一组核心交互插件、UI部件、使用jQuery风格的可视化效果、事件驱动架构等,旨在提升Web标准、可访问性、样式灵活性以及设计友好性。