user1636077
user1636077

Reputation: 123

Getting the list of packages in a java project

I was trying to get the List of packages from a java src folder by parsing it(using Files). Irrespective of the package structure (some may be com.example.abc, some may be com.example.xxyz.pqr, some may be com.application etc.) i want to get the list of packages in the src folder. Here is the function i've written. I'm getting very strange outputs. Please help me.

public static void displayIt(File node) {

    File[] subNode = node.listFiles();

    if (subNode.length == 1) {

        for (File file : subNode) {
            if (file.isDirectory()) {
                packageName = packageName + file.getName();
                displayIt(file);
            }
        }
    }

    else {
        subFolders = new ArrayList<String>();
        for (File file : subNode) {
            // parent.add(file.getName());
            subFolders.add(file.getName());
            if (file.isDirectory()) {
                File[] subDir = file.listFiles();
                for (File tempFile : subDir) {

                    if (tempFile.isDirectory()) {

                        // temp=file.getName()+"."+tempFile.getName();
                        packageList
                                .add(file.getName() + tempFile.getName());

                        displayIt(tempFile);
                    }

                }

            }

            displayIt(file);
        }
    }

}

Upvotes: 2

Views: 3464

Answers (2)

Chew Zai
Chew Zai

Reputation: 39

List All Packages

   public class q {
        public static void main(String args[]) {
            Package[] pack = Package.getPackages();

          // print all packages, one by one
            for (int i = 0; i < pack.length; i++) {
                String a = pack[i].toString()  ;
                System.out.println(a.replaceAll("package ", ""));
            }
        }
    }

Result

java.util.jar
jdk.internal.perf
sun.nio.fs
sun.invoke.empty
sun.security.action
java.lang
jdk.internal.jrtfs
java.util.function
java.security
java.util.regex
jdk.internal.reflect
sun.util.calendar
jdk.internal.misc
jdk.internal.ref
java.util.zip
java.math
sun.security.util
java.time.chrono
sun.util.locale
java.util.concurrent.atomic
sun.nio.ch
jdk.internal.org.objectweb.asm
sun.reflect.annotation
jdk.internal.vm
java.util.concurrent.locks
java.lang.constant
java.nio
jdk.internal.loader
java.time.temporal
sun.net.util
java.nio.charset
jdk.jfr.internal
jdk.internal.math
sun.io
java.io
sun.net.www
java.nio.file.attribute
sun.nio.cs
java.nio.channels
java.nio.channels.spi
jdk.internal.util.jar
jdk.internal.access
java.util.stream
jdk.internal.util
sun.net.www.protocol.file
java.lang.ref
java.nio.file.spi
java.lang.reflect
sun.util
java.time.zone
sun.launcher
sun.net.www.protocol.jrt
java.lang.module
java.util
jdk.internal.jimage.decompressor
java.time
java.nio.charset.spi
java.security.cert
sun.net.www.protocol.jar
java.nio.file
sun.reflect.misc
java.lang.invoke
sun.invoke.util
java.lang.annotation
jdk.internal.jimage
jdk.internal.module
java.net
java.util.concurrent
javax.lang.model.util
javax.tools
javax.lang.model
javax.lang.model.type
jdk.nio.zipfs
javax.lang.model.element
com.sun.tools.javac.tree
com.sun.tools.javac.launcher
com.sun.tools.javac.main
com.sun.tools.javac.parser
com.sun.tools.javac.platform
com.sun.tools.javac.processing
com.sun.tools.javac.file
com.sun.tools.javac.api
com.sun.source.util
com.sun.source.tree
com.sun.tools.javac.jvm
com.sun.tools.doclint
com.sun.tools.javac.model
com.sun.tools.javac.comp
com.sun.tools.javac.code
com.sun.tools.javac.resources
com.sun.tools.javac.util
com.sun.source.doctree

Upvotes: 0

Rustam
Rustam

Reputation: 6515

you can do like this :

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Set<String> files=new HashSet<>();
        listOfPackage("src/",files);

        System.out.println(files);
    }

    public static void listOfPackage(String directoryName, Set<String> pack) {
        File directory = new File(directoryName);

        // get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList) {
            if (file.isFile()) {
                String path=file.getPath();
                String packName=path.substring(path.indexOf("src")+4, path.lastIndexOf('\\'));
                pack.add(packName.replace('\\', '.'));
            } else if (file.isDirectory()) {

                listOfPackage(file.getAbsolutePath(), pack);
            }
        }
    }
}

output:

[com.think.android, com.think.java, com.test.java]

Upvotes: 8

Related Questions