Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use jar untility in java program to jar files?  RSS feed

 
Sue Stephen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing a web application with JSP/Servlet/Tomcat 5. I am trying to generate a directory with files dynamically on the server side, make a jar of it and write it to the browser so that the user can choose to download the jar file. I tried to use jar untility to jar the files in a jsp. In this case, a jar file is created and a download popup box is displayed to the user. Howerver, a file exception is thrown at the same time saying the dynamically generated jar file can not be found, although the jar file is there. If I don't use the jar utility to jar files and directly write the jar file that is already present, the error won't appear.


Does anyone know if I can use jar utility in this way and what could be the problem? Any suggestion is greatly appreciated.


--------------------
error:
-------------------

java.io.FileNotFoundException: c:\testjar\jar1.jar (The
system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at org.apache.jsp.download_jsp._jspService(download_jsp.java:167)

--------------
download_jsp
-----------------
....

Runtime rt = Runtime.getRuntime();
String command = "jar cf c:\\testjar\\jar1.jar c:\\testdir\\*";
Process ps = rt.exec(command);

try{
String disHeader = "Attachment;Filename=\"jar1.jar\"";// + jarName;
response.setHeader("Content-Disposition", disHeader);
response.setContentType("APPLICATION/OCTET-STREAM");
FileInputStream fileInputStream = new FileInputStream("c:\\testjar\\jar1.jar"); // Error at this line

int i;
while ((i=fileInputStream.read())!=-1)
{
out.write(i);
}
fileInputStream.close();
//out.close();
}

catch(Exception e)
{
e.printStackTrace();
}
.........
---------------------
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than make system calls which in turn call a java program, you might want to look in to java.util.zip:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html#package_description


Also the build program ANT has functionality for zipping and unzipping jar files. Since it's open source, you can download it and muck around to see exactly how they're doing it.
 
Sue Stephen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the information. I know that I can use Java.util.jar to jar files in java program. But it seems simpler to use jar.exe utility. Is there any reason why this won't work properly and has it been done?
 
mark chester
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I you use the jar command line tool your web application will not be portable to other platforms...

The zip/jar reader packages aren't THAT hard to get to grips with. You will find they other much more functionality
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use ANT. And make a .war file which will do exactly what you want (.jar won't, but you may need it in .war depending on how you want it).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!