利用java代码实现java源文件的编译和打包为jar文件

一、编译部分


[java] view plaincopy在CODE上查看代码片派生到我的代码片

  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(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.      



二、打包部分

[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.       


三、使用

[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. }  



转载于:https://my.oschina.net/sniperLi/blog/523631

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值