SWT file drop: identify folders - java

Code
I'm trying to add drop functionality to my program so users can drag and drop files from Windows. Therefore I implemented the SWT DropTargetListener interface and added a SWT DropTarget to my SWT control.
public class MyFileDrop implements DropTargetListener {
public MyFileDrop(final Control control) {
addDropTarget(control);
}
private void addDropTarget(final Control control) {
final DropTarget dropTarget = new DropTarget(control, DND.DROP_COPY | DND.DROP_DEFAULT);
dropTarget.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dropTarget.addDropListener(this);
}
#Override
public void drop(final DropTargetEvent event) {
String[] fileNames = (String[]) event.data;
for (final String fileName : fileNames) {
System.out.println(fileName);
}
}
// other DropTargetListener methods [...]
}
If I drag and drop a folder from Windows Explorer to my control, the folder name gets printed out. But all files inside that dropped folder aren't listed.
Example
folder1
- subfile1.png
- subfile2.exe
file1.png
file2.exe
If I select folder1, file1 and file2 and drag and drop it to my program, this is the output:
path/to/folder1
path/to/file1.png
path/to/file2.exe
Questions
Why aren't the files inside the folder dropped to the program? How can I archive that these files are also dropped?
How can I set the files apart from the folder?
Regards winklerrr

Code
So this is my solution
#Override
public void drop(DropTargetEvent event) {
String[] fileNames = (String[]) event.data;
for (String fileName : fileNames) {
final File file = new File(fileName);
if (file.isDirectory()) {
Collection<File> subFiles = FileUtils.listFiles(file, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File subFile : subFiles) {
System.out.println(subFile.getAbsolutePath());
}
} else {
System.out.println(file.getAbsolutePath());
}
}
}
I used the FileUtils from the commons-io package.
Example
folder1
- subfile1.png
- subfile2.exe
file1.png
file2.exe
With the new code, this is now the output:
path/to/folder1/subfile1.png
path/to/folder1/subfile2.exe
path/to/file1.png
path/to/file2.exe
Answers
How can I archive that these files are also dropped?
FileUtils.listFiles(File, IOFileFilter, IOFileFilter)
How can I set the files apart from the folder?
File.isDirectory()

Related

How to use Java 8 `Files.find` method?

I am trying to write an app to use Files.find method in it.
Below program works perfectly :
package ehsan;
/* I have removed imports for code brevity */
public class Main {
public static void main(String[] args) throws IOException {
Path p = Paths.get("/home/ehsan");
final int maxDepth = 10;
Stream<Path> matches = Files.find(p,maxDepth,(path, basicFileAttributes) -> String.valueOf(path).endsWith(".txt"));
matches.map(path -> path.getFileName()).forEach(System.out::println);
}
}
This works fine and gives me a list of files ending with .txt ( aka text files ) :
hello.txt
...
But below program does not show anything :
package ehsan;
public class Main {
public static void main(String[] args) throws IOException {
Path p = Paths.get("/home/ehsan");
final int maxDepth = 10;
Stream<Path> matches = Files.find(p,maxDepth,(path, basicFileAttributes) -> path.getFileName().equals("workspace"));
matches.map(path -> path.getFileName()).forEach(System.out::println);
}
}
But it does not show anything :(
Here is my home folder hiearchy (ls result) :
blog Projects
Desktop Public
Documents Templates
Downloads The.Purge.Election.Year.2016.HC.1080p.HDrip.ShAaNiG.mkv
IdeaProjects The.Purge.Election.Year.2016.HC.1080p.HDrip.ShAaNiG.mkv.aria2
Music Videos
Pictures workspace
So whats going wrong with path.getFileName().equals("workspace")?
Path.getFilename() does not return a String, but a Path object, do this:
getFilename().toString().equals("workspace")
Use the following and look at the console. Maybe none of your files contains workspace in it
Files.find(p,maxDepth,(path, basicFileAttributes) -> {
if (String.valueOf(path).equals("workspace")) {
System.out.println("FOUND : " + path);
return true;
}
System.out.println("\tNOT VALID : " + path);
return false;
});

JavaFX Drag and Drop: accept only some file extensions

What i want to do:
when the user tries to drag and drop something into the window,he can do it only if the drag has file with extensions (.mp3,.ogg,.wav).If the files have not this extension the drop can't be done.I don't want links,etc...to be dropped.
For example accept only html is so easy as:
controller.setOnDragOver((over) -> {
Dragboard board = over.getDragboard();
if (board.hasHtml())
over.acceptTransferModes(TransferMode.LINK);
});
How can i add a filter for that?
You can use the getFiles method of DragBoard returned by getDragboard method of DragEvent in the event handler set in setOnDragOver of your Node or Scene to get the list of files currently dragged.
You can check the extensions in this list either using for example getExtension of Apache Commons IO or by implementing your own function to get the extension of a file. If the extension(s) of the file(s) of the dragboard does not match with the predefined extensions you can simply consume the DragEvent.
Example
In this example I have created a Stage with a VBox inside which accepts only files with extension of "jpg" and "png" to be dropped on it. If the drop was succesful it prints the absolute file path of the files.
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
VBox root = new VBox();
Scene scene = new Scene(root,400,400);
// Extensions that are valid to be drag-n-dropped
List<String> validExtensions = Arrays.asList("jpg", "png");
root.setOnDragOver(event -> {
// On drag over if the DragBoard has files
if (event.getGestureSource() != root && event.getDragboard().hasFiles()) {
// All files on the dragboard must have an accepted extension
if (!validExtensions.containsAll(
event.getDragboard().getFiles().stream()
.map(file -> getExtension(file.getName()))
.collect(Collectors.toList()))) {
event.consume();
return;
}
// Allow for both copying and moving
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
});
root.setOnDragDropped(event -> {
boolean success = false;
if (event.getGestureSource() != root && event.getDragboard().hasFiles()) {
// Print files
event.getDragboard().getFiles().forEach(file -> System.out.println(file.getAbsolutePath()));
success = true;
}
event.setDropCompleted(success);
event.consume();
});
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
// Method to to get extension of a file
private String getExtension(String fileName){
String extension = "";
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) //if the name is not empty
return fileName.substring(i + 1).toLowerCase();
return extension;
}
public static void main(String[] args) {
launch(args);
}
}

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);
}
}
}
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]
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

