Java Code Examples for jdk.testlibrary.JarUtils#createJarFile()

The following examples show how to use jdk.testlibrary.JarUtils#createJarFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaAgentBuilder.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build a java agent jar file with a given agent class.
 * The agent class will be added as both premain class and agent class.
 *
 * @param agentClass fully qualified name of an agent class
 * @param agentJar   file name of the agent jar to be created
 *                   the file will be placed in a current work directory
 * @throws IOException
 */
public static void build(String agentClass, String agentJar) throws IOException {
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    attrs.putValue("Premain-Class", agentClass);
    attrs.putValue("Agent-Class", agentClass);

    Path jarFile = Paths.get(".", agentJar);
    String testClasses = Utils.TEST_CLASSES;
    String agentPath = agentClass.replace(".", File.separator) + ".class";
    Path agentFile = Paths.get(testClasses, agentPath);
    Path dir = Paths.get(testClasses);
    JarUtils.createJarFile(jarFile, mf, dir, agentFile);
    System.out.println("Agent built:" + jarFile.toAbsolutePath());
}
 
Example 2
Source File: JarClassPathFileEntry.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    // Create Other.class in OTHER_DIR, off the default classpath
    byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
                                                   "public class Other {}");
    ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);

    // Create Other.jar in OTHER_DIR
    JarUtils.createJarFile(OTHER_JAR_PATH,
                           Paths.get(OTHER_DIR),
                           Paths.get(OTHER_DIR, "Other.class"));

    // Create Context.class
    klassbuf = InMemoryJavaCompiler.compile("Context",
                                            "public class Context {}");
    ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);

    // Create Context.jar w/ "file:" entry for Other.jar
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");

    String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
                                                  :            OTHER_JAR_PATH.toString());
    attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);

    System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
    JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
                           Paths.get(TEST_CLASSES),
                           Paths.get(TEST_CLASSES, "Context.class"));

    // Use URLClassLoader w/ Context.jar to load Other.class, which will
    // load via the Class-Path entry
    URL url = CONTEXT_JAR_PATH.toUri().toURL();
    URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
                                            null); // don't delegate to App CL
    Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
    System.out.println("Loaded: " + otherClass);
}
 
Example 3
Source File: JarClassPathFileEntry.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    // Create Other.class in OTHER_DIR, off the default classpath
    byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
                                                   "public class Other {}");
    ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);

    // Create Other.jar in OTHER_DIR
    JarUtils.createJarFile(OTHER_JAR_PATH,
                           Paths.get(OTHER_DIR),
                           Paths.get(OTHER_DIR, "Other.class"));

    // Create Context.class
    klassbuf = InMemoryJavaCompiler.compile("Context",
                                            "public class Context {}");
    ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);

    // Create Context.jar w/ "file:" entry for Other.jar
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");

    String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
                                                  :            OTHER_JAR_PATH.toString());
    attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);

    System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
    JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
                           Paths.get(TEST_CLASSES),
                           Paths.get(TEST_CLASSES, "Context.class"));

    // Use URLClassLoader w/ Context.jar to load Other.class, which will
    // load via the Class-Path entry
    URL url = CONTEXT_JAR_PATH.toUri().toURL();
    URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
                                            null); // don't delegate to App CL
    Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
    System.out.println("Loaded: " + otherClass);
}
 
Example 4
Source File: JarClassPathFileEntry.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    // Create Other.class in OTHER_DIR, off the default classpath
    byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
                                                   "public class Other {}");
    ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);

    // Create Other.jar in OTHER_DIR
    JarUtils.createJarFile(OTHER_JAR_PATH,
                           Paths.get(OTHER_DIR),
                           Paths.get(OTHER_DIR, "Other.class"));

    // Create Context.class
    klassbuf = InMemoryJavaCompiler.compile("Context",
                                            "public class Context {}");
    ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);

    // Create Context.jar w/ "file:" entry for Other.jar
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");

    String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
                                                  :            OTHER_JAR_PATH.toString());
    attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);

    System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
    JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
                           Paths.get(TEST_CLASSES),
                           Paths.get(TEST_CLASSES, "Context.class"));

    // Use URLClassLoader w/ Context.jar to load Other.class, which will
    // load via the Class-Path entry
    URL url = CONTEXT_JAR_PATH.toUri().toURL();
    URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
                                            null); // don't delegate to App CL
    Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
    System.out.println("Loaded: " + otherClass);
}
 
Example 5
Source File: JarClassPathFileEntry.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    // Create Other.class in OTHER_DIR, off the default classpath
    byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
                                                   "public class Other {}");
    ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);

    // Create Other.jar in OTHER_DIR
    JarUtils.createJarFile(OTHER_JAR_PATH,
                           Paths.get(OTHER_DIR),
                           Paths.get(OTHER_DIR, "Other.class"));

    // Create Context.class
    klassbuf = InMemoryJavaCompiler.compile("Context",
                                            "public class Context {}");
    ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);

    // Create Context.jar w/ "file:" entry for Other.jar
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");

    String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
                                                  :            OTHER_JAR_PATH.toString());
    attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);

    System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
    JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
                           Paths.get(TEST_CLASSES),
                           Paths.get(TEST_CLASSES, "Context.class"));

    // Use URLClassLoader w/ Context.jar to load Other.class, which will
    // load via the Class-Path entry
    URL url = CONTEXT_JAR_PATH.toUri().toURL();
    URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
                                            null); // don't delegate to App CL
    Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
    System.out.println("Loaded: " + otherClass);
}
 
