I know already how to get all the packages available, however I’m trying to only get the packages that I made in my own java project.
for example I don’t want java.util to show up in the list of packages.
I’m using this code to gather the classes:
List<String> result = new LinkedList<String>();
Package[] packages = Package.getPackages();
for(Package pack : packages)
{
result.add(pack.getName());
}
And it works very good, now I just want to get rid of all the system packages.
Is there any way to filter the packages so that only the ones I’ve defined remain?
PS, I’m not worried about efficiency, the purpose of this can be as slow as it needs to be, it will only run once at the beginning of my program.
I have looked at several related questions, but none of them are what I am looking for.
If there is a way to tell if a package name is a system package, that will do the job perfectly.
EDIT!
I am making this as part of an API, and because of that I can’t really just compare the package name against a set token to see if it’s a package name I’m using, I don’t want there to be any editing required, just place the class file in my project and use it, regardless of what package names I choose.
One method I would try, howver it doesn’t seem to work, and gives me random errors, is scanning the jar file to check if there is a directory equivelant to the package name. Whenever I scan the jar file however I get the error “Can not access jar file, is it being used by another process?”.
Scanning the jar file would definitly work to see if the class existed in my project (jar file) but another limitation is that I have to compile to test it, can’t just test it in Eclipse, I edited the code 10 times trying to get the jar file search to work, and each time get access errors because javaw.exe is using the jar file it’s not allowing me to read the files inside of it.
Edit2!
Someone suggested that I make a list of all the known system classes and compare it to that list, which would work if different JDKs didn’t have some different classes, so I was wondering a new question sort of, is there any way to get all the jar files that are part of the current JRE System Library? Then I could read through those and add all their packages to the filters.
I don’t know about any method from this… just for classes.
But you could make a own list by letting this run, maybe saving to a file for every operating system in an own program.
Then load this list and iterate through every package in your other program where you want to filter the packages…
So you could kind of check it… sure there are different versions of the java-libraries but i dont think you want to use it that serious hm?