Package io.github.classgraph.utils
Class JarUtils
- java.lang.Object
-
- io.github.classgraph.utils.JarUtils
-
public class JarUtils extends Object
Jarfile utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static String[]
SYSTEM_PACKAGE_PATH_PREFIXES
Prefixes of system (JRE) packages, turned into path form (with slashes instead of dots).static String[]
SYSTEM_PACKAGE_PREFIXES
Prefixes of system (JRE) packages.
-
Constructor Summary
Constructors Constructor Description JarUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
countBytesBeforePKMarker(File zipfile)
Count the number of bytes before the characters "PK" in a zipfile.static ClassLoader
createURLClassLoaderFromPathString(String classpathStr)
Create a custom URLClassLoader from a classpath path string.static List<String>
getJreJarPaths()
static Set<String>
getJreLibOrExtJars()
static boolean
isInSystemPackageOrModule(String packageOrModuleName)
Return true if the given class name, package name or module name has a system package or module prefixstatic boolean
isJREJar(String filePath, ScanSpec scanSpec, LogNode log)
static String
leafName(String path)
Returns the leafname of a path, after first stripping off everything after the first '!', if present.static void
logJavaInfo(LogNode log)
Log the Java version and the JRE paths that were found.static String
pathElementsToPathStr(Iterable<?> pathElts)
Get a set of path elements as a string, from an array of objects (e.g.static String
pathElementsToPathStr(Object... pathElts)
Get a set of path elements as a string, from an array of objects (e.g.static String[]
smartPathSplit(String pathStr)
Split a path on File.pathSeparator (':' on Linux, ';' on Windows), but also allow for the use of URLs with protocol specifiers, e.g.static void
stripSFXHeader(File srcZipfile, long sfxHeaderBytes, File destZipfile)
Strip the self-extracting archive header from the beginning of a zipfile.
-
-
-
Field Detail
-
SYSTEM_PACKAGE_PREFIXES
public static final String[] SYSTEM_PACKAGE_PREFIXES
Prefixes of system (JRE) packages.
-
SYSTEM_PACKAGE_PATH_PREFIXES
public static final String[] SYSTEM_PACKAGE_PATH_PREFIXES
Prefixes of system (JRE) packages, turned into path form (with slashes instead of dots).
-
-
Method Detail
-
smartPathSplit
public static String[] smartPathSplit(String pathStr)
Split a path on File.pathSeparator (':' on Linux, ';' on Windows), but also allow for the use of URLs with protocol specifiers, e.g. "http://domain/jar1.jar:http://domain/jar2.jar". This is really not even handled by the JRE, in all likelihood, but it's better to be robust.- Parameters:
pathStr
- The path to split.- Returns:
- The path element substrings.
-
createURLClassLoaderFromPathString
public static ClassLoader createURLClassLoaderFromPathString(String classpathStr)
Create a custom URLClassLoader from a classpath path string.- Parameters:
classpathStr
- The classpath string.- Returns:
- A custom
URLClassLoader
that can load from the path string.
-
pathElementsToPathStr
public static String pathElementsToPathStr(Object... pathElts)
Get a set of path elements as a string, from an array of objects (e.g. of String, File or URL type, whose toString() method will be called to get the path component), and return the path as a single string delineated with the standard path separator character.- Parameters:
pathElts
- The path elements.- Returns:
- The delimited path formed out of the path elements.
-
pathElementsToPathStr
public static String pathElementsToPathStr(Iterable<?> pathElts)
Get a set of path elements as a string, from an array of objects (e.g. of String, File or URL type, whose toString() method will be called to get the path component), and return the path as a single string delineated with the standard path separator character.- Parameters:
pathElts
- The path elements.- Returns:
- The delimited path formed out of the path elements, after calling each of their toString() methods.
-
leafName
public static String leafName(String path)
Returns the leafname of a path, after first stripping off everything after the first '!', if present.- Parameters:
path
- A file path.- Returns:
- The leafname of the path.
-
getJreJarPaths
public static List<String> getJreJarPaths()
- Returns:
- The paths of jars in all JRE/JDK system directories, with any rt.jar listed first.
-
getJreLibOrExtJars
public static Set<String> getJreLibOrExtJars()
- Returns:
- The paths for any JRE/JDK "lib/" or "ext/" jars.
-
isJREJar
public static boolean isJREJar(String filePath, ScanSpec scanSpec, LogNode log)
- Parameters:
filePath
- A file path.scanSpec
- TheScanSpec
.log
- The log.- Returns:
- Whether a given jarfile is in a JRE system directory (jre, jre/lib, jre/lib/ext, etc.).
-
isInSystemPackageOrModule
public static boolean isInSystemPackageOrModule(String packageOrModuleName)
Return true if the given class name, package name or module name has a system package or module prefix- Parameters:
packageOrModuleName
- The class, package or module name.- Returns:
- Whether this is a system class, package or module.
-
countBytesBeforePKMarker
public static long countBytesBeforePKMarker(File zipfile) throws IOException
Count the number of bytes before the characters "PK" in a zipfile.- Parameters:
zipfile
- The zipfile.- Returns:
- The number of bytes before the characters "PK" in a zipfile. Returns -1 if PK is not found anywhere in the file.
- Throws:
IOException
- If the file could not be read.
-
stripSFXHeader
public static void stripSFXHeader(File srcZipfile, long sfxHeaderBytes, File destZipfile) throws IOException
Strip the self-extracting archive header from the beginning of a zipfile.- Parameters:
srcZipfile
- The source zipfile.sfxHeaderBytes
- The number of bytes of the header to strip.destZipfile
- The destination to save to.- Throws:
IOException
- If the operation could not be completed.
-
logJavaInfo
public static void logJavaInfo(LogNode log)
Log the Java version and the JRE paths that were found.- Parameters:
log
- The log.
-
-