Java code for compiling and packaging Java source files as jar files

Source: Internet
Author: User

First, the compilation part


[Java] view plaincopy

  1. public void Complier () throws IOException {

  2. SYSTEM.OUT.PRINTLN ("* *-and start compiling Java source code ...");

  3. File Javaclassdir = new file (Javaclasspath);

  4. if (!javaclassdir.exists ()) {

  5. Javaclassdir.mkdirs ();

  6. }

  7. list<string> javasourcelist = new arraylist<string> ();

  8. Getfilelist (New File (Javasourcepath), javasourcelist);

  9. Javacompiler Javacompiler = Toolprovider.getsystemjavacompiler ();

  10. int result =-1;

  11. for (int i = 0; i < javasourcelist.size (); i++) {

  12. result = Javacompiler.run (null, NULL, NULL, "-D", Javaclasspath, Javasourcelist.get (i));

  13. System.out.println (Result = = 0?) "* * * Compile succeeded:" + javasourcelist.get (i): "# # Compile Failed:" + javasourcelist.get (i));

  14. }

  15. SYSTEM.OUT.PRINTLN ("* * * and Java source code compilation completed. ");

  16. }



[Java] view plaincopy

  1. private void Getfilelist (file file, list<string> fileList) throws IOException {

  2. if (File.isdirectory ()) {

  3. file[] files = file.listfiles ();

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

  5. if (Files[i].isdirectory ()) {

  6. Getfilelist (Files[i], fileList);

  7. } else {

  8. Filelist.add (Files[i].getpath ());

  9. }

  10. }

  11. }

  12. }



Second, the packaging part

[Java] view plaincopy

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

  2. SYSTEM.OUT.PRINTLN ("* * * and start generating jar packages ...");

  3. String Targetdirpath = targetpath.substring (0, Targetpath.lastindexof ("/"));

  4. File TargetDir = new file (Targetdirpath);

  5. if (!targetdir.exists ()) {

  6. Targetdir.mkdirs ();

  7. }

  8. Manifest Manifest = new Manifest ();

  9. Manifest.getmainattributes (). Put (Attributes.Name.MANIFEST_VERSION, "1.0");

  10. Jaroutputstream target = new Jaroutputstream (new FileOutputStream (TargetPath), manifest);

  11. Writeclassfile (New File (Javaclasspath), target);

  12. Target.close ();

  13. SYSTEM.OUT.PRINTLN ("* * * and the jar package is complete.) ");

  14. }


[Java] view plaincopy

  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. String middlename = Source.getpath (). replace ("\ \", "/"). Substring (Javaclasspath.length ());

  21. Jarentry entry = new Jarentry (middlename);

  22. Entry.settime (Source.lastmodified ());

  23. Target.putnextentry (entry);

  24. in = new Bufferedinputstream (new FileInputStream (source));

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

  26. while (true) {

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

  28. if (count = =-1)

  29. Break

  30. Target.write (buffer, 0, count);

  31. }

  32. Target.closeentry ();

  33. } finally {

  34. if (in = null)

  35. In.close ();

  36. }

  37. }


Third, the use

[Java] view plaincopy

  1. public static void Main (string[] args) throws IOException, Interruptedexception {

  2. String Currentdir = "C:/myproject";

  3. String Javasourcepath = currentdir + "/src/main/java/";

  4. String Javaclasspath = currentdir + "/classes";

  5. String TargetPath = currentdir + "/target/myproject.jar";

  6. Compilerandjartools cl = new Compilerandjartools (Javasourcepath, Javaclasspath, TargetPath);

  7. Cl.complier ();

  8. Cl.generatejar ();

  9. }



Java code for compiling and packaging Java source files as jar files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.