Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import javax.tools.JavaCompiler;
- import javax.tools.ToolProvider;
- public class CompileOneFile {
- public static void main(String args[]) throws IOException {
- // Tao File Test.java
- GhiFileBanDau("C:/Test.java");
- // Bien dich file Test.java thanh Test.class
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- if (compiler == null) {
- System.out.println("Làm ơn chỉ định java tool của JDK");
- return;
- }
- int results = compiler.run(null, null, null, "C:/Test.java");
- if (results == 0) {
- System.out.println("Successfully");
- // Tao file chay.bat
- GhiFile("C:/chay.bat");
- // Chay file chay.bat
- runCmd();
- } else {
- System.out.println("Compile fail…");
- }
- }
- public static void GhiFileBanDau(String strInput) {
- try {
- File f = new File(strInput);
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- // Noi dung file
- bw.write("import javax.swing.JOptionPane;public class Test {public static void main(String[] args) {JOptionPane.showMessageDialog(null, \"Thieu Mao\");}}");
- bw.close();
- fw.close();
- } catch (Exception ex) {
- System.out.println("Loi roi");
- }
- }
- public static void GhiFile(String strInput) {
- try {
- File f = new File(strInput);
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- // Noi dung file
- bw.write("C: \n cd/ \n java Test");
- bw.close();
- fw.close();
- } catch (Exception ex) {
- System.out.println("Loi roi");
- }
- }
- public static void runCmd() {
- System.out.println("Begin...");
- try {
- Runtime rt = Runtime.getRuntime();
- String[] cmd = {"C:\\chay.bat"};
- System.out.println(cmd.length);
- Process p = rt.exec(cmd);
- BufferedReader in = new BufferedReader(new InputStreamReader(p
- .getInputStream()));
- String line = null;
- while ((line = in.readLine()) != null) {
- System.out.println(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Final");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment