Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in copying JAR file  RSS feed

 
Padhy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Has anyone used Java I/O classes to copy one JAR file to another? If so could you please let me know what is wrong with the following code? The following code runs without error and copies the source jar file to the destination with "zero" byte size.
If you have a better solution to copy JAR file at the runtime, please let me know.
Code starts:
-----------------
<Pre>
import java.io.*;
import java.util.jar.*;
public class CopyJarFile {
public static void main(String args[]) {
String newLine = System.getProperty("line.separator");
FileInputStream fis = null;
FileOutputStream fos = null;
JarInputStream jis = null;
JarOutputStream jos = null;
BufferedReader br = null;
BufferedWriter bw = null;
File source = null;
if (args.length < 2)
System.out.println("Command Syntax: CopyFile <source>
<dest>\n\n<source>\tThe source file name (path optional)\n<dest>\t\tThe
destination file name (path optional)");
else
{
try
{
fis = new FileInputStream(args[0]);
fos = new FileOutputStream(args[1]);
jis = new JarInputStream(fis);
jos = new JarOutputStream(fos);
br = new BufferedReader(new InputStreamReader(jis));
bw = new BufferedWriter(new OutputStreamWriter(jos));
/* Determine the size of the buffer to allocate */
source = new File(args[0]);
int fileLength = (int) source.length();
char charBuff[] = new char[fileLength];
while (br.read(charBuff,0,fileLength) != -1)
bw.write(charBuff,0,fileLength);
}
catch(FileNotFoundException fnfe)
{System.out.println(args[0] + " does not exist!");}
catch(IOException ioe)
{System.out.println("Error reading/writing files!");}
finally
{
try
{
if (br != null)
br.close();
if (bw != null)
bw.close();
}
catch(IOException ioe){}
}

}
}
}
</Pre>
----------------
Code ends:
Thanks,
Padhy
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padhy,
I think your problem is using a BufferedReader/BufferedWriter. Reader/Writer classes and their subclasses are meant to be used to read/write text files (i.e. character streams). A jar file is a binary file, so you should use BufferedInputStream/BufferedOutputStream.
Please note: The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name.
Regards,
-Peter
[This message has been edited by Peter Tran (edited January 21, 2001).]
 
Padhy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Thanks for your prompt reply. About the naming policy: "Padhy" is the only actual name and I don't have any other initials. That is the reason I could not stick to your two word naming policy.
Per your suggestion, I changed the code to make use of BufferedInputStream instead of Bufferedreader but the problem still exists. Could you please look at the code and let me know if I'm doing anything wrong?
Code Starts:
-----------
<Pre>
import java.io.*;
import java.util.jar.*;
public class CopyJarFile {
public static void main(String args[]) {
/**
* This method copies the jar file
*/
FileInputStream fis = null;
FileOutputStream fos = null;
JarInputStream jis = null;
JarOutputStream jos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File source = null;
if (args.length < 2)
System.out.println("Command Syntax: CopyJarFile <source> <dest>\n\n<source>\tThe source file name (path optional)\n<dest>\t\tThe destination file name (path optional)");
else
{
try
{
fis = new FileInputStream(args[0]);
fos = new FileOutputStream(args[1]);
jis = new JarInputStream(fis);
jos = new JarOutputStream(fos);
bis = new BufferedInputStream(jis);
bos = new BufferedOutputStream(jos);
/* Determine the size of the buffer to allocate */
source = new File(args[0]);
int fileLength = (int) source.length();
byte byteBuff[] = new byte[fileLength];
while (bis.read(byteBuff,0,fileLength) != -1)
bos.write(byteBuff,0,fileLength);
}
catch(FileNotFoundException fnfe)
{System.out.println(args[0] + " does not exist!");}
catch(IOException ioe)
{System.out.println("Error reading/writing files!");}
finally
{
try
{
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
catch(IOException ioe){}
}

}
}
}
</Pre>
--------
End of Code
Regards,
Padhy
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padhy,
You don't have a first name and a last name?
Anway, in your code you're using a java.io.JarFile class which isn't appropriate for what you want to do. The JarFile class allows you to look at the content of the jar file and do some manipulation of that content. To copy the jar file from one location to another is like copying any other binary file.
Check out this link to see an example of how this should be done.
http://www.javaranch.com/ubb/Forum38/HTML/000151.html
-Peter
[This message has been edited by Peter Tran (edited January 22, 2001).]
 
Padhy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Peter. It works fine.
P.S. I don't have last name. "FNU"
 
Krishna Moorthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Padhy,
i saw ur problem. hope now u got a solution . At present, i am having the same problem, that is, problem in copying files from one jar file to the another. pls mail me the solution when u find time . my mail id is krishivaa@yahoo.com and put cc to krishnamoorthy.k@tek.com

------------------
Regards and Thanks,
Krishna.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!