Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-03-28
4
Medium Priority
?
268 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:rajani1976
  • 3
4 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6902447
What does the 'pw' represents? What is the class?
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 300 total points
ID: 6902867
try to close your JarOutputStream:
....
 zipentry.setMethod(zipentry.DEFLATED);
 zipentry.setSize(pw.jar.length);
 jos.putNextEntry(zipentry);
 jos.write(pw.jar);
 jos.flush();
 jos.flush();
 jos.closeEntry();
// CLOSE THE STREAM
jos.close();
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7923818
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
0
 

Expert Comment

by:modulo
ID: 7978433
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question