Header Ads

How To Check System Configuration Before Proceeding With Our Code In Java

The objective of this post is to check system configuration for running java codes. when it is exactly required??
Well lets say I have developed a program in Java-1.6, and used some code which got enhanced in that particular version.
like-
  • Native platform Security (GSS/Kerberos) integration.
  • Java Authentication and Authorization Service (JAAS) login 
  • module that employs LDAP authentication
  • New Smart Card I/O API
  • Native security services technical article
Lets assume that this program will run on java -1.5 . Definitely the compatibility will not be there.

As per stack over flow...
The compiler is not backwards compatible because bytecode generated with Java6 JDK won't run in Java 1.5 jvm (unless compiled with the -target 1.5 flag). But the JVM is backwards compatible, as it can run older bytecodes.

So I guess they chose to consider the compatibility from the point of view of javac (as it is the part specific to the JDK), meaning that the bytecode generated can be run in future releases of the jvm (that is more related to the JRE, but also bundled in the JDK).

In brief, we can say:

    JDK's are (usually) forward compatible.
    JRE's are (usually) backward compatible.
Let us also assume , my program needs the below written...
  • Java version-java 1.7
  • OS-Windows XP
  •  and I have some requirement where the .class file should be in C drive.
if all of the above are matching then our program is good to go else we will simply say with the  configuration, this program will not run...

Lets see how:
In my previous post here..http://www.mydigitalvoice.in/2013/05/how-to-get-system-properties-in-java.html
I have shown how to get system properties.
From the list of properties we are only going to choose
  • java version
  • OS 
  • class. path



import java.util.Properties;


public class systemTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  boolean b=false;
  Properties p=System.getProperties();
  p.list(System.out);
//get all the list of properties.
                String path=p.getProperty("java.class.path");
  String os=p.getProperty("os.name");
  String javaVersion=p.getProperty("java.specification.version");
  System.out.println(path);
  System.out.println(os);
  System.out.println(javaVersion);
  if((path.indexOf("C:\\")>=0||path.equals(".")) && os.equals("Windows XP") && javaVersion.equals("1.7"))
    b=true;
//checks the path contains C drive,also checks for OS or java version
                  if(b)
    System.out.print("We can proceed");
    else
    System.out.print("We can not proceed");
      }
       
}

output:
. (dot means present working directory.)
Windows XP
1.7
We can proceed

Now if the java version is 1.4 (running)in the client machine. It should say "We can not proceed"

By this way we can achieve the precondition cases for running our tests.

Powered by Blogger.