|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.iids.aos.util.Class2Jar
public class Class2Jar
Utility class to convert a (set of) class files (from the runtime environment) to a JAR file. This utlity also creates a manifest that is contained in the resulting JAR file.
This class currently only works if the class data is present on the local file system. It queries the class loader for the calling thread to find the source of the bytecode to add to the resulting jar. This works if the class is directly on the file system or inside a jar. It is also possible to add the contents of entire packages in one go to the resulting jar file. The resulting jar file is always executable if there is at least one class in the archive. This class is convenient for those applications that require transporting a (set of) classes as a JAR that are available to the local class loader, without transporting the entire JAR file (which may contain more classes than are needed). This is also convenient if the location of the JAR file is not known in advance to the application. Programs that require the transport of a JAR file (for example, when creating an agent) can just calladdClass or addPackage and ship
the JAR file that results from getJar.
TODO Remote JAR files cannot be processed (is this even possible?).
| Constructor Summary | |
|---|---|
Class2Jar()
Creates a new Class2Jar context. |
|
| Method Summary | |
|---|---|
Class2Jar |
add(java.lang.Class... classes)
Add a (set of) Class(es) to the JAR file. |
void |
addClassPath(java.lang.String... classPathEntries)
Add entries to the Class-Path field of the manifest. |
void |
addPackage(java.lang.Package p,
boolean recursive)
|
void |
addPackage(java.lang.String pkgName,
boolean recursive)
Add all classes contained in a package to the resulting JAR. |
void |
addResource(java.lang.String path,
byte[] contents)
Put a resource in the jar file. |
static java.lang.String |
getBaseName(java.lang.String name)
|
byte[] |
getClassData(java.lang.Class cls)
Read raw class data from the classpath. |
java.lang.Iterable<java.lang.Class> |
getClasses()
|
static java.io.File |
getContainingJar(java.lang.Class cls)
Determine which JAR file a class was loaded from. |
byte[] |
getJar()
Generate a JAR file from the classes/main class. |
java.lang.Class |
getMainClass()
|
static java.lang.String |
getPathName(java.lang.Package p)
Get directory name for a given package |
static java.lang.String |
getPathName(java.lang.String packageName)
|
static byte[] |
getResource(java.net.URI zip,
java.lang.String entry)
|
static java.lang.String |
getZipFileEntryName(java.lang.Class c)
|
static java.lang.String |
getZipFileEntryName(java.lang.Package p)
|
static boolean |
isZipDirectoryEntryName(java.lang.String name)
|
static java.util.List<java.lang.Class<?>> |
listClasses(java.lang.Package pkg,
boolean recursive)
List all classes that belong to a package. |
static java.util.List<java.lang.Class<?>> |
listClasses(java.lang.String pkgName,
boolean recursive)
List all classes that belong to a package. |
static void |
main(java.lang.String[] args)
|
static byte[] |
readFile(java.net.URI file)
Read a file completely as a byte[]. |
void |
setMainClass(java.lang.Class c)
Set the default class for this JAR archive. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Class2Jar()
Classes and/or Packages to the
Class2Jar context. Once all desired classes are added, a Jar archive
containing only these classes can be obtained by calling the method
getJar.
| Method Detail |
|---|
public java.lang.Iterable<java.lang.Class> getClasses()
public java.lang.Class getMainClass()
public Class2Jar add(java.lang.Class... classes)
getJar() is called.
If no default class was set before calling this method, then the first
class in the classes list will be set as the main class. This
method also processes any (private) internal classes declared by the
classes in the list.
classes - A list of classes that will be added to the final JAR file.
public void addPackage(java.lang.Package p,
boolean recursive)
public void addPackage(java.lang.String pkgName,
boolean recursive)
pkgName - The name of the package to scan for contained classes.recursive - If true then the contents of subdirectories in the
package tree will also be scanned for classes.public void addClassPath(java.lang.String... classPathEntries)
classPathEntries - String entries for the class path.
public static java.util.List<java.lang.Class<?>> listClasses(java.lang.Package pkg,
boolean recursive)
pkg - The package to search in.recursive - Recurse into sub-packages
public static java.util.List<java.lang.Class<?>> listClasses(java.lang.String pkgName,
boolean recursive)
pkgName - The name of the package (e.g. org.iids.aos)recursive - Recurse into sub-packages
public void setMainClass(java.lang.Class c)
c - The Class to set as main class of the JAR file.
public void addResource(java.lang.String path,
byte[] contents)
path - Path name to store the contents in.contents - Raw byte stream with the file contents.
public byte[] getJar()
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException - If one or more of the classes entered through addClass
or addPackage(java.lang.Package, boolean) cannot be found in the class path.
public byte[] getClassData(java.lang.Class cls)
throws java.lang.ClassNotFoundException
cls - The class to read the data from.
java.lang.ClassNotFoundException - Some error resolving the class.public static java.io.File getContainingJar(java.lang.Class cls)
cls - The class to inspect.
File pointing to where the file is loaded
from, or null if the Class was not loaded from a
JAR file.public static byte[] readFile(java.net.URI file)
file - Location of the file to read.
public static byte[] getResource(java.net.URI zip,
java.lang.String entry)
public static java.lang.String getBaseName(java.lang.String name)
public static boolean isZipDirectoryEntryName(java.lang.String name)
public static java.lang.String getZipFileEntryName(java.lang.Class c)
public static java.lang.String getZipFileEntryName(java.lang.Package p)
public static java.lang.String getPathName(java.lang.Package p)
p -
public static java.lang.String getPathName(java.lang.String packageName)
public static void main(java.lang.String[] args)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||