利用java代碼實現java源文件的編譯和打包爲jar文件

1、編譯部分java


[java] view plaincopy在CODE上查看代碼片派生到個人代碼片ide

  1.    

  2.   

  3. public void complier() throws IOException {  .net

  4.   

  5.     System.out.println("*** --> 開始編譯java源代碼...");  code

  6.   

  7.     File javaclassDir = new File(javaClassPath);  blog

  8.     if (!javaclassDir.exists()) {  ip

  9.         javaclassDir.mkdirs();  get

  10.     }  string

  11.   

  12.     List<String> javaSourceList = new ArrayList<String>();  it

  13.     getFileList(new File(javaSourcePath), javaSourceList);  io

  14.   

  15.     JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();  

  16.     int result = -1;  

  17.     for (int i = 0; i < javaSourceList.size(); i++) {  

  18.         result = javaCompiler.run(null, null, null, "-d", javaClassPath, javaSourceList.get(i));  

  19.         System.out.println(result == 0 ? "*** 編譯成功 : " + javaSourceList.get(i) : "### 編譯失敗 : " + javaSourceList.get(i));  

  20.     }  

  21.     System.out.println("*** --> java源代碼編譯完成。");  

  22. }  



[java] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1.     

  2. private void getFileList(File file, List<String> fileList) throws IOException {  

  3.   

  4.         if (file.isDirectory()) {  

  5.             File[] files = file.listFiles();  

  6.             for (int i = 0; i < files.length; i++) {  

  7.                 if (files[i].isDirectory()) {  

  8.                     getFileList(files[i], fileList);  

  9.                 } else {  

  10.                     fileList.add(files[i].getPath());  

  11.                 }  

  12.             }  

  13.         }  

  14.     }  

  15.      



2、打包部分

[java] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. public void generateJar() throws FileNotFoundException, IOException {  

  2.   

  3.     System.out.println("*** --> 開始生成jar包...");  

  4.     String targetDirPath = targetPath.substring(0, targetPath.lastIndexOf("/"));  

  5.     File targetDir = new File(targetDirPath);  

  6.     if (!targetDir.exists()) {  

  7.         targetDir.mkdirs();  

  8.     }  

  9.   

  10.     Manifest manifest = new Manifest();  

  11.     manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");  

  12.   

  13.     JarOutputStream target = new JarOutputStream(new FileOutputStream(targetPath), manifest);  

  14.     writeClassFile(new File(javaClassPath), target);  

  15.     target.close();  

  16.     System.out.println("*** --> jar包生成完畢。");  

  17. }  

  18.    


[java] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. private void writeClassFile(File source, JarOutputStream target) throws IOException {  

  2.     BufferedInputStream in = null;  

  3.     try {  

  4.         if (source.isDirectory()) {  

  5.             String name = source.getPath().replace("\\", "/");  

  6.             if (!name.isEmpty()) {  

  7.                 if (!name.endsWith("/")) {  

  8.                     name += "/";  

  9.                 }  

  10.                 name = name.substring(javaClassPath.length());  

  11.                 JarEntry entry = new JarEntry(name);  

  12.                 entry.setTime(source.lastModified());  

  13.                 target.putNextEntry(entry);  

  14.                 target.closeEntry();  

  15.             }  

  16.             for (File nestedFile : source.listFiles())  

  17.                 writeClassFile(nestedFile, target);  

  18.             return;  

  19.         }  

  20.   

  21.         String middleName = source.getPath().replace("\\", "/").substring(javaClassPath.length());  

  22.         JarEntry entry = new JarEntry(middleName);  

  23.         entry.setTime(source.lastModified());  

  24.         target.putNextEntry(entry);  

  25.         in = new BufferedInputStream(new FileInputStream(source));  

  26.   

  27.         byte[] buffer = new byte[1024];  

  28.         while (true) {  

  29.             int count = in.read(buffer);  

  30.             if (count == -1)  

  31.                 break;  

  32.             target.write(buffer, 0, count);  

  33.         }  

  34.         target.closeEntry();  

  35.     } finally {  

  36.         if (in != null)  

  37.             in.close();  

  38.     }  

  39. }  

  40.       


3、使用

[java] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. public static void main(String[] args) throws IOException, InterruptedException {  

  2.   

  3.     String currentDir = "c:/myProject";  

  4.     String javaSourcePath = currentDir + "/src/main/java/";  

  5.     String javaClassPath = currentDir + "/classes";  

  6.     String targetPath = currentDir + "/target/MyProject.jar";  

  7.   

  8.     CompilerAndJarTools cl = new CompilerAndJarTools(javaSourcePath, javaClassPath, targetPath);  

  9.     cl.complier();  

  10.     cl.generateJar();  

  11. }  

相關文章
相關標籤/搜索