利用java代码实现java源文件的编译和打包为jar文件
来源:互联网 发布:抑郁症的表现 知乎 编辑:程序博客网 时间:2024/05/24 01:13
一、编译部分
public void complier() throws IOException {System.out.println("*** --> 开始编译java源代码...");File javaclassDir = new File(javaClassPath);if (!javaclassDir.exists()) {javaclassDir.mkdirs();}List<String> javaSourceList = new ArrayList<String>();getFileList(new File(javaSourcePath), javaSourceList);JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();int result = -1;for (int i = 0; i < javaSourceList.size(); i++) {result = javaCompiler.run(null, null, null, "-d", javaClassPath, javaSourceList.get(i));System.out.println(result == 0 ? "*** 编译成功 : " + javaSourceList.get(i) : "### 编译失败 : " + javaSourceList.get(i));}System.out.println("*** --> java源代码编译完成。");}
private void getFileList(File file, List<String> fileList) throws IOException {if (file.isDirectory()) {File[] files = file.listFiles();for (int i = 0; i < files.length; i++) {if (files[i].isDirectory()) {getFileList(files[i], fileList);} else {fileList.add(files[i].getPath());}}}}
二、打包部分
public void generateJar() throws FileNotFoundException, IOException {System.out.println("*** --> 开始生成jar包...");String targetDirPath = targetPath.substring(0, targetPath.lastIndexOf("/"));File targetDir = new File(targetDirPath);if (!targetDir.exists()) {targetDir.mkdirs();}Manifest manifest = new Manifest();manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");JarOutputStream target = new JarOutputStream(new FileOutputStream(targetPath), manifest);writeClassFile(new File(javaClassPath), target);target.close();System.out.println("*** --> jar包生成完毕。");}
private void writeClassFile(File source, JarOutputStream target) throws IOException {BufferedInputStream in = null;try {if (source.isDirectory()) {String name = source.getPath().replace("\\", "/");if (!name.isEmpty()) {if (!name.endsWith("/")) {name += "/";}name = name.substring(javaClassPath.length());JarEntry entry = new JarEntry(name);entry.setTime(source.lastModified());target.putNextEntry(entry);target.closeEntry();}for (File nestedFile : source.listFiles())writeClassFile(nestedFile, target);return;}String middleName = source.getPath().replace("\\", "/").substring(javaClassPath.length());JarEntry entry = new JarEntry(middleName);entry.setTime(source.lastModified());target.putNextEntry(entry);in = new BufferedInputStream(new FileInputStream(source));byte[] buffer = new byte[1024];while (true) {int count = in.read(buffer);if (count == -1)break;target.write(buffer, 0, count);}target.closeEntry();} finally {if (in != null)in.close();}}
三、使用
public static void main(String[] args) throws IOException, InterruptedException {String currentDir = "c:/myProject";String javaSourcePath = currentDir + "/src/main/java/";String javaClassPath = currentDir + "/classes";String targetPath = currentDir + "/target/MyProject.jar";CompilerAndJarTools cl = new CompilerAndJarTools(javaSourcePath, javaClassPath, targetPath);cl.complier();cl.generateJar();}
0 0
- 利用java代码实现java源文件的编译和打包为jar文件
- linux下java多文件编译和jar打包
- java编译并打包成可执行jar和exe文件
- java命令行编译引入jar包的java源文件
- .java to .jar(java文件的打包)
- java 打包jar文件
- Java打包JAR文件
- jar 打包java文件
- Java打包成jar文件,以及jar文件的运行+将jar文件导出为exe文件(看图说)
- java实现超链接按钮JButton,可以打包为jar实现代码重
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- 6-7
- IO实例:复制文件
- mpdroot: open failed for root's mpd conf filempiexec&nbs
- QuestCentralforDB2 5.0 下载分享 附注册码
- Java中关于字符串拼接的问题
- 利用java代码实现java源文件的编译和打包为jar文件
- LPC1788 UART-DMA遇到的问题
- Construct Binary Tree from Preorder and Inorder Traversal
- 关于springmvc 学习的很好博客
- 对比度增强128*。。。
- 建站知识:域名/ 空间/ IP/ 端口之间的关系
- 对于POJ 1011的一点理解
- 读《浪潮之巅 下册》有感
- PL/SQL Developer结合oracle精简客户端使用配置