Do you have a tool that will load a resource(say an image) from a jar file that contains code that is being loaded by a custom class loader. The jar file is NOT on the class path.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
One other thing about the requirement. The class file doesn't know the name/path of the jar file it is in.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
> Here is latest version of loader: https://github.com/nikelin/Redshape-...cesLoader.java
A few observations:
Line 20; don't work with raw string paths. As soon as the user passes in a root directory, create a File object out of it or better yet ask the user to pass in a "real" directory in the form of a File object rather than a "string" path.
Line 36; this will fail on Windows platform since the path separator is not ":" but ";". That line can be better written as: System.getProperty("java.class.path").split(System.getProperty("path.separator"))
Line 41; similarly for "searchPath"; don't work with raw strings treating them as path.
Line 67; "/" is a path separator on *nix platforms while on windows it is "\". That line can be better written as: File someFile = new File(rootFile, fileNameString)
Line 87, loadData method; you don't close the input stream! Plus, if your aim is to read textual data line by line, use a Scanner instead. Scanner in = new Scanner(myFile); . Strings in Java are immutable and hence the concat() on line 95 will end up creating new String objects each time a concat is done. Better use a StringBuilder for incrementally creating strings. Don't manually append "\n" since the EOL (end of line) character is different for *nix, Win and Mac. You need to use the system property, line.separator.
Line 101; why "\0"? If you want to ignore non-printable characters, why not "" (a blank string)?
Throughout the code, you also need to verify that the user provided path is a directory and if not act accordingly though this is more of a "validation" part.
I guess that's it from a casual glance at your source code! :-)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
One other thing about the requirement. The class file doesn't know the name/path of the jar file it is in.
Here's a way to do it - I found this ages ago, so I can't remember where I saw it, but it works for me...
File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073