thieumao

Chạy File trong File

Oct 14th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import javax.tools.JavaCompiler;
  9. import javax.tools.ToolProvider;
  10.  
  11. public class CompileOneFile {
  12.  
  13.     public static void main(String args[]) throws IOException {
  14.         // Tao File Test.java
  15.         GhiFileBanDau("C:/Test.java");
  16.         // Bien dich file Test.java thanh Test.class
  17.         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  18.         if (compiler == null) {
  19.             System.out.println("Làm ơn chỉ định java tool của JDK");
  20.             return;
  21.         }
  22.         int results = compiler.run(null, null, null, "C:/Test.java");
  23.         if (results == 0) {
  24.             System.out.println("Successfully");
  25.             //  Tao file chay.bat
  26.             GhiFile("C:/chay.bat");
  27.             // Chay file chay.bat
  28.             runCmd();
  29.         } else {
  30.             System.out.println("Compile fail…");
  31.         }
  32.     }
  33.  
  34.     public static void GhiFileBanDau(String strInput) {
  35.         try {
  36.             File f = new File(strInput);
  37.             FileWriter fw = new FileWriter(f);
  38.             BufferedWriter bw = new BufferedWriter(fw);
  39.             // Noi dung file
  40.             bw.write("import javax.swing.JOptionPane;public class Test {public static void main(String[] args) {JOptionPane.showMessageDialog(null, \"Thieu Mao\");}}");
  41.             bw.close();
  42.             fw.close();
  43.         } catch (Exception ex) {
  44.             System.out.println("Loi roi");
  45.         }
  46.     }
  47.  
  48.     public static void GhiFile(String strInput) {
  49.         try {
  50.             File f = new File(strInput);
  51.             FileWriter fw = new FileWriter(f);
  52.             BufferedWriter bw = new BufferedWriter(fw);
  53.             // Noi dung file
  54.             bw.write("C: \n cd/ \n java Test");
  55.             bw.close();
  56.             fw.close();
  57.         } catch (Exception ex) {
  58.             System.out.println("Loi roi");
  59.         }
  60.     }
  61.  
  62.     public static void runCmd() {
  63.         System.out.println("Begin...");
  64.         try {
  65.             Runtime rt = Runtime.getRuntime();
  66.             String[] cmd = {"C:\\chay.bat"};
  67.             System.out.println(cmd.length);
  68.             Process p = rt.exec(cmd);
  69.             BufferedReader in = new BufferedReader(new InputStreamReader(p
  70.                     .getInputStream()));
  71.             String line = null;
  72.             while ((line = in.readLine()) != null) {
  73.                 System.out.println(line);
  74.             }
  75.         } catch (IOException e) {
  76.             e.printStackTrace();
  77.         }
  78.         System.out.println("Final");
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment