java原生API生成Jar包

来源:互联网 发布:人工智能的英语短文 编辑:程序博客网 时间:2024/06/06 07:20

一、编译部分


[java] view plain copy
  1.    
  2.   
  3. public void complier() throws IOException {  
  4.   
  5.     System.out.println("*** --> 开始编译java源代码...");  
  6.   
  7.     File javaclassDir = new File(javaClassPath);  
  8.     if (!javaclassDir.exists()) {  
  9.         javaclassDir.mkdirs();  
  10.     }  
  11.   
  12.     List<String> javaSourceList = new ArrayList<String>();  
  13.     getFileList(new File(javaSourcePath), javaSourceList);  
  14.   
  15.     JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();  
  16.     int result = -1;  
  17.     for (int i = 0; i < javaSourceList.size(); i++) {  
  18.         result = javaCompiler.run(nullnullnull"-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 plain copy
  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.      


二、打包部分

[java] view plain copy
  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 plain copy
  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.       

三、使用

[java] view plain copy
  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. }  
原创粉丝点击