天天看點

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

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

  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的打包工具的關鍵代碼:

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

  }

  }