Multipart Upload Servlet 3.0 - temporary files not deleting

I have an upload servlet that is working great but leaves it's temporary files lying around. I am trying to use the part.delete() to clean them up as I go, but they are not deleting.
The docs say the container will delete them when it does GC. But even if I wait an hour and eventually shut the server down, they are still there...
What's the trick? It's Eclipse Kepler with Tomcat 7.0.47 on Windows for the moment. But production will be Linux.
Thanks
Code condensed substantially:
#MultipartConfig(location = "C:/tmp",
fileSizeThreshold=1024*1024*10, // 10MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
#WebServlet("/upload.do")
...
for (Part part : request.getParts()) {
String filename = getFilename(part);
if(!(filename==null)){
part.write("/elsewhere/"+filename);
part.delete();
} else {
out.println("skip field...");
}
}
Hi you can create Servlet Listner like this
#WebListener
public class ContextListner implements ServletRequestListener, ServletContextListener {
public ContextListner() {
}
public void requestDestroyed(ServletRequestEvent sre) {
deleteDir(sre.getServletContext().getRealPath("") + File.separator + UploadConstants.TEMP_DIR);
}
public void contextInitialized(ServletContextEvent sce) {
}
public void contextDestroyed(ServletContextEvent sce) {
deleteDir(sce.getServletContext().getRealPath("") + File.separator + UploadConstants.TEMP_DIR);
}
public void requestInitialized(ServletRequestEvent sre) {
}
private void deleteDir(final String dirPath) {
if (null == dirPath)
return;
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
if (null != files) {
for (File file : files) {
file.delete();
}
}
}
}
}
And mark your servlet with annotation as mentioned below.
#WebListener(value = "ContextListner")
This will delete temp file under temp directory or your specified directory.
public void requestDestroyed(ServletRequestEvent sre) {
deleteDir(sre.getServletContext().getRealPath("") + File.separator + UploadConstants.TEMP_DIR);
}
This method get call after response send back to client.

How to list the files inside a JAR file?

