Solved

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

Posted on 2002-03-28
4
238 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 75 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now