BAEL-5632 add jar example
This commit is contained in:
parent
1199ce0b12
commit
a746e8119d
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.createjar;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.jar.*;
|
||||
import com.baeldung.createjar.JarTool;
|
||||
|
||||
public class Driver {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
JarTool tool = new JarTool();
|
||||
tool.startManifest();
|
||||
tool.addToManifest("Main-Class", "com.baeldung.createjar.HelloWorld");
|
||||
JarOutputStream target = tool.openJar("HelloWorld.jar");
|
||||
|
||||
tool.addFile(target, System.getProperty("user.dir") + "\\src\\main\\java", System.getProperty("user.dir") + "\\src\\main\\java\\com\\baeldung\\createjar\\HelloWorld.class");
|
||||
target.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.createjar;
|
||||
|
||||
public class HelloWorld {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.createjar;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.jar.*;
|
||||
|
||||
public class JarTool {
|
||||
private Manifest manifest = new Manifest();
|
||||
|
||||
public void addToManifest(String key, String value) {
|
||||
manifest.getMainAttributes()
|
||||
.put(new Attributes.Name(key), value);
|
||||
}
|
||||
|
||||
public void addDirectoryEntry(JarOutputStream target, String parentPath, File dir) throws IOException {
|
||||
String remaining = "";
|
||||
if (parentPath.endsWith(File.separator))
|
||||
remaining = dir.getAbsolutePath()
|
||||
.substring(parentPath.length());
|
||||
else
|
||||
remaining = dir.getAbsolutePath()
|
||||
.substring(parentPath.length() + 1);
|
||||
String name = remaining.replace("\\", "/");
|
||||
if (!name.endsWith("/"))
|
||||
name += "/";
|
||||
JarEntry entry = new JarEntry(name);
|
||||
entry.setTime(dir.lastModified());
|
||||
target.putNextEntry(entry);
|
||||
target.closeEntry();
|
||||
}
|
||||
|
||||
public void addFile(JarOutputStream target, String rootPath, String source) throws IOException {
|
||||
BufferedInputStream in = null;
|
||||
String remaining = "";
|
||||
if (rootPath.endsWith(File.separator))
|
||||
remaining = source.substring(rootPath.length());
|
||||
else
|
||||
remaining = source.substring(rootPath.length() + 1);
|
||||
String name = remaining.replace("\\", "/");
|
||||
JarEntry entry = new JarEntry(name);
|
||||
entry.setTime(new File(source).lastModified());
|
||||
target.putNextEntry(entry);
|
||||
in = new BufferedInputStream(new FileInputStream(source));
|
||||
byte[] buffer = new byte[1024];
|
||||
while (true) {
|
||||
int count = in.read(buffer);
|
||||
if (count == -1)
|
||||
break;
|
||||
target.write(buffer, 0, count);
|
||||
}
|
||||
target.closeEntry();
|
||||
in.close();
|
||||
}
|
||||
|
||||
public JarOutputStream openJar(String jarFile) throws IOException {
|
||||
JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile), manifest);
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setMainClass(String mainFQCN) {
|
||||
if (mainFQCN != null && !mainFQCN.equals(""))
|
||||
manifest.getMainAttributes()
|
||||
.put(Attributes.Name.MAIN_CLASS, mainFQCN);
|
||||
}
|
||||
|
||||
public void startManifest() {
|
||||
manifest.getMainAttributes()
|
||||
.put(Attributes.Name.MANIFEST_VERSION, "1.0");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue