Link to home
Start Free TrialLog in
Avatar of rajani1976
rajani1976

asked on

How to create a jar having the conents of the jar in byte array?

Hi,

I have a server and client application. My server returns the contents of the jar in the form of byte array to the client on a particluar method call. Now at the client end, i want to reconstruct the jar to be used. How do I do this?

The server uses the JarInputStream to read the contents of the jar to be returned. I have tried using the JarOutputStream, but looks like this works fine if we have spearate class files to be added to the jar. Now that I have the contents of the jar itself, how do i recreate the jar?


Here is the server code that send the jar contents as bytes

byte[] mtd1()
{
   JarFile jar=new JarFile("new.jar") ;
   JarEntry jarEntry = jar.getJarEntry(
                    "A.class") ;
  InputStream jarStream = jar.getInputStream(jarEntry) ;
  byte[] jarContents = new byte[jarStream.available()] ;
  //Read the contents into a byte array
  int len= jarStream.read(jarContents, 0,
                     jarStream.available()) ;
return jarContents ;
}

At the client end, I tried
  File tmp = new File("D:\\Zenith\\ContentMgmt\\FS0\\temp.jar") ;
  FileOutputStream out = new FileOutputStream (tmp) ;

  JarOutputStream jos = new JarOutputStream(out) ;
  ZipEntry zipentry = new ZipEntry(pw.className);
  zipentry.setMethod(zipentry.DEFLATED);
  zipentry.setSize(pw.jar.length);
  jos.putNextEntry(zipentry);
  jos.write(pw.jar);
  jos.flush();
  jos.flush();
  jos.closeEntry();

This seems to create a temp.jar, but when i try to view using Winzip, i cannot see any files in it. But with jar tvf temp.jar command at the prompt shows me
    "3281 Thu Mar 28 14:15:26 IST 2002 A.class"

I am unable to figure out what is happening.
Can anyone please help me with this?

Thanks
Rajani
Avatar of Venci75
Venci75

What does the 'pw' represents? What is the class?
ASKER CERTIFIED SOLUTION
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Venci75
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange