java中如何使用jdk1.5和jdk1.6進行項目打包

1、jdk1.6中調用java的打包工具的關鍵代碼:

 /**
   * 編譯java類
   * @param file java文件列表
   * @param dir 編譯後calss文件存放目錄
   * @throws IOException
   * @throws ClassNotFoundException
   * @throws IllegalAccessException
   * @throws InstantiationException
   */
  public  void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
  {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  String version=System.getProperty("java.version");
  if(compiler==null)
  {//在運行環境中JRE不包含JDK中的工具包tools.jar必須重新加載JDK中LIB下面的tools.jar工具包
   System.out.println("load JavaCompiler...");
   compiler=Loader.loadCompile().newInstance();//加載編譯器並且實例化
   if(compiler==null)
   {//JDK 工具包tools.jar 沒有找到
    System.out.println("[../jdk"+version+"/lib/tools.jar] not found");
    new JOptionPane().showMessageDialog(null, "jdk"+version+"/lib/tools.jar未找到");
    System.exit(0);//程序退出
    //return;
   }
  }
  DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        //java文件管理器
  StandardJavaFileManager fileManager = compiler
    .getStandardFileManager(diagnostics, null, null);
  //指定class文件存放目錄
  //-d是指定編譯好的class文件目錄,-source指定兼容版本,-cp指定引用類文件,-target是編譯後的class文件的版本,-encoding是使用的編碼,-sourcepath是java源文件引用其他目錄下面的java源文件
  lib.append(LoaderLibrary.getLibraryAllFiles());
  Iterable<String> options=Arrays.asList("-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source);
  //設置編譯的java文件列表
  Iterable<? extends JavaFileObject> compilationUnits = fileManager
  .getJavaFileObjects(file);
  JavaCompiler.CompilationTask task = compiler.getTask(null,
    fileManager, diagnostics, options,
    null, compilationUnits);
 
  boolean rs=task.call();//執行編譯
  for(Diagnostic info:diagnostics.getDiagnostics())
  {//輸出日誌信息
   System.out.println("message:"+info.getMessage(null));
  }
  if(rs)
  {
   System.out.println("成功編譯了"+file.length+"個JAVA文件");
  }
  else
  {
   System.out.println("本次編譯失敗");
  }
  fileManager.close();
  }

 

2、jdk1.5中調用java的打包工具的關鍵代碼:

/**
   * 編譯java類
   * @param file java文件列表
   * @param dir 編譯後calss文件存放目錄
   * @throws IOException
   * @throws ClassNotFoundException
   * @throws IllegalAccessException
   * @throws InstantiationException
  * @throws 
  * @throws 
  * @throws SecurityException
  * @throws IllegalArgumentException
   */
  public  void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException
  {
   try {
   String version=System.getProperty("java.version");
   lib.append(LoaderLibrary.getLibraryAllFiles());
   String[] options=new String[]{"-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source};
   String[] list=processFile(file,options);
   Class compilerClass=loadCompile();//編譯器類
   Class contextClass=Class.forName("com.sun.tools.javac.util.Context",true,compilerClass.getClassLoader());
   Object compiler=compilerClass.getConstructor(String.class).newInstance("javac");//編譯器實例
   Method method=compilerClass.getMethod("compile", String[].class,contextClass);//編譯方法
   //指定class文件存放目錄
   int rs=(Integer)method.invoke(compiler, list,contextClass.newInstance());//執行編譯
  
   if(rs==0)
   {
    System.out.println("成功編譯了"+file.length+"個JAVA文件");
   }
   else
   {
    System.out.println("本次編譯失敗");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章