kalyandm
asked on
read application.xml or manifest
Hi friends
I have an EAR file which has application.xml and manifest.mf files.Now I have a java class which is alos part of the same EAR and has a method that needs to read a property from manifest.mf called "Implementation-Version".P lease can anyone suggest how I can do this.
Thanks
I have an EAR file which has application.xml and manifest.mf files.Now I have a java class which is alos part of the same EAR and has a method that needs to read a property from manifest.mf called "Implementation-Version".P
Thanks
ASKER
Thanks .but I want to know How I can read the manifest from within ear for eg. manifest is located in meta-inf/ within ear >how can I get refrence that and then get it's attributes.
ASKER
Hi
Thanks.I have seen thsi example already and we have to pass the name of the jar as an argument so it works fine but in my case I already have an ear and my class should find the manifest file within this ear and then read the properties from the manifest file.if you can help on this please
Thanks.I have seen thsi example already and we have to pass the name of the jar as an argument so it works fine but in my case I already have an ear and my class should find the manifest file within this ear and then read the properties from the manifest file.if you can help on this please
i will try to find out, but its not that easy. it will be good if you increase the points.
ASKER
points increased!!
following is the program that reads the manifest file in a ear file (Archive & Directory Format)
use like this
java EarVersion Your_EAR_Directory
java EarVersion YourEar.ear
import java.io.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Attributes;
public class EarVersion{
private static final String ATTR_IMPL_VERSION="Impleme ntation-Ve rsion";
private static final String ATTR_IMPL_TITLE="Implement ation-Titl e";
private static final String ATTR_IMPL_VENDOR="Implemen tation-Ven dor";
private static void printUsage(){
System.out.println("java -jar earversion.jar {EAR file|Directory}");
}
public void directoryVersionInfo(File directory)throws Exception{
String [] archives = directory.list(new java.io.FilenameFilter(){
public boolean accept(File dir, String name){
boolean result = (name.endsWith(".jar")||na me.endsWit h(".war")) ||new File(dir,name).isDirectory ();
return result;
}
});
for(int i = 0;i<archives.length;i++){
File file = new File(directory, archives[i]);
if(file.isDirectory()){
directoryVersionInfo(file) ;
System.out.println(file.ge tAbsoluteP ath());
java.util.jar.Manifest earManifest = new java.util.jar.Manifest(new FileInputStream(file.getAb solutePath ().toStrin g()+"\\MAN IFEST.MF") );
Attributes attribMap = earManifest.getMainAttribu tes();
displayVersionInfo(attribM ap);
}
}
}
private static void displayVersionInfo(Attribu tes attribMap){
String version = (String) attribMap.getValue(ATTR_IM PL_VERSION );
if(version!=null){
System.out.println("\tVers ion: " + version);
}
String vendor = (String) attribMap.getValue(ATTR_IM PL_VENDOR) ;
if(vendor!=null){
System.out.println("\tVend or: " + vendor);
}
String title = (String) attribMap.getValue(ATTR_IM PL_TITLE);
if(title!=null){
System.out.println("\tTitl e: " + title);
}
if(version==null&&title==n ull&vendor ==null){
System.out.println("\tNo version information available.");
}
}
public void archiveVersionInfo(File archive)throws Exception{
JarFile earFile = new JarFile(archive, true, JarFile.OPEN_READ);
try{
java.util.jar.Manifest earManifest = earFile.getManifest();
if (earManifest == null) {
throw new Exception("No Manifest file in " + archive.getName());
}
Attributes attribMap = earManifest.getMainAttribu tes();
if (attribMap == null) {
throw new Exception("Invalid Manifest file in " + archive.getName());
}
System.out.println("Hello/ " +archive.getName());
displayVersionInfo(attribM ap);
}finally{
earFile.close();
}
}
public static final void main(String [] args){
if(args.length<1){
printUsage();
return ;
}
String earFileName = args[0];
File earFile = new File(earFileName);
EarVersion versionChecker = new EarVersion();
try{
if (earFile.isDirectory()) {
versionChecker.directoryVe rsionInfo( earFile);
}
else {
versionChecker.archiveVers ionInfo(ea rFile);
}
}catch(Exception ex){
System.err.println("Error obtaining version for Enterprise Application Repository: "+ex.toString());
ex.printStackTrace();
}
}
}
use like this
java EarVersion Your_EAR_Directory
java EarVersion YourEar.ear
import java.io.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Attributes;
public class EarVersion{
private static final String ATTR_IMPL_VERSION="Impleme
private static final String ATTR_IMPL_TITLE="Implement
private static final String ATTR_IMPL_VENDOR="Implemen
private static void printUsage(){
System.out.println("java -jar earversion.jar {EAR file|Directory}");
}
public void directoryVersionInfo(File directory)throws Exception{
String [] archives = directory.list(new java.io.FilenameFilter(){
public boolean accept(File dir, String name){
boolean result = (name.endsWith(".jar")||na
return result;
}
});
for(int i = 0;i<archives.length;i++){
File file = new File(directory, archives[i]);
if(file.isDirectory()){
directoryVersionInfo(file)
System.out.println(file.ge
java.util.jar.Manifest earManifest = new java.util.jar.Manifest(new
Attributes attribMap = earManifest.getMainAttribu
displayVersionInfo(attribM
}
}
}
private static void displayVersionInfo(Attribu
String version = (String) attribMap.getValue(ATTR_IM
if(version!=null){
System.out.println("\tVers
}
String vendor = (String) attribMap.getValue(ATTR_IM
if(vendor!=null){
System.out.println("\tVend
}
String title = (String) attribMap.getValue(ATTR_IM
if(title!=null){
System.out.println("\tTitl
}
if(version==null&&title==n
System.out.println("\tNo version information available.");
}
}
public void archiveVersionInfo(File archive)throws Exception{
JarFile earFile = new JarFile(archive, true, JarFile.OPEN_READ);
try{
java.util.jar.Manifest earManifest = earFile.getManifest();
if (earManifest == null) {
throw new Exception("No Manifest file in " + archive.getName());
}
Attributes attribMap = earManifest.getMainAttribu
if (attribMap == null) {
throw new Exception("Invalid Manifest file in " + archive.getName());
}
System.out.println("Hello/
displayVersionInfo(attribM
}finally{
earFile.close();
}
}
public static final void main(String [] args){
if(args.length<1){
printUsage();
return ;
}
String earFileName = args[0];
File earFile = new File(earFileName);
EarVersion versionChecker = new EarVersion();
try{
if (earFile.isDirectory()) {
versionChecker.directoryVe
}
else {
versionChecker.archiveVers
}
}catch(Exception ex){
System.err.println("Error obtaining version for Enterprise Application Repository: "+ex.toString());
ex.printStackTrace();
}
}
}
ASKER
Hi
Thanks for the post.As I mentioned in my previous post, I don't have an option to specify the file name (of the ear) anywhere in my class as my class is the part of ear itself and it needs to refer to manifest within the same ear.sing fileinputstream is a good option but how can I navigate with in the ear to find manifest file is the question..
Thanks for the post.As I mentioned in my previous post, I don't have an option to specify the file name (of the ear) anywhere in my class as my class is the part of ear itself and it needs to refer to manifest within the same ear.sing fileinputstream is a good option but how can I navigate with in the ear to find manifest file is the question..
ASKER
I'm trying to use
InputStream is = this.getClass().getResourc eAsStream ("meta-inf/manifest.mf");
to resolve the path of manifest file within ear but it returns null.Any ideas please?
InputStream is = this.getClass().getResourc
to resolve the path of manifest file within ear but it returns null.Any ideas please?
try giving
../meta-inf/manifest.mf
or
../../meta-inf/manifest.mf
../meta-inf/manifest.mf
or
../../meta-inf/manifest.mf
ASKER
Tried it doesn't work..sorry..
Basically the class in which check for manifest exists like this
x.ear--
|__meta-inf/manifest.mf
|__a.jar--
|_myclass
so myclass should access manifest file.
Basically the class in which check for manifest exists like this
x.ear--
|__meta-inf/manifest.mf
|__a.jar--
|_myclass
so myclass should access manifest file.
ASKER
Hi friends any idea please .do i need to modify the classpath or something in my manifest file under ear...
ASKER
Hello Administrator,
Please can you close this question as I have found the soultion myself now.I just had to correct the path and it works.
Thanks
Please can you close this question as I have found the soultion myself now.I just had to correct the path and it works.
Thanks
can you please explain how you have solved the problem?
ASKER
I did not manage to access application.xml/manifest file as such but we created a new file which just has the version number of build in same jar and I'm reading that.tha solves the problem but I'm still looking at how I can access manifest under the ear file.
Hope this clarifies
Hope this clarifies
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or here is an alternative approach
http://forum.java.sun.com/thread.jspa?threadID=642761&messageID=3887881