Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating jar using java.util.jar

 
kala praveen
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have an answer to this..I'm also facing the same problem
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic