Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes creating jar using java.util.jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating jar using java.util.jar" Watch "creating jar using java.util.jar" New topic
Author

creating jar using java.util.jar

kala praveen
Greenhorn

Joined: Nov 04, 2004
Posts: 21
Greetings..!

I am using java.util.jar.* to create a jar file from Java code. It is creating the file properly with an empty manifest file. But, when I load this JAR into the application server the server is not recognizing it. If I use the same set of files and create a JAR file using the jar -cvf command at dos prompt and load it into application server it recognizes it. What could be the problem?

Both Jar files are having same set of files, same size except the manifest.mf file.

Following is the code that I am using.


final int BUFFER = 2048;
Manifest man=new Manifest();
BufferedInputStream origin = null;
File f = null;
String[] files = null;
FileOutputStream dest = new FileOutputStream("Adv-Rep.jar");
JarOutputStream out = new JarOutputStream(new BufferedOutputStream(dest),man);
byte[] data = new byte[BUFFER];
f = new File("class-files" + seperator + "org" +
seperator + "mirapoint");
files = f.list();

for (int i = 0; i < files.length; i++) {
System.out.println("Adding: " + files[i]);

FileInputStream fi = new FileInputStream("org" + seperator +
"mirapoint" + seperator + files[i]);


origin = new BufferedInputStream(fi, BUFFER);

JarEntry entry = new JarEntry("org" + seperator + "mirapoint" +
seperator + files[i]);
out.putNextEntry(entry);

int count;

while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}

origin.close();
}

f = new File "META-INF");


files = f.list();

for (int i = 0; i < files.length; i++) {
System.out.println("Adding: " + files[i]);

FileInputStream fi = null;
fi = new FileInputStream("META-INF" +
seperator + files[i]);
}

origin = new BufferedInputStream(fi, BUFFER);

JarEntry entry = new JarEntry("meta-inf" + seperator + files[i]);
out.putNextEntry(entry);

int count;

while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}

origin.close();
}

out.close();
flag=true;


How to create a perfect JAR file thru JAVA code? What must be the Manifest.mf entries?

Thanks in Advance..!

Kala.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Why don't you use Eclipse for this purpose. This provides a simple tool to create jars.

Just create the project in Eclipse and then right click and say export.

Select the Jar option and follow the steps.

If you want to add jar in the class path open the jar's manifest and add the paths in the class path field.

Class-Path: jar1.jar jar2.jar
kala praveen
Greenhorn

Joined: Nov 04, 2004
Posts: 21
Hi Pradeep,

I am not trying to create the JAR for development purpose. I am writing an installer which takes some different class files at run time and create a JAR.

Kala.
M murali krishna reddy
Greenhorn

Joined: Jul 20, 2008
Posts: 1
Does anyone have an answer to this..I'm also facing the same problem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to JavaRanch.

Please don't wake the zombies. Note that the "zombie" you resurrected is more than 4 years old. Just start a new topic with your question, instead of resurrecting and old topic.

A simple Google search found me some promising links:

Creating ZIP and JAR Files
Create Jar file

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating jar using java.util.jar
 
Similar Threads
problem in attached file .
Problem zipping the file
Corrupt file name after compression.
Not able to put some files inside Zip file
Unable to create ,zip file