I have the MyClassin package X, package X also has Y and Z packages:
X - MyClass
X-Y - Some Files
X-Z - Some Files
How do I get a list of all the files from Maclase's packages Y and Z?
Java package mirror directory structure. You can use class. Specifically, see the listFiles () method.
Edit
You can dynamically find your execution location. Here is a project that I have worked recently; I wanted to be able to find the directory that I'm running from Jar (if I'm running a jar), or through the JAR, the Ki directory is running from class files. In my case, my Jar and & lt; Project Root & gt; Are in / classes .
final URL location; Last string class location = JavaPlanner.class.getName (). Replace ('.', '/') + ".class"; Last Class Loader Loader = JavaPlanner.class.getClassLoader (); If (loader == empty) {try to throw new exception (new class notfound exception) ("class filled with bootstrap loader");} (clasnotfound exception CNFE) {new initialization exception (CNF);}} Else {location = loader .getResource (classLocation);} If (location.toString (). BeginWith ("file: /")) // line 14 {// .class file is running from string path; try {path = URLDecoder.decode (location .toString (). Substring (6), "UTF-8");} Catch (unsupported encoding exposition U) {new initialization extension (U);} // package Move the folder up in the root, add / bin / file package_ = new file (path) .getParentFile (); BinPath = package_.getParentFile (). GetParentFile (). GetParentFile (). GetParentFile () + File.Sepapter + " Bin "+ file. Separator;} Else // line 25 {// .jar file string is running jarURL = JavaPlanner.class.getResource (" / "+ JavaPlanner.class.getName ()) all (" \\ " . "," / ") +" Class ") toString (). JarURL = jarURL.substring (4) .replaceFirst ("/ [^ /] + \\ .Jar!. * $", "/"); Try {file dir = new file (new URL (jarURL) .toURI ()); JarURL = dir.getAbsolutePath (); } Catch (malmarmdureluxception mu) {New Initialization Extension (MU); } Grip (URIsXXEpension Use) {New Initialization Expansion (Usage); } BinPath = jarURL; } In line 14, I have found that I am running an application from a class file. The file path of In line 25, I've found that I'm running the application from jar. The block becomes the path to the folder with only the executable jar. Obviously, this code is not 100% optimized for your purpose (most notably, I'm calling The entire purpose of the above code was to be able to find the right resource files for my application. In the production version, only jar will be available to the user, but I did not want to reproduce the jar to test some code every time, and I did not want to copy my resource files, and < / Em> I did not want to pollute my bin / folder with class files (because everything in the bin / user had to be sent). string path initially JavaPlanner (my main method) is set to the file path I know that the package structure javaplayer < / Code>, so I get the appropriate code several times to find the project root, and then bin /. / P / getParentFile specific time for my package structure), but I think it should help.
Comments
Post a Comment