Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in copying files for one jar file to the other

 
Krishna Moorthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having 5 files in a "first.jar" file. I want to move the files from first.jar to another jar file say, "second.jar"
first.jar file contains:
-----------------------------
META-INF/,true,0
META-INF/MANIFEST.MF,false,68
example/,true,0
example/Details.class,false,2695
example/Personal.class,false,1549
example/Qualification.class,false,1120
example/VersionNumber.class,false,1697
version.txt,false,3

I am using JarOutputStream and putNextEntry(). I had written the code to do it. In this, i want to modify one file ("version.txt")and copy this updated file to the "second.jar" and then rename "second.jar" to "first.jar". But, with the following code i am getting exception which is aslo enclosed. Can anyone help me ?
The CODE is:
--------------------
public void updateJar(String newVersion) throws java.lang.Exception {
String jarfilePath = "D:\\krish\\implement\\update.jar";
OutputStream os = new FileOutputStream(jarfilePath);
JarOutputStream jos = new JarOutputStream(os);
try
{
Enumeration enum = jar.entries();
while(enum.hasMoreElements())
{
ZipEntry ze = (ZipEntry)enum.nextElement();
if(ze.getName().equals("version.txt"))
{
java.io.ByteArrayOutputStream fout = new java.io.ByteArrayOutputStream();
String currentVersion = newVersion;
byte buf[] = newVersion.getBytes();
fout.write(buf);
OutputStream foutStr = new FileOutputStream("version.txt");
fout.writeTo(foutStr);
foutStr.close();
fout.close();
}
System.out.println(ze.getName() + "," + ze.isDirectory() +
"," + ze.getSize() );
jos.putNextEntry(ze);
jos.finish();
}
}
catch(Exception ex)
{
System.out.println("Error : " +ex.getMessage());
ex.printStackTrace();
}
os.close();
}
With this code, it reads
----------------------------------
META-INF/,true,0
META-INF/MANIFEST.MF,false,68
example/,true,0
and then it throws the following exception:
Error : invalid entry size (expected 68 but got 0 bytes)
java.util.zip.ZipException: invalid entry size (expected 68 but got 0 bytes)
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
java.io.IOException(java.lang.String)
java.util.zip.ZipException(java.lang.String)
void java.util.zip.ZipOutputStream.closeEntry()
void java.util.zip.ZipOutputStream.putNextEntry(java.util.zip.ZipEntry)
void java.util.jar.JarOutputStream.putNextEntry(java.util.zip.ZipEntry)
void example.JarReader.updateJar(java.lang.String)
void example.JarReader.list()
void example.JarReader.main(java.lang.String [])

------------------
Regards and Thanks,
Krishna.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic