1. Introduction
In this tutorial, we are going to present a way to read files from the JAR archive in Java. This article will cover the method available in Java 8.
2. Using java.util.jar.JarFile
to read JAR file
The best way to read the contents of a JAR archive is to use JarFile
utility class available in Java 8. This class can read any Java Archive File
that can be opened with RandomAccessFile
.
The JarFile
constructor accepts a single parameter with the Jar file.
The following example code shows how to print all .class
files from Jar located in /tmp/commons-io-2.4.jar
.
Copy
package com.frontbackend.java.util;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
public class ReadClassesFromJarFile {
public static void main(String[] args) throws IOException {
try (JarFile jar = new JarFile(new File("/tmp/commons-io-2.4.jar"))) {
EnumerationUtils.enumerationAsStream(jar.entries())
.filter(jarEntry -> jarEntry.getName()
.endsWith(".class"))
.forEach(jarEntry -> System.out.println(jarEntry.getName()));
}
}
}
We used java.util.jar.JarFile.entries()
method to get all resources from the JAR file. This method returns the Enumeration
object that provides not so modern way to iterate over collections, so we created utility class EnumerationUtils
that will convert Enumeration
to the Stream
object.
Copy
package com.frontbackend.java.util;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public final class EnumerationUtils {
private EnumerationUtils() {
}
public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<T>() {
public T next() {
return e.nextElement();
}
public boolean hasNext() {
return e.hasMoreElements();
}
}, Spliterator.ORDERED), false);
}
}
For version 2.4
of commons-io
library this snippet will produce the following output:
Copy
org/apache/commons/io/ByteOrderMark.class
org/apache/commons/io/Charsets.class
org/apache/commons/io/comparator/AbstractFileComparator.class
org/apache/commons/io/comparator/CompositeFileComparator.class
org/apache/commons/io/comparator/DefaultFileComparator.class
org/apache/commons/io/comparator/DirectoryFileComparator.class
org/apache/commons/io/comparator/ExtensionFileComparator.class
org/apache/commons/io/comparator/LastModifiedFileComparator.class
org/apache/commons/io/comparator/NameFileComparator.class
org/apache/commons/io/comparator/PathFileComparator.class
org/apache/commons/io/comparator/ReverseComparator.class
org/apache/commons/io/comparator/SizeFileComparator.class
org/apache/commons/io/CopyUtils.class
org/apache/commons/io/DirectoryWalker$CancelException.class
org/apache/commons/io/DirectoryWalker.class
org/apache/commons/io/EndianUtils.class
org/apache/commons/io/FileCleaner.class
org/apache/commons/io/FileCleaningTracker$Reaper.class
org/apache/commons/io/FileCleaningTracker$Tracker.class
org/apache/commons/io/FileCleaningTracker.class
org/apache/commons/io/FileDeleteStrategy$ForceFileDeleteStrategy.class
org/apache/commons/io/FileDeleteStrategy.class
org/apache/commons/io/FileExistsException.class
org/apache/commons/io/filefilter/AbstractFileFilter.class
org/apache/commons/io/filefilter/AgeFileFilter.class
org/apache/commons/io/filefilter/AndFileFilter.class
org/apache/commons/io/filefilter/CanReadFileFilter.class
org/apache/commons/io/filefilter/CanWriteFileFilter.class
org/apache/commons/io/filefilter/ConditionalFileFilter.class
org/apache/commons/io/filefilter/DelegateFileFilter.class
org/apache/commons/io/filefilter/DirectoryFileFilter.class
org/apache/commons/io/filefilter/EmptyFileFilter.class
org/apache/commons/io/filefilter/FalseFileFilter.class
org/apache/commons/io/filefilter/FileFileFilter.class
org/apache/commons/io/filefilter/FileFilterUtils.class
org/apache/commons/io/filefilter/HiddenFileFilter.class
org/apache/commons/io/filefilter/IOFileFilter.class
org/apache/commons/io/filefilter/MagicNumberFileFilter.class
org/apache/commons/io/filefilter/NameFileFilter.class
org/apache/commons/io/filefilter/NotFileFilter.class
org/apache/commons/io/filefilter/OrFileFilter.class
org/apache/commons/io/filefilter/PrefixFileFilter.class
org/apache/commons/io/filefilter/RegexFileFilter.class
org/apache/commons/io/filefilter/SizeFileFilter.class
org/apache/commons/io/filefilter/SuffixFileFilter.class
org/apache/commons/io/filefilter/TrueFileFilter.class
org/apache/commons/io/filefilter/WildcardFileFilter.class
org/apache/commons/io/filefilter/WildcardFilter.class
org/apache/commons/io/FilenameUtils.class
org/apache/commons/io/FileSystemUtils.class
org/apache/commons/io/FileUtils.class
org/apache/commons/io/HexDump.class
org/apache/commons/io/input/AutoCloseInputStream.class
org/apache/commons/io/input/BOMInputStream$1.class
org/apache/commons/io/input/BOMInputStream.class
org/apache/commons/io/input/BoundedInputStream.class
org/apache/commons/io/input/BrokenInputStream.class
org/apache/commons/io/input/CharSequenceInputStream.class
org/apache/commons/io/input/CharSequenceReader.class
org/apache/commons/io/input/ClassLoaderObjectInputStream.class
org/apache/commons/io/input/ClosedInputStream.class
org/apache/commons/io/input/CloseShieldInputStream.class
org/apache/commons/io/input/CountingInputStream.class
org/apache/commons/io/input/DemuxInputStream.class
org/apache/commons/io/input/NullInputStream.class
org/apache/commons/io/input/NullReader.class
org/apache/commons/io/input/ProxyInputStream.class
org/apache/commons/io/input/ProxyReader.class
org/apache/commons/io/input/ReaderInputStream.class
org/apache/commons/io/input/ReversedLinesFileReader$1.class
org/apache/commons/io/input/ReversedLinesFileReader$FilePart.class
org/apache/commons/io/input/ReversedLinesFileReader.class
org/apache/commons/io/input/SwappedDataInputStream.class
org/apache/commons/io/input/TaggedInputStream.class
org/apache/commons/io/input/Tailer.class
org/apache/commons/io/input/TailerListener.class
org/apache/commons/io/input/TailerListenerAdapter.class
org/apache/commons/io/input/TeeInputStream.class
org/apache/commons/io/input/XmlStreamReader.class
org/apache/commons/io/input/XmlStreamReaderException.class
org/apache/commons/io/IOCase.class
org/apache/commons/io/IOExceptionWithCause.class
org/apache/commons/io/IOUtils.class
org/apache/commons/io/LineIterator.class
org/apache/commons/io/monitor/FileAlterationListener.class
org/apache/commons/io/monitor/FileAlterationListenerAdaptor.class
org/apache/commons/io/monitor/FileAlterationMonitor.class
org/apache/commons/io/monitor/FileAlterationObserver.class
org/apache/commons/io/monitor/FileEntry.class
org/apache/commons/io/output/BrokenOutputStream.class
org/apache/commons/io/output/ByteArrayOutputStream.class
org/apache/commons/io/output/ClosedOutputStream.class
org/apache/commons/io/output/CloseShieldOutputStream.class
org/apache/commons/io/output/CountingOutputStream.class
org/apache/commons/io/output/DeferredFileOutputStream.class
org/apache/commons/io/output/DemuxOutputStream.class
org/apache/commons/io/output/FileWriterWithEncoding.class
org/apache/commons/io/output/LockableFileWriter.class
org/apache/commons/io/output/NullOutputStream.class
org/apache/commons/io/output/NullWriter.class
org/apache/commons/io/output/ProxyOutputStream.class
org/apache/commons/io/output/ProxyWriter.class
org/apache/commons/io/output/StringBuilderWriter.class
org/apache/commons/io/output/TaggedOutputStream.class
org/apache/commons/io/output/TeeOutputStream.class
org/apache/commons/io/output/ThresholdingOutputStream.class
org/apache/commons/io/output/WriterOutputStream.class
org/apache/commons/io/output/XmlStreamWriter.class
org/apache/commons/io/TaggedIOException.class
org/apache/commons/io/ThreadMonitor.class
If you want to include commons-io
dependency for your maven project, check the following link - you can find here all available versions.
3. Conclusion
In this short tutorial, we presented a way to get all files from Java Archive file using utility class available in Java 8. Note that this example code will work for not-signed files.
As always the example code use in this article is available in our GitHub repository .
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}