I have this code which reads all the files from a directory.
File textFolder = new File("text_directory");
File [] texFiles = textFolder.listFiles( new FileFilter() {
public boolean accept( File file ) {
return file.getName().endsWith(".txt");
}
});
It works great. It fills the array with all the files that end with ".txt" from directory "text_directory".
How can I read the contents of a directory in a similar fashion within a JAR file?
So what I really want to do is, to list all the images inside my JAR file, so I can load them with:
ImageIO.read(this.getClass().getResource("CompanyLogo.png"));
(That one works because the "CompanyLogo" is "hardcoded" but the number of images inside the JAR file could be from 10 to 200 variable length.)
EDIT
So I guess my main problem would be: How to know the name of the JAR file where my main class lives?
Granted I could read it using java.util.Zip.
My Structure is like this:
They are like:
my.jar!/Main.class
my.jar!/Aux.class
my.jar!/Other.class
my.jar!/images/image01.png
my.jar!/images/image02a.png
my.jar!/images/imwge034.png
my.jar!/images/imagAe01q.png
my.jar!/META-INF/manifest
Right now I'm able to load for instance "images/image01.png" using:
ImageIO.read(this.getClass().getResource("images/image01.png));
But only because I know the file name, for the rest I have to load them dynamically.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.startsWith("path/to/your/dir/")) {
/* Do something with this entry. */
...
}
}
}
else {
/* Fail... */
}
Note that in Java 7, you can create a FileSystem from the JAR (zip) file, and then use NIO's directory walking and filtering mechanisms to search through it. This would make it easier to write code that handles JARs and "exploded" directories.
Code that works for both IDE's and .jar files:
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
public class ResourceWalker {
public static void main(String[] args) throws URISyntaxException, IOException {
URI uri = ResourceWalker.class.getResource("/resources").toURI();
Path myPath;
if (uri.getScheme().equals("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
myPath = fileSystem.getPath("/resources");
} else {
myPath = Paths.get(uri);
}
Stream<Path> walk = Files.walk(myPath, 1);
for (Iterator<Path> it = walk.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}
erickson's answer worked perfectly:
Here's the working code.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
List<String> list = new ArrayList<String>();
if( src != null ) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream( jar.openStream());
ZipEntry ze = null;
while( ( ze = zip.getNextEntry() ) != null ) {
String entryName = ze.getName();
if( entryName.startsWith("images") && entryName.endsWith(".png") ) {
list.add( entryName );
}
}
}
webimages = list.toArray( new String[ list.size() ] );
And I have just modify my load method from this:
File[] webimages = ...
BufferedImage image = ImageIO.read(this.getClass().getResource(webimages[nextIndex].getName() ));
To this:
String [] webimages = ...
BufferedImage image = ImageIO.read(this.getClass().getResource(webimages[nextIndex]));
I would like to expand on acheron55's answer, since it is a very non-safe solution, for several reasons:
It doesn't close the FileSystem object.
It doesn't check if the FileSystem object already exists.
It isn't thread-safe.
This is somewhat a safer solution:
private static ConcurrentMap<String, Object> locks = new ConcurrentHashMap<>();
public void walk(String path) throws Exception {
URI uri = getClass().getResource(path).toURI();
if ("jar".equals(uri.getScheme()) {
safeWalkJar(path, uri);
} else {
Files.walk(Paths.get(path));
}
}
private void safeWalkJar(String path, URI uri) throws Exception {
synchronized (getLock(uri)) {
// this'll close the FileSystem object at the end
try (FileSystem fs = getFileSystem(uri)) {
Files.walk(fs.getPath(path));
}
}
}
private Object getLock(URI uri) {
String fileName = parseFileName(uri);
locks.computeIfAbsent(fileName, s -> new Object());
return locks.get(fileName);
}
private String parseFileName(URI uri) {
String schemeSpecificPart = uri.getSchemeSpecificPart();
return schemeSpecificPart.substring(0, schemeSpecificPart.indexOf("!"));
}
private FileSystem getFileSystem(URI uri) throws IOException {
try {
return FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap());
}
}
There's no real need to synchronize over the file name; one could simply synchronize on the same object every time (or make the method synchronized), it's purely an optimization.
I would say that this is still a problematic solution, since there might be other parts in the code that use the FileSystem interface over the same files, and it could interfere with them (even in a single threaded application).
Also, it doesn't check for nulls (for instance, on getClass().getResource().
This particular Java NIO interface is kind of horrible, since it introduces a global/singleton non thread-safe resource, and its documentation is extremely vague (a lot of unknowns due to provider specific implementations). Results may vary for other FileSystem providers (not JAR). Maybe there's a good reason for it being that way; I don't know, I haven't researched the implementations.
So I guess my main problem would be, how to know the name of the jar where my main class lives.
Assuming that your project is packed in a Jar (not necessarily true!), you can use ClassLoader.getResource() or findResource() with the class name (followed by .class) to get the jar that contains a given class. You'll have to parse the jar name from the URL that gets returned (not that tough), which I will leave as an exercise for the reader :-)
Be sure to test for the case where the class is not part of a jar.
I've ported acheron55's answer to Java 7 and closed the FileSystem object. This code works in IDE's, in jar files and in a jar inside a war on Tomcat 7; but note that it does not work in a jar inside a war on JBoss 7 (it gives FileSystemNotFoundException: Provider "vfs" not installed, see also this post). Furthermore, like the original code, it is not thread safe, as suggested by errr. For these reasons I have abandoned this solution; however, if you can accept these issues, here is my ready-made code:
import java.io.IOException;
import java.net.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
public class ResourceWalker {
public static void main(String[] args) throws URISyntaxException, IOException {
URI uri = ResourceWalker.class.getResource("/resources").toURI();
System.out.println("Starting from: " + uri);
try (FileSystem fileSystem = (uri.getScheme().equals("jar") ? FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap()) : null)) {
Path myPath = Paths.get(uri);
Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
});
}
}
}
Here is an example of using Reflections library to recursively scan classpath by regex name pattern augmented with a couple of Guava perks to to fetch resources contents:
Reflections reflections = new Reflections("com.example.package", new ResourcesScanner());
Set<String> paths = reflections.getResources(Pattern.compile(".*\\.template$"));
Map<String, String> templates = new LinkedHashMap<>();
for (String path : paths) {
log.info("Found " + path);
String templateName = Files.getNameWithoutExtension(path);
URL resource = getClass().getClassLoader().getResource(path);
String text = Resources.toString(resource, StandardCharsets.UTF_8);
templates.put(templateName, text);
}
This works with both jars and exploded classes.
Here's a method I wrote for a "run all JUnits under a package". You should be able to adapt it to your needs.
private static void findClassesInJar(List<String> classFiles, String path) throws IOException {
final String[] parts = path.split("\\Q.jar\\\\E");
if (parts.length == 2) {
String jarFilename = parts[0] + ".jar";
String relativePath = parts[1].replace(File.separatorChar, '/');
JarFile jarFile = new JarFile(jarFilename);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String entryName = entry.getName();
if (entryName.startsWith(relativePath)) {
classFiles.add(entryName.replace('/', File.separatorChar));
}
}
}
}
Edit:
Ah, in that case, you might want this snippet as well (same use case :) )
private static File findClassesDir(Class<?> clazz) {
try {
String path = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
final String codeSourcePath = URLDecoder.decode(path, "UTF-8");
final String thisClassPath = new File(codeSourcePath, clazz.getPackage().getName().repalce('.', File.separatorChar));
} catch (UnsupportedEncodingException e) {
throw new AssertionError("impossible", e);
}
}
Just to mention that if you are already using Spring, you can take advantage of the PathMatchingResourcePatternResolver.
For instance to get all the PNG files from a images folder in resources
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("images/*.png");
for (Resource r: resources){
logger.info(r.getFilename());
// From your example
// ImageIO.read(cl.getResource("images/" + r.getFilename()));
}
A jar file is just a zip file with a structured manifest. You can open the jar file with the usual java zip tools and scan the file contents that way, inflate streams, etc. Then use that in a getResourceAsStream call, and it should be all hunky dory.
EDIT / after clarification
It took me a minute to remember all the bits and pieces and I'm sure there are cleaner ways to do it, but I wanted to see that I wasn't crazy. In my project image.jpg is a file in some part of the main jar file. I get the class loader of the main class (SomeClass is the entry point) and use it to discover the image.jpg resource. Then some stream magic to get it into this ImageInputStream thing and everything is fine.
InputStream inputStream = SomeClass.class.getClassLoader().getResourceAsStream("image.jpg");
JPEGImageReaderSpi imageReaderSpi = new JPEGImageReaderSpi();
ImageReader ir = imageReaderSpi.createReaderInstance();
ImageInputStream iis = new MemoryCacheImageInputStream(inputStream);
ir.setInput(iis);
....
ir.read(0); //will hand us a buffered image
Given an actual JAR file, you can list the contents using JarFile.entries(). You will need to know the location of the JAR file though - you can't just ask the classloader to list everything it could get at.
You should be able to work out the location of the JAR file based on the URL returned from ThisClassName.class.getResource("ThisClassName.class"), but it may be a tiny bit fiddly.
Some time ago I made a function that gets classess from inside JAR:
public static Class[] getClasses(String packageName)
throws ClassNotFoundException{
ArrayList<Class> classes = new ArrayList<Class> ();
packageName = packageName.replaceAll("\\." , "/");
File f = new File(jarName);
if(f.exists()){
try{
JarInputStream jarFile = new JarInputStream(
new FileInputStream (jarName));
JarEntry jarEntry;
while(true) {
jarEntry=jarFile.getNextJarEntry ();
if(jarEntry == null){
break;
}
if((jarEntry.getName ().startsWith (packageName)) &&
(jarEntry.getName ().endsWith (".class")) ) {
classes.add(Class.forName(jarEntry.getName().
replaceAll("/", "\\.").
substring(0, jarEntry.getName().length() - 6)));
}
}
}
catch( Exception e){
e.printStackTrace ();
}
Class[] classesA = new Class[classes.size()];
classes.toArray(classesA);
return classesA;
}else
return null;
}
public static ArrayList<String> listItems(String path) throws Exception{
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
byte[] b = new byte[in.available()];
in.read(b);
String data = new String(b);
String[] s = data.split("\n");
List<String> a = Arrays.asList(s);
ArrayList<String> m = new ArrayList<>(a);
return m;
}
There are two very useful utilities both called JarScan:
www.inetfeedback.com/jarscan
jarscan.dev.java.net
See also this question: JarScan, scan all JAR files in all subfolders for specific class
The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)
How to know the name of the JAR file where my main class lives?
URI mainClasspathElementURI;
try (ScanResult scanResult = new ClassGraph().whitelistPackages("x.y.z")
.enableClassInfo().scan()) {
mainClasspathElementURI =
scanResult.getClassInfo("x.y.z.MainClass").getClasspathElementURI();
}
How can I read the contents of a directory in a similar fashion within a JAR file?
List<String> classpathElementResourcePaths;
try (ScanResult scanResult = new ClassGraph().overrideClasspath(mainClasspathElementURI)
.scan()) {
classpathElementResourcePaths = scanResult.getAllResources().getPaths();
}
There are lots of other ways to deal with resources too.
One more for the road that's a bit more flexible for matching specific filenames because it uses wildcard globbing. In a functional style this could resemble:
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import static java.nio.file.FileSystems.getDefault;
import static java.nio.file.FileSystems.newFileSystem;
import static java.util.Collections.emptyMap;
/**
* Responsible for finding file resources.
*/
public class ResourceWalker {
/**
* Globbing pattern to match font names.
*/
public static final String GLOB_FONTS = "**.{ttf,otf}";
/**
* #param directory The root directory to scan for files matching the glob.
* #param c The consumer function to call for each matching path
* found.
* #throws URISyntaxException Could not convert the resource to a URI.
* #throws IOException Could not walk the tree.
*/
public static void walk(
final String directory, final String glob, final Consumer<Path> c )
throws URISyntaxException, IOException {
final var resource = ResourceWalker.class.getResource( directory );
final var matcher = getDefault().getPathMatcher( "glob:" + glob );
if( resource != null ) {
final var uri = resource.toURI();
final Path path;
FileSystem fs = null;
if( "jar".equals( uri.getScheme() ) ) {
fs = newFileSystem( uri, emptyMap() );
path = fs.getPath( directory );
}
else {
path = Paths.get( uri );
}
try( final var walk = Files.walk( path, 10 ) ) {
for( final var it = walk.iterator(); it.hasNext(); ) {
final Path p = it.next();
if( matcher.matches( p ) ) {
c.accept( p );
}
}
} finally {
if( fs != null ) { fs.close(); }
}
}
}
}
Consider parameterizing the file extensions, left an exercise for the reader.
Be careful with Files.walk. According to the documentation:
This method must be used within a try-with-resources statement or similar control structure to ensure that the stream's open directories are closed promptly after the stream's operations have completed.
Likewise, newFileSystem must be closed, but not before the walker has had a chance to visit the file system paths.
Just a different way of listing/reading files from a jar URL and it does it recursively for nested jars
https://gist.github.com/trung/2cd90faab7f75b3bcbaa
URL urlResource = Thead.currentThread().getContextClassLoader().getResource("foo");
JarReader.read(urlResource, new InputStreamCallback() {
#Override
public void onFile(String name, InputStream is) throws IOException {
// got file name and content stream
}
});

Categories