Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.side;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.jar.Attributes;
- import java.util.jar.JarEntry;
- import java.util.jar.JarOutputStream;
- import java.util.jar.Manifest;
- import javax.tools.JavaCompiler;
- import javax.tools.JavaFileObject;
- import javax.tools.StandardJavaFileManager;
- import javax.tools.ToolProvider;
- /**
- * Class to replace the functionality of the Sun JDK.
- * Currently implements a replacement for javac, javadoc, and jar.
- *
- */
- public class JDKClass {
- private String homeDir;
- /**
- * Default Constructor
- */
- public JDKClass() {
- }
- /**
- * Constructs a new JDKClass with the given absolute path to the home directory.
- *
- * @param homeDir Absolute path to home directory.
- */
- public JDKClass(String homeDir) {
- this.homeDir = homeDir;
- }
- /**
- * Gets the current home directory.
- *
- * @return Home Directory
- */
- public String getHomeDir() {
- return homeDir;
- }
- /**
- * Sets the home directory.
- *
- * @param homeDir Home Directory
- */
- public void setHomeDir(String homeDir) {
- this.homeDir = homeDir;
- }
- /**
- * Searches a directory recursively for java source files. Uses wildcards in paths
- *
- * @param buffer The buffer to append the path of the .java files in a directory, deliminated by ;
- * @param srcDir Directory File.
- */
- private static void findJavaRecursive(List<String> buffer, File srcDir) {
- File[] javaFiles = srcDir.listFiles();
- for (File file : javaFiles) {
- if (file.getName().endsWith(".java")) {
- buffer.add(file.getAbsolutePath());
- continue;
- }
- if (file.isDirectory()) {
- findJavaRecursive(buffer, file);
- continue;
- }
- }
- }
- private static void findRecursive(List<File> buffer, File dir) {
- File[] javaFiles = dir.listFiles();
- for (File file : javaFiles) {
- if (file.getName().endsWith(".class") || file.getName().endsWith(".java")) {
- buffer.add(file);
- continue;
- }
- if (file.isDirectory()) {
- findRecursive(buffer, file);
- continue;
- }
- }
- }
- /**
- * This compiles the java source files using the newer javax.tools.JavaCompiler
- * as the other is deprecated.
- *
- * @param srcDir The source directory.
- * @param binDir The build directory.
- * @param classPath The class path.
- * @return
- */
- public boolean compile(String srcDir, String binDir, String classPath) {
- boolean success = false;
- try {
- //Arrays.asList("-classpath", System.getProperty("java.class.path"));
- List<String> files = new LinkedList<String>();
- List<String> args = new LinkedList<String>();
- args.add("-cp");
- args.add(classPath.replaceAll("[:;]", System.getProperty("path.separator")));
- args.add("-d");
- args.add(homeDir + binDir);
- findJavaRecursive(files, new File(homeDir + srcDir));
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
- Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files);
- success = compiler.getTask(null, fileManager, null, args, null, compilationUnits).call();
- fileManager.close();
- } catch (IOException ex) {
- return false;
- }
- return success;
- }
- /**
- * This produces javadocs in the javadoc subdirectory of the home directory.
- *
- * @param srcDir The subdirectory of the home directory where the source files reside.
- * @return True if javadoc'n was succesful, false otherwise.
- */
- public boolean javaDoc(String srcDir) {
- if (homeDir == null) {
- return false;
- }
- List<String> args = new LinkedList<String>();
- args.add("-d");
- args.add(homeDir + "javadoc/");
- findJavaRecursive(args, new File(homeDir + srcDir));
- StringBuilder param = new StringBuilder().append("javadoc");
- for (String a : args) {
- param.append(' ').append(a);
- }
- System.out.println(param);
- int exitCode = com.sun.tools.javadoc.Main.execute(args.toArray(new String[0]));
- return exitCode == 0;
- }
- /**
- * This produces a jar file in the home directory, complete with a manifest attribute of Main-Class:
- *
- * @param name Name of the jar file to create.
- * @param binDir The first subdirectory of home directory of whose content you want in the jar file.
- * @param srcDir The second subdirectory of home directory of whose content you want in the jar file.
- * @param mainClass Main Class to put in the manifest.
- * @return True if the jar was created succesfully, false otherwise.
- */
- public boolean jar(String name, String binDir, String mainClass, String srcDir) {
- if (homeDir == null) {
- return false;
- }
- if (!name.endsWith(".jar")) {
- name += ".jar";
- }
- File archiveFile = new File(homeDir + name);
- List<File> toBeJaredList = new LinkedList<File>();
- if (binDir != null) {
- findRecursive(toBeJaredList, new File(homeDir + binDir));
- }
- if (srcDir != null) {
- findRecursive(toBeJaredList, new File(homeDir + srcDir));
- }
- try {
- byte[] buffer = new byte[10240];
- FileOutputStream stream = new FileOutputStream(archiveFile);
- Manifest manifest = new Manifest();
- if (mainClass != null) {
- manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
- manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
- }
- JarOutputStream out = new JarOutputStream(stream, manifest);
- for (File tobeJared : toBeJaredList) {
- if (tobeJared == null || !tobeJared.exists()
- || tobeJared.isDirectory()) {
- continue;
- }
- System.out.println("Compressing and adding " + tobeJared.getName());
- JarEntry jarAdd = new JarEntry(tobeJared.getName());
- jarAdd.setTime(tobeJared.lastModified());
- out.putNextEntry(jarAdd);
- FileInputStream in = new FileInputStream(tobeJared);
- while (true) {
- int nRead = in.read(buffer, 0, buffer.length);
- if (nRead <= 0) {
- break;
- }
- out.write(buffer, 0, nRead);
- }
- in.close();
- }
- out.close();
- stream.close();
- return true;
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement