First, the compilation part
[Java] view plaincopy
public void Complier () throws IOException {
SYSTEM.OUT.PRINTLN ("* *-and start compiling Java source code ...");
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?) "* * * Compile succeeded:" + javasourcelist.get (i): "# # Compile Failed:" + javasourcelist.get (i));
}
SYSTEM.OUT.PRINTLN ("* * * and Java source code compilation completed. ");
}
[Java] view plaincopy
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 ());
}
}
}
}
Second, the packaging part
[Java] view plaincopy
public void Generatejar () throws FileNotFoundException, IOException {
SYSTEM.OUT.PRINTLN ("* * * and start generating jar packages ...");
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 ("* * * and the jar package is complete.) ");
}
[Java] view plaincopy
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 ();
}
}
Third, the use
[Java] view plaincopy
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 ();
}
Java code for compiling and packaging Java source files as jar files