Get JBoss Version using Java Code

The following are the things we will be looking into now.

a) How to get the JBOSS version from JBOSS zip file using java code?
b) How to get the JBoss version from JBoss folder using java code?
c) How to get the manifest details of a jar file that is present inside a Zip file?


The actual version of JBoss can be determined by using the run.jar file present inside the bin folder of JBoss. It is not that difficult to get the manifest details of any jar file present either inside a folder or in a zip file[If you think it is difficult, don't worry,very soon we will make it easy for you]

We are here trying to get the manifest details of the run.jar file, which answers all the questions we have posted above.

The first method "getJbossVersion(String sourcefile)" in the code snippet below not only tells us how to get the Version of JBoss, but also tells us how to get the contents of MANIFEST.MF file that is present inside a jar file which in turn is again present inside a zip file.

i.e We will read the contents of MANIFEST.MF file present inside

jboss-4.2.3.GA.zip/jboss-4.2.3.GA/bin/run.jar/META-INF folder

The main difference between the first and the second method is that the second method gets the JBoss Version from JBoss folder, where as the first one gets it from the JBoss Zip file.






import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class GetJbossVersionDetails {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  GetJbossVersionDetails details = new GetJbossVersionDetails();
  details.getJbossVersion("E:/servers/jboss-4.2.3.GA.zip");
  details.printJBossVersion("E:/servers/jboss-4.0.5.GA");
 }
 
 public void getJbossVersion(String sourcefile)  {
  try{
   ZipFile zf = new ZipFile(sourcefile);
   ZipInputStream in = new ZipInputStream(new FileInputStream(sourcefile));
   for(Enumeration em = zf.entries(); em.hasMoreElements();){
    ZipEntry ze = in.getNextEntry();
    if(ze.getName()!=null && ze.getName().endsWith("run.jar")) {
     JarInputStream jarIS = new JarInputStream(zf.getInputStream(ze));
     Manifest manifest = jarIS.getManifest();
         Attributes attrs = manifest.getMainAttributes();
     System.out.println("Jboss Version [From Zip file] : "+
                 attrs.getValue(Attributes.Name.SPECIFICATION_VERSION));
     break;
    }
   }
   in.close();
  } catch(Exception ex){
   ex.printStackTrace();
  }
 }
 
 public void printJBossVersion(String jBossHome) {
  try {
   String pathOfRunJar = jBossHome+"/bin/run.jar";
   URL url = new URL("file", null, pathOfRunJar);
   URLClassLoader urlClassLoader = new URLClassLoader(new URL [] { url });
   Class mainClass = urlClassLoader.loadClass("org.jboss.Main"); 
   Package jbossPackage = mainClass.getPackage();
   System.out.println("Jboss Version [From folder] : "+
                        jbossPackage.getSpecificationVersion());
  }
  catch(Exception ex) {
   ex.printStackTrace();
  }
 }

}




Why do we need to know the JBoss version if full path of JBoss server is already given?

This is just a stand alone program to demonstrate the process of getting the manifest details from a jar file which can be present either inside a folder or in a zip file.Other than a stand alone program, if we use Swings or other applications where we need to browse and validate JBoss server , then definitely the above code helps.

Comments

Popular posts from this blog

Creating Custom Code Panels using InstallAnywhere

Server Migration from JBoss-4.0.5 to JBoss-4.2.3.GA