Showing posts with label Utility-Java. Show all posts
Showing posts with label Utility-Java. Show all posts

Saturday, January 30, 2010

List out the system properties values - Java Source Code

import java.util.Properties;
public class ListProperties {

/** Creates a new instance of ListProperties */
public ListProperties() {
}

private void getListProperties() {
Properties props = System.getProperties();
props.list(System.out);
}

public static void main(String[] args) {
ListProperties listProps = new ListProperties();
listProps.getListProperties();
}
}

http://java.sun.com/javase/6/docs/api/java/util/Properties.html

Output
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.vm.version=1.5.0_06-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=GB
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
user.dir=E:\JavaProjects\JavaTest
java.runtime.version=1.5.0_06-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_06\jre...
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\SC0031~1\LOCALS~1\Temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\sc0031744
user.timezone=
java.security.policy=applet.policy
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.5
user.name=sc0031744
java.class.path=C:\Program Files\netbeans-5.0\platfor...
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.5.0_06\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_06
java.ext.dirs=C:\Program Files\Java\jdk1.5.0_06\jre...
sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=

Monday, July 6, 2009

Java Source Code to read from and write into a properties files

In many of applications we are developing in many of our live projects, we might find that there are different environments that we use while development phase, testing phase and production phase.

The paths, the input file and output file locations etc are varying in different environments and we generally build three different jars/wars/ears for three different environments.


However, if we use a file which contains the locations which can vary from environment to environment, we can easily build a single archive file and deploy into the environment with environment specific files.


These environment specific files can be achieved using Properties class of the java.util package and the files are called properties file.


Though I have given example of the environment, we can use heavily in each applications where there may be a susceptible change required frequently.


This will not only reduce the time to code each time, it will also reduce the cost for any changes required [if can be incorporated with properties files].

The code here does nothing but reading from properties file (SampleProperties.properties) and writing specific values into the properties file. Please see the entries and the outputs of the Properties file.

The properties file can be written in notepad and has to be saved with .properties extension.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SampleProperties {
private Properties props;
private File fName = new File("./SampleProperties.properties");
public SampleProperties() {
props = new Properties();
try {
FileInputStream fis = new FileInputStream(fName);
props.load(fis);
} catch (FileNotFoundException fNFE){
fNFE.printStackTrace();
} catch (IOException iOEx){
iOEx.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public String getPathProperties(String propName) {
String retProp=new String("");
retProp = props.getProperty(propName);
return retProp;
}

public void setPathProperties(String propName,String propValue) {
try {
props.setProperty(propName,propValue);
props.store(new FileOutputStream(fName),"Updated");
props.load(new FileInputStream(fName));
} catch (FileNotFoundException fNFE) {
fNFE.printStackTrace();
} catch (IOException iOEx) {
iOEx.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) {
SampleProperties sampleProps = new SampleProperties();
System.out.println("Value of key1 "+sampleProps.getPathProperties("key1"));
sampleProps.setPathProperties("key2","value2");
sampleProps.setPathProperties("key4","value4");
}
}
//End of class



SampleProperties.properties

Inputs:

Entries in the properties:


key1=value1
key2=value_Diff
key3=value3

Output (result of the writing into the properties file):

key4=value4
key3=value3
key2=value2
key1=value1



Hope you all will find it useful.

Friday, June 19, 2009

Java code to read the windows registry

The following code will show us the proxy settings of the internet explorer and the version of the same.
Useful APIs

Preferences
Method

import java.lang.reflect.Method;
import java.util.prefs.Preferences;


public class JavaRegistryReader {
private static final int HKEY_CURRENT_USER = 0x80000001;
private static final int KEY_QUERY_VALUE = 1;
private static final int KEY_SET_VALUE = 2;
private static final int KEY_READ = 0x20019;
public static void main(String args[]) {
final Preferences userRoot = Preferences.userRoot();
final Preferences systemRoot = Preferences.systemRoot();
final Class clz = userRoot.getClass();
try {
final Method openKey = clz.getDeclaredMethod("openKey",byte[].class, int.class, int.class);
openKey.setAccessible(true);
final Method closeKey = clz.getDeclaredMethod("closeKey",int.class);
closeKey.setAccessible(true);
final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);
winRegQueryValue.setAccessible(true);
final Method winRegEnumValue = clz.getDeclaredMethod("WindowsRegEnumValue1", int.class, int.class, int.class);
winRegEnumValue.setAccessible(true);
final Method winRegQueryInfo = clz.getDeclaredMethod("WindowsRegQueryInfoKey1", int.class);
winRegQueryInfo.setAccessible(true);
byte[] valb = null;
String vals = null;
String key = null;
Integer handle = -1;
//Query Internet Settings for Proxy
key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),toCstr("ProxyServer"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Proxy Server = " + vals);
closeKey.invoke(Preferences.userRoot(), handle);
// Query for IE version
key = "SOFTWARE\\Microsoft\\Internet Explorer";
handle = (Integer) openKey.invoke(systemRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(systemRoot, handle, toCstr("Version"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Internet Explorer Version = " + vals);
closeKey.invoke(Preferences.systemRoot(), handle);
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] toCstr(String str) {
byte[] result = new byte[str.length() + 1];
for (int i = 0; i < str.length(); i++) {
result[i] = (byte) str.charAt(i);
}
result[str.length()] = 0;
return result;
}
}


Thanks

Total Pageviews