zoukankan      html  css  js  c++  java
  • Java调用Bat

    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();
    	}
    }
    

      

  • 相关阅读:
    加了一句话
    由于数据库 'XXX' 离线,无法打开该数据库。
    Linux命令list
    js 数组去重
    JSON.parse(JSON.stringify()) 实现对对象的深度拷贝,从而互不影响
    docker
    Node child_process Study.2
    node assert模块 Study.1
    git 合并本地分支到远程分支
    Vue 项目搭建
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6812968.html
Copyright © 2011-2022 走看看