use of java.util.jar.JarOutputStream in project hadoop by apache.
the class TestMRJobs method makeJobJarWithLib.
private Path makeJobJarWithLib(String testDir) throws FileNotFoundException, IOException {
Path jobJarPath = new Path(testDir, "thejob.jar");
FileOutputStream fos = new FileOutputStream(new File(jobJarPath.toUri().getPath()));
JarOutputStream jos = new JarOutputStream(fos);
// Have to put in real jar files or it will complain
createAndAddJarToJar(jos, new File(new Path(testDir, "lib1.jar").toUri().getPath()));
createAndAddJarToJar(jos, new File(new Path(testDir, "lib2.jar").toUri().getPath()));
jos.close();
localFs.setPermission(jobJarPath, new FsPermission("700"));
return jobJarPath;
}
use of java.util.jar.JarOutputStream in project hadoop by apache.
the class TestLocalJobSubmission method makeJar.
private Path makeJar(Path p) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(p.toString()));
JarOutputStream jos = new JarOutputStream(fos);
ZipEntry ze = new ZipEntry("test.jar.inside");
jos.putNextEntry(ze);
jos.write(("inside the jar!").getBytes());
jos.closeEntry();
jos.close();
return p;
}
use of java.util.jar.JarOutputStream in project hbase by apache.
the class ClassLoaderTestHelper method createJarArchive.
/**
* Jar a list of files into a jar archive.
*
* @param archiveFile the target jar archive
* @param tobejared a list of files to be jared
*/
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
try {
byte[] buffer = new byte[BUFFER_SIZE];
// Open archive file
FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream, new Manifest());
for (int i = 0; i < tobeJared.length; i++) {
if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
continue;
}
// Add archive entry
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
jarAdd.setTime(tobeJared[i].lastModified());
out.putNextEntry(jarAdd);
// Write file to archive
FileInputStream in = new FileInputStream(tobeJared[i]);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}
out.close();
stream.close();
LOG.info("Adding classes to jar file completed");
return true;
} catch (Exception ex) {
LOG.error("Error: " + ex.getMessage());
return false;
}
}
use of java.util.jar.JarOutputStream in project hbase by apache.
the class TestClassFinder method packageAndLoadJar.
/**
* Makes a jar out of some class files. Unfortunately it's very tedious.
* @param filesInJar Files created via compileTestClass.
* @return path to the resulting jar file.
*/
private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception {
// First, write the bogus jar file.
String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar";
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
FileOutputStream fos = new FileOutputStream(path);
JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest);
// Directory entries for all packages have to be added explicitly for
// resources to be findable via ClassLoader. Directory entries must end
// with "/"; the initial one is expected to, also.
Set<String> pathsInJar = new HashSet<>();
for (FileAndPath fileAndPath : filesInJar) {
String pathToAdd = fileAndPath.path;
while (pathsInJar.add(pathToAdd)) {
int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2);
if (ix < 0) {
break;
}
pathToAdd = pathToAdd.substring(0, ix);
}
}
for (String pathInJar : pathsInJar) {
jarOutputStream.putNextEntry(new JarEntry(pathInJar));
jarOutputStream.closeEntry();
}
for (FileAndPath fileAndPath : filesInJar) {
File file = fileAndPath.file;
jarOutputStream.putNextEntry(new JarEntry(fileAndPath.path + file.getName()));
byte[] allBytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(allBytes);
fis.close();
jarOutputStream.write(allBytes);
jarOutputStream.closeEntry();
}
jarOutputStream.close();
fos.close();
// Add the file to classpath.
File jarFile = new File(path);
assertTrue(jarFile.exists());
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() });
return jarFile.getAbsolutePath();
}
use of java.util.jar.JarOutputStream in project hbase by apache.
the class JarFinder method createJar.
private static void createJar(File dir, File jarFile) throws IOException {
Preconditions.checkNotNull(dir, "dir");
Preconditions.checkNotNull(jarFile, "jarFile");
File jarDir = jarFile.getParentFile();
if (!jarDir.exists()) {
if (!jarDir.mkdirs()) {
throw new IOException(MessageFormat.format("could not create dir [{0}]", jarDir));
}
}
try (FileOutputStream fos = new FileOutputStream(jarFile);
JarOutputStream jos = new JarOutputStream(fos)) {
jarDir(dir, "", jos);
}
}
Aggregations