Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

831 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