2

I'm trying to create working .jar file programmatically.

Firstly, I generate .class file and then put it into my JarEntry like this:

FileOutputStream stream = new FileOutputStream(jarFile);
JarOutputStream output = new JarOutputStream(stream, manifest);
JarEntry entry = new JarEntry(token.getPackage().getName().replace(".", File.separator).concat(File.separator).concat(exactName).concat(".class"));
output.putNextEntry(entry);

BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathToClass.concat(exactName).concat(".class")));
 byte [] buffer = new byte[1024];
 int read = 0;
 while(true) {
     read = in.read(buffer);
     if(read < 0)
         break;
     output.write(buffer, 0, read);
 }

 output.closeEntry();
 output.close();
 in.close();

After that I'm creating .jar file using command:

jar cmf manifest.txt Test.jar javax/xml/bind/*.class

My manifest.txt file:

Manifest-Version: 1.0
Class-Path: ..\artifacts\ImplementorTest.jar
Main-Class: javax.xml.bind.ElementImpl

So size of output .jar files differs on ~100 byte (jar file generated by "jar .." is bigger). And moreover .jar file generated programmatically doesn't work. What the reason of such behaviour?

Known issue is line "Created-By: 1.8.0_20 (Oracle Corporation)" in autogenerated jar file. But I'm talking about jar files with equal manifest and .class files.

  • 2
    `And moreover .jar file generated programmatically doesn't work`. What do you mean by doesn't work? `Known issue is line "Created-By: 1.8.0_20 (Oracle Corporation)` What do you mean by this? – Chetan Kinger May 20 '15 at 15:08
  • I mean that it fails with "Could not find or load main class javax.xml.bind.ElementImpl". But generated by cmd file can find that file and can execute it. "Created-By.." string adds some bytes to .jar file. And my difference is measured after removing that string. Manifest files are equal. – Svetozar Milykh May 20 '15 at 15:15
  • Are you sure manifests are the same? And file structure of the jar? And actual files in the jar? – Alex May 20 '15 at 16:35

0 Answers0