⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javacompiler.java

📁 用java编写的IDE程序示例
💻 JAVA
字号:
/*
 * Created on 2004-5-27
 */
package yuchifang.javaIDE.compilers;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.swing.JOptionPane;

import yuchifang.javaIDE.interfaces.ICompiler;
import yuchifang.javaIDE.interfaces.IExecCaller;

/**
 * @author yuchifang
 */
public class JavaCompiler extends Thread implements ICompiler
{
  private String exePath;
  private String filePath;
  private IExecCaller ec;  
  private JavaCompiler() { }
  
  public JavaCompiler(String path)
  {
    exePath = path;
  }
  
  public void run()
  {
    synchronized(JavaCompiler.class)
    { 
      ec.print("\r\n文件:[" + filePath + "]\r\n");
      ec.print("------------------开始编译[" + new Date() + "]------------------\r\n");
      long interval = System.currentTimeMillis();
          
      if (exePath == null)
      {
        int choice = JOptionPane.showConfirmDialog(
                        null,
                        "JDK配置出错\r\n请先配置JDK!",
                        "JDK未配置",
                        JOptionPane.OK_OPTION); //##用null合适吗?
        return;
      }
      String[] args = 
      {
        getExePath(),
        filePath
      };
      
      ProcessBuilder pb = new ProcessBuilder(args);
      pb.directory(new File(new File(filePath).getParent())); //有效吗?
      
      Process p = null;
      try
      {
        p = pb.start();
      } catch (IOException e)
      {
        e.printStackTrace();
        return;
      }
  
      ec.printResults(p.getInputStream());
      ec.printResults(p.getErrorStream());
      
      interval = System.currentTimeMillis() - interval;
      ec.print("\r\n------------------编译结束["+ new Date() + "],耗时[" + interval/1000.0 + "]秒------------------\r\n");
    }
  }
  
	public void compile(String filePath, IExecCaller ec)
	{
    this.filePath = filePath;
    this.ec = ec;
    start();
	}
  
  public void setExePath(String path)
  {
    exePath = path;
  }
  
  private String getExePath()
  {
    return exePath;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -