1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

¿Como ingresar datos desde la terminal en el ClassPath de Java?

Discussion in 'Programming/Internet' started by Nacho Zve De La Torre, Oct 8, 2018.

  1. Este es mi programa Java:

    public class ComponentCompare {

    private static BufferedWriter BW;
    private static final JSONObject ROOT = new JSONObject();
    private static final JSONObject COMPAREMENT = new JSONObject();
    private static final JSONArray DIFF = new JSONArray();

    // private static int numDir = 0;
    // private static int numFiles = 0;
    public static void main(String[] args) {

    try {
    File rootDir = new File("/home/incentivate/Desktop/resources");
    ROOT.put("root", rootDir.getAbsolutePath());
    JSONArray content = new JSONArray();
    ROOT.put("content", content);

    generateReport(rootDir, content);
    //System.out.println("root " + root);
    // System.out.println(rootDir.toString() + " Carpetas: " + numDir + ", Archivos: " + numFiles);

    System.out.println("\n\n\n\n\n\n\n\n");

    File dirToCompare = new File("/home/incentivate/Desktop/th_watson_chatbot");
    COMPAREMENT.put("root", dirToCompare.getAbsolutePath());
    JSONArray compareContent = new JSONArray();
    COMPAREMENT.put("content", compareContent);
    generateReport(dirToCompare, compareContent);
    System.out.println("\n\n\n\n\n\n");

    JSONObject reportComparison = compareContent(ROOT, COMPAREMENT, DIFF);
    System.out.println("\n\n\n Comparison: " + reportComparison);
    //
    // File salida = new File("/home/incentivate/Desktop/resources/salida.txt");
    // BW = new BufferedWriter(new FileWriter(salida));
    //// bw.write("Se han encontrado: " + numDir + " directorios \n");
    //// bw.write("Se han encontrado: " + numFiles + " archivos \n");
    // BW.write(ROOT.toString());
    // BW.close();
    } catch (IOException e) {
    System.out.println("No existe la ruta de entrada!");
    } catch (ParseException ex) {
    Logger.getLogger(ComponentCompare.class.getName()).log(Level.SEVERE, null, ex);
    }

    } // end of main()

    public static String md5OfString(String input) {
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] messageDigest = md.digest(input.getBytes());
    BigInteger number = new BigInteger(1, messageDigest);
    String hashtext = number.toString(16);

    while (hashtext.length() < 32) {
    hashtext = "0" + hashtext;
    }
    return hashtext;
    } catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
    }
    } // end of getMD5()

    public static String md5OfFile(File file) {
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream fs = new FileInputStream(file);
    BufferedInputStream bs = new BufferedInputStream(fs);
    byte[] buffer = new byte[1024];
    int bytesRead;

    while ((bytesRead = bs.read(buffer, 0, buffer.length)) != -1) {
    md.update(buffer, 0, bytesRead);
    }
    byte[] digest = md.digest();

    StringBuilder sb = new StringBuilder();
    for (byte bite : digest) {
    sb.append(String.format("%02x", bite & 0xff));
    }
    return sb.toString();
    } catch (NoSuchAlgorithmException | IOException ex) {
    Logger.getLogger(ComponentCompare.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
    } // end of md5OfFile()

    public static void generateReport(File dir, JSONArray content) throws IOException, ParseException {
    File listFile[] = dir.listFiles();
    String md5;
    if (listFile != null && listFile.length > 0) {
    for (int i = 0; i < listFile.length; i++) {
    JSONObject obj = new JSONObject();
    content.add(obj);
    if (listFile.isDirectory()) {
    md5 = md5OfString(listFile.getAbsolutePath());
    obj.put("md5", md5);
    obj.put("type", "folder");
    obj.put("path", listFile.getAbsolutePath());
    JSONArray contentSon = new JSONArray();
    obj.put("content", contentSon);
    generateReport(listFile, contentSon);
    //numDir++;
    } else if (listFile.isFile()) {
    md5 = md5OfFile(listFile);
    obj.put("md5", md5);
    obj.put("type", "file");
    obj.put("name", listFile.getAbsolutePath());
    //numFiles++;
    }
    }
    }
    } // end of generateReport()

    public static JSONObject compareContent(JSONObject root, JSONObject compare, JSONArray diff) throws IOException, ParseException {
    JSONObject differences = new JSONObject();
    JSONObject folderDifferences = new JSONObject();
    JSONObject fileDifferences = new JSONObject();
    differences.put("root", root.get("root"));
    differences.put("origin-report", compare.get("root"));
    differences.put("diff", diff);
    System.out.println(differences);
    JSONArray rootContent = (JSONArray) root.get("content");
    JSONArray compareContent = (JSONArray) compare.get("content");
    for (Object o : rootContent) {
    JSONObject ob = (JSONObject) o;
    //System.out.println("OB " + ob);
    System.out.println("\n\n\n");
    if (ob.get("type") == "file") {
    String fileName = (String) ob.get("name");
    String fileMd5 = (String) ob.get("md5");
    System.out.println("File encontrado " + fileName);
    JSONObject fileInDestination = getFirstElementFromContentByName(compareContent, fileName);
    if (fileInDestination != null) {
    System.out.println("Found file " + fileName + " in destination.");
    if (fileMd5.equals(fileInDestination.get("md5"))) {
    System.out.println("Original file MD5 is equal to destination file MD5 for " + fileName);
    } else {
    System.out.println("Original file MD5 is DIFFERENT to destination file MD5 for " + fileName);
    }
    } else {
    System.out.println("Did NOT found file " + fileName + " in destination.");
    fileDifferences.put("name", fileName);
    fileDifferences.put("origin-md5", fileMd5);
    fileDifferences.put("type", ob.get("type"));
    diff.add(fileDifferences);
    }
    } else if (ob.get("type") == "folder") {
    String path = (String) ob.get("path");
    System.out.println("Found folder " + path);
    JSONObject folderInDestination = getFirstElementFromContentByName(compareContent, path);
    if (folderInDestination != null) {
    int elementCount = ob.size();
    if (elementCount > 0) {
    System.out.println("Folder has " + elementCount + " elements inside.");
    compareContent(ob, folderInDestination, diff);
    }
    } else {
    System.out.println("Did NOT found folder " + path + " in destination.");
    folderDifferences.put("origin-md5", ob.get("md5"));
    folderDifferences.put("type", ob.get("type"));
    folderDifferences.put("path", path);
    diff.add(folderDifferences);
    }
    }
    }// end of for
    return differences;
    } // end of compareContent()

    public static JSONObject getFirstElementFromContentByName(JSONArray content, String _name) {
    if (content == null) {
    return null;
    }
    JSONArray contentFilter = new JSONArray();
    for (Object o : content) {
    JSONObject ob = (JSONObject) o;
    if (ob.get("type") == "folder") {
    if (ob.get("path").equals(_name)) {
    contentFilter.add(ob);
    }
    } else if (ob.get("type") == "file") {
    if (ob.get("name").equals(_name)) {
    contentFilter.add(ob);
    }
    }
    }
    if (contentFilter.isEmpty()) {
    return null;
    } else {
    return (JSONObject) contentFilter.get(0);
    }
    } // end of getFirstElementFromContentByName()

    }// end of ComponentCompare class


    Funcionamiento:

    Primero genera dos archivos JSON a partir de dos rutas de carpetas diferentes existentes en mi computadora. Luego de armar los dos arboles JSON compara ambos y genera un nuevo JSON con las diferencias existentes entre ambos archivos anteriores.

    Lo que me pidieron, y no estoy pudiendo hacer es que cuando se compile el programa en la terminal "java -jar ..., etc" se le pasen las dos rutas, en el ClassPath, a los directorios desde donde se crearan los dos primeros arboles JSON ...

    Osea, en el programa estas dos rutas serían:

    1) File rootDir = new File("/home/incentivate/Desktop/resources") (la ruta de este File)

    2) File dirToCompare = new File("/home/incentivate/Desktop/th_watson_chatbot") (la ruta de este segundo File)

    No sé como hacer para generar esto! Nunca compile un programa por terminal y no sé cual es su funcionamiento detrás ...

    Login To add answer/comment
     

Share This Page