Solved

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

Posted on 2002-03-28
4
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 68
Delete Android all or certain applications data programmatically 9 153
Java string replace 11 77
Java program running SQL query 5 66
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:
Suggested Courses

739 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