Wednesday, October 30, 2013

Reading windows registry using Runtime.getRuntime().exec(..) command

In my previous post @ http://exploreeclipse.blogspot.in/2013/10/reading-windows-registry-values-using.html , I was talked about reading windows registry using JNA library. But Java also provides support to read the windows registry through Java Runtime command execution.

In this article will focus on how to read win registry using Runtime.getRuntime().exec(..) command.

Basically you need these 3 parameters to hit the registry.
1. What is the Location ?
2. What is the registry key type ?
3. What is the key you want to read ?

Example:
I want to read the following parameters.
Location = "HKEY_LOCAL_MACHINE\\Software\\Wow6432Node\\Android SDK Tools";
Key = "Path";
Registry Type = "REG_SZ"

Above parameters basically tells, what is the Andriod SDK Tools 'Path' directory ? That is what i want to figure it out.

Below piece of code, which will help you to get the Andriod sdk tools path directory.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WindowsRegistryReader {

public static void main(String[] args) {

String location = "HKEY_LOCAL_MACHINE\\Software\\Wow6432Node\\Android SDK Tools";
String key = "Path";
String regType = "REG_SZ";

try {
String value = read(location, key, regType);
System.out.println("==========Output=================");
System.out.println("location: " + location);
System.out.println("Type: " + regType);
System.out.println("key: " + key + "  value: " + value);
System.out.println("=================================");

} catch (IOException e) {
e.printStackTrace();
}
}

public static String read(String location, String name, String regType) throws IOException {

Process p = Runtime.getRuntime().exec("reg QUERY \"" + location + "\" /v \"" + name + "\"");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String out = "";
while ((out = in.readLine()) != null) {
if (out.matches("(.*)\\s+REG_(.*)")) {
break;
}
}
in.close();
if (out != null) {
int indexOf = out.indexOf(regType);
if (indexOf != -1) {
String value = out.substring(indexOf + regType.length()).trim();
return value;
}
}
return null;
}
}


Output:
==========Output=================
location: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Android SDK Tools
Type: REG_SZ
key: Path  value: C:\Program Files (x86)\Android\android-sdk

=================================

Various Windows registry type values can be found @ http://msdn.microsoft.com/en-us/library/windows/desktop/ms724884(v=vs.85).aspx

Monday, October 28, 2013

Programmatically gettting Eclipse installation path directory

You might have some properties file or some other file at your eclipse home directory and you want to read that, for this you required a eclipse home directory path.

Below is the piece of code which helps you to get that.

public class EclipseLocation {

@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz, String filter) {
BundleContext context = MyPlugin.getDefault().getBundle().getBundleContext();
ServiceTracker tracker = null;
try {
tracker = new ServiceTracker(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + clazz.getName() //$NON-NLS-1$ //$NON-NLS-2$
+ ")" + filter + ")"), null); //$NON-NLS-1$ //$NON-NLS-2$
tracker.open();
return (T) tracker.getService();
} catch (InvalidSyntaxException e) {
return null;
} finally {
if (tracker != null)
tracker.close();
}
}
}

Invocation:
EclipseLocation.getService(Location.class, Location.INSTALL_FILTER)


Output:
C:/myInstallations/Eclipse



You can also achieve the same thing using the system property 'eclipse.launcher'.

//this returns C:/myInstallations/Eclipse/eclipse.exe
String launcherLocation = System.getProperty("eclipse.launcher");  //

if (launcherLocation != null ) {
File launcherLocationFile = new File(launcherLocation);

//get the parent of eclipse.exe file->C:/myInstallations/Eclipse
String eclipseDir = launcherLocationFile.getParent();

//your piece of logical code resides here
                   }



Hope this helps.

Thursday, October 24, 2013

Reading Windows registry values using java


This can be done using JNA(Java Native Access) library.

Use this link to download @ https://github.com/twall/jna#readme

Required jars: 

Piece of code to test:

Here I am trying to read Android installation path from windows registry.


public static void main(String[] args) {

try {
Map<String, Object> values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Android SDK Tools");
for (String value : values.keySet()) {
System.out.println(value + "   " + values.get(value));

}
} catch (Exception e) {
e.printStackTrace();
}

}

public static void main(String[] args) {

try {
Map<String, Object> values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Android SDK Tools");
for (String value : values.keySet()) {
System.out.println(value + "   " + values.get(value));

}
} catch (Exception e) {
e.printStackTrace();
}

}


Output:
Path   D:\Work\Android\adt-bundle-windows-x86-20130522\adt-bundle-windows-x86-20130522

Friday, October 18, 2013

Java VM PermGen space

The Java VM memory is split up to 3 memory spaces:

  • The Java Heap. Applicable for all JVM vendors, usually split between YoungGen (nursery) & OldGen (tenured) spaces.
  • The PermGen (permanent generation). Applicable to the Sun HotSpot VM only (PermGen space will be removed in future Java 7 or Java 8 updates)
  • The Native Heap (C-Heap). Applicable for all JVM vendors.

The Java HotSpot VM permanent generation space is the JVM storage used mainly to store your Java Class objects.  The Java Heap is the primary storage that is storing the actual short and long term instances of your PermGen Class objects.

The PermGen space is fairly static by nature unless using third party tool and/or Java Reflection API which relies heavily on dynamic class loading.
It is important to note that this memory storage is applicable only for a Java HotSpot VM; other JVM vendors such as IBM and Oracle JRockit do not have such fixed and configurable PermGen storage and are using other techniques to manage the non Java Heap memory (native memory).

Below diagram showcases various heap memories and how we can configure them using vm arguments.




Understanding how the JVM uses native memory on Windows

JVM: Permgen is a non-heap


In Oracle's JVM, the permanent generation is not part of the heap. It's a separate space for class definitions and related data. In Java 6 and earlier, interned strings were also stored in the permanent generation. In Java 7, interned strings are stored in the main object heap.


From above link, we can understand these terms:

Eden Space (heap): The pool from which memory is initially allocated for most objects.

Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.

Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.

Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.

Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.



Finding java process id's

Go to command prompt and type jps, this will provide the currently running java process id's in your system.