菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Test3
{
	static final String path = "D:\\develop\\test.bat";

	public static void main(String[] args)
			throws IOException, InterruptedException
	{
		 printJava();
		createBat();
		Process p = null;
		File f = new File("d:");
		p = Runtime.getRuntime().exec(new String[] { "cmd", "/c", path },null,f);
		p.waitFor();
		System.out.print(p.exitValue());
		printf(p.getInputStream());
	}

	static void printf(InputStream out) throws IOException
	{
		String line = null;
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(out, "gbk"));
		while ((line = reader.readLine()) != null)
		{
			System.out.println(line);
		}
	}

	static void createBat() throws IOException
	{
		File file = new File(path);
		if (file.exists())
		{
			file.delete();
		}
		file.createNewFile();
		PrintWriter pw = new PrintWriter(file);
		pw.println("echo start");
		pw.println("pwd");
		pw.println("cd %JAVA_HOME%");
		pw.println("cd ..");
		pw.println("cd jre");
		pw.println("pwd");
		pw.println("ls");
		pw.println("javac Test.java");
		pw.println("clear");
		pw.println("for /L %%i in (0,1,2) do start java Test");
		pw.flush();
		pw.close();
	}
	
	static void printJava() throws IOException
	{
		String home = System.getProperty("java.home");
		System.out.print(home);
		File dir = new File(home);
		File file = new File(dir,"Test.java");
		if(file.exists())
			file.delete();
		file.createNewFile();
		PrintWriter pw = new PrintWriter(file);
		pw.println("public  class Test");
		pw.println("{");
			pw.println("public static void main(String[] args)");
			pw.println("{");
				 	pw.println("System.out.println(\"hello world\");");
				 	pw.println("int i = 0;");
				 	pw.println("while(i++ < 100000) System.out.println(\"hello world\");");
			 pw.println("}");
		pw.println("}");
		pw.flush();
		pw.close();
	}
}

  

posted on 2017-05-05 14:49  好吧,就是菜菜  阅读(356)  评论(0编辑  收藏  举报