JarOutputStream을 사용하여 JAR 파일을 생성하는 방법은 무엇입니까?
어떻게 프로그래밍 방식으로 JAR 파일을 생성 java.util.jar.JarOutputStream
합니까? 내 프로그램에서 생성 된 JAR 파일은 올바로 보이지만 (잘 추출 됨) 라이브러리를로드하려고 할 때 Java는 그 안에 명확하게 저장된 파일을 찾을 수 없다고 불평합니다. JAR 파일을 추출하고 Sun의 jar
명령 줄 도구를 사용하여 다시 압축하면 결과 라이브러리가 제대로 작동합니다. 요컨대, 내 JAR 파일에 문제가 있습니다.
매니페스트 파일로 완성 된 JAR 파일을 프로그래밍 방식으로 만드는 방법을 설명하십시오.
그 밖으로 그것은집니다 JarOutputStream
세 문서화되지 않은 단점이있다 :
- 디렉토리 이름은 '/'슬래시로 끝나야합니다.
- 경로는 '\'가 아닌 '/'슬래시를 사용해야합니다.
- 항목은 '/'슬래시로 시작할 수 없습니다.
Jar 파일을 만드는 올바른 방법은 다음과 같습니다.
public void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
add(new File("inputDirectory"), target);
target.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try
{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(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();
}
finally
{
if (in != null)
in.close();
}
}
There's another "quirk" to pay attention: All JarEntry's names should NOT begin with "/".
For example: The jar entry name for the manifest file is "META-INF/MANIFEST.MF" and not "/META-INF/MANIFEST.MF".
The same rule should be followed for all jar entries.
Here's some sample code for creating a JAR file using the JarOutputStream:
You can do it with this code:
public void write(File[] files, String comment) throws IOException {
FileOutputStream fos = new FileOutputStream(PATH + FILE);
JarOutputStream jos = new JarOutputStream(fos, manifest);
BufferedOutputStream bos = new BufferedOutputStream(jos);
jos.setComment(comment);
for (File f : files) {
print("Writing file: " + f.toString());
BufferedReader br = new BufferedReader(new FileReader(f));
jos.putNextEntry(new JarEntry(f.getName()));
int c;
while ((c = br.read()) != -1) {
bos.write(c);
}
br.close();
bos.flush();
}
bos.close();
// JarOutputStream jor = new JarOutputStream(new FileOutputStream(PATH + FILE), manifest);
}
PATH
variable: path to JAR file
FILE
variable: name and format
ReferenceURL : https://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
'programing' 카테고리의 다른 글
SQL Server 에이전트 작업-존재 후 삭제 하시겠습니까? (0) | 2021.01.19 |
---|---|
C #을 사용하여 프로그래밍 방식으로 로컬 컴퓨터 저장소에 인증서를 설치하려면 어떻게해야합니까? (0) | 2021.01.19 |
Eclipse Android 변경 API 레벨 (0) | 2021.01.19 |
브라우저 콘솔에서 모든 JavaScript 이벤트 모니터링 (0) | 2021.01.19 |
HTML5 캔버스 및 앤티 앨리어싱 (0) | 2021.01.19 |