Example 6
Source File: ClassForNameLeak.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    Path classFile = FileSystems.getDefault().getPath(TESTCLASSES,
                                                      CLASSFILENAME);

    // Create a temporary .jar file containing ClassForName.class
    Path testClassesDir = Paths.get(TESTCLASSES);
    Path jarFilePath = Files.createTempFile("cfn", ".jar");
    JarUtils.createJarFile(jarFilePath, testClassesDir, classFile);
    jarFilePath.toFile().deleteOnExit();

    // Remove the ClassForName.class file that jtreg built, to make sure
    // we're loading from the tmp .jar
    Files.delete(classFile);

    // Make simultaneous calls to the test method, to stress things a bit
    ExecutorService es = Executors.newFixedThreadPool(THREADS);

    List<Callable<PhantomReference<ClassLoader>>> callables =
            Stream.generate(() -> {
                Callable<PhantomReference<ClassLoader>> cprcl = () -> {
                    return loadAndRun(jarFilePath);
                };
                return cprcl;
            }).limit(THREADS).collect(Collectors.toList());

    List<Future<PhantomReference<ClassLoader>>> refs = es.invokeAll(callables);

    // Give the GC a chance to enqueue the PhantomReferences
    for (int i = 0; i < 10; i++) {
        System.gc();
    }
    // Make sure all PhantomReferences to the leaked classloader are enqueued
    for (int j = 0; j < THREADS; j++) {
        Reference rmRef = rq.remove(TIMEOUT);
        if (rmRef == null) {
            throw new RuntimeException("ClassLoader was never enqueued!");
        } else {
            System.out.println("Enqueued " + rmRef);
        }
    }
    System.out.println("All Classloaders successfully enqued");
}
 
Example 7
Source File: ClassForNameLeak.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    Path classFile = FileSystems.getDefault().getPath(TESTCLASSES,
                                                      CLASSFILENAME);

    // Create a temporary .jar file containing ClassForName.class
    Path testClassesDir = Paths.get(TESTCLASSES);
    Path jarFilePath = Files.createTempFile("cfn", ".jar");
    JarUtils.createJarFile(jarFilePath, testClassesDir, classFile);
    jarFilePath.toFile().deleteOnExit();

    // Remove the ClassForName.class file that jtreg built, to make sure
    // we're loading from the tmp .jar
    Files.delete(classFile);

    // Make simultaneous calls to the test method, to stress things a bit
    ExecutorService es = Executors.newFixedThreadPool(THREADS);

    List<Callable<PhantomReference<ClassLoader>>> callables =
            Stream.generate(() -> {
                Callable<PhantomReference<ClassLoader>> cprcl = () -> {
                    return loadAndRun(jarFilePath);
                };
                return cprcl;
            }).limit(THREADS).collect(Collectors.toList());

    List<Future<PhantomReference<ClassLoader>>> refs = es.invokeAll(callables);

    // Give the GC a chance to enqueue the PhantomReferences
    for (int i = 0; i < 10; i++) {
        System.gc();
    }
    // Make sure all PhantomReferences to the leaked classloader are enqueued
    for (int j = 0; j < THREADS; j++) {
        Reference rmRef = rq.remove(TIMEOUT);
        if (rmRef == null) {
            throw new RuntimeException("ClassLoader was never enqueued!");
        } else {
            System.out.println("Enqueued " + rmRef);
        }
    }
    System.out.println("All Classloaders successfully enqued");
}
 
Example 8
Source File: ClassForNameLeak.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    Path classFile = FileSystems.getDefault().getPath(TESTCLASSES,
                                                      CLASSFILENAME);

    // Create a temporary .jar file containing ClassForName.class
    Path testClassesDir = Paths.get(TESTCLASSES);
    Path jarFilePath = Files.createTempFile("cfn", ".jar");
    JarUtils.createJarFile(jarFilePath, testClassesDir, classFile);
    jarFilePath.toFile().deleteOnExit();

    // Remove the ClassForName.class file that jtreg built, to make sure
    // we're loading from the tmp .jar
    Files.delete(classFile);

    // Make simultaneous calls to the test method, to stress things a bit
    ExecutorService es = Executors.newFixedThreadPool(THREADS);

    List<Callable<PhantomReference<ClassLoader>>> callables =
            Stream.generate(() -> {
                Callable<PhantomReference<ClassLoader>> cprcl = () -> {
                    return loadAndRun(jarFilePath);
                };
                return cprcl;
            }).limit(THREADS).collect(Collectors.toList());

    List<Future<PhantomReference<ClassLoader>>> refs = es.invokeAll(callables);

    // Give the GC a chance to enqueue the PhantomReferences
    for (int i = 0; i < 10; i++) {
        System.gc();
    }
    // Make sure all PhantomReferences to the leaked classloader are enqueued
    for (int j = 0; j < THREADS; j++) {
        Reference rmRef = rq.remove(TIMEOUT);
        if (rmRef == null) {
            throw new RuntimeException("ClassLoader was never enqueued!");
        } else {
            System.out.println("Enqueued " + rmRef);
        }
    }
    System.out.println("All Classloaders successfully enqued");
}