w3hJava

What, Why, When and How of Java, JavaFX and related technologies

Archive for the ‘Java’


Published May 13th, 2008

Listing Process from Java Code

A month back, I wanted to find out all processes running on my machine from java code for some stupid purpose. I tried to write some code and was pretty successful. Java can’t play with system process and hence invoking a runtime is only solution to get all process and here it is:

import java.io.*;
class ListProcess {
    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        String cmds[] = {”cmd”, “/c”, “tasklist”};
        Process proc = runtime.exec(cmds);
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

Code is written exclusively for Windows Machine :). And one line change in this code will list you only java running process.

String cmds[] = {”cmd”, “/c”, “jps”}; this is nothing but running jps.exe file in bin (jdk6 onwards). Its not all done. Writing Runtime code is not the real solution as there is little of platform dependencies. So, I have decided to write the code for getting List of Java Process. Again, I have checked by OpenJDK code for jps(search on jps.java file :) ) and I got some hint how to do it and here it goes:


import java.util.*;
import sun.jvmstat.monitor.*;
public class ListJavaProcess {
    public static void main(String[] args) throws Exception {
        /* Checking for local Host, one can do for remote machine as well */
        MonitoredHost local = MonitoredHost.getMonitoredHost(”localhost”);
        /* Take all active VM’s on Host, LocalHost here */
        Set vmlist = new HashSet(local.activeVms());
        for (Object id : vmlist) {
            /* 1234 - Specifies the Java Virtual Machine identified by lvmid 1234 on an unnamed host.
            This string is transformed into the absolute form //1234, which must be resolved against
            a HostIdentifier. */
            MonitoredVm vm = local.getMonitoredVm(new VmIdentifier(”//” + id));
            /* take care of class file and jar file both */
            String processname = MonitoredVmUtil.mainClass(vm, true);
            System.out.println(id + ” ——> ” + processname);
        }
    }
}

I have written good amount of comment as it is all together a sun import rather than java or javax import. This import resides in tools.jar, so even running simple javac and java will not work. So, running the program will go here:

E:\Program Files\Java\jdk1.6.0_10\bin>javac -classpath "E:\Program Files\Java\jdk1.6.0_10\libtools.jar" ListJavaProcess.java
E:\Program Files\Java\jdk1.6.0_10\bin>java -classpath .;"E:\Program FilesJava\jdk1.6.0_10lib\tools.jar" ListJavaProcess
3700 ------> ListJavaProcess

Right now only one java process is running. Now in the second code, you can play with some of the java process, but with native process in the above code you can’t do anything except watching it :)

No idea how to do this in JDK 1.5 or backwards(runtime is off course one option).

Published May 11th, 2008

Number of JREs on my Windows machine?

Sometime back, we were discussing something about JRE and one of my friends Lawrence asked me “How to find the number of JRE  installed on system with the help of Java Code ?

After giving much thought to it, I don’t think Java has any such API which will tell how many JRE are installed on the System and what are they ? One of my another friend Vikram told me that JRE installation records information in Registry. With this knowledge at the hindsight, I tried to write this piece of code.

It will only run on Windows :) because I have used Runtime class. I would love to know how the same could be achieved in Unix Systems. This code is not doing anything great, just queries the registry and reflects the answer on the console.

import java.io.*;
class NoofJRE {
    static String REG_PATH = "reg query " +
                          "\"HKLM\Software\JavaSoft\Java Runtime Environment";
       public static void  getJREInfo() {
            try {
            Process process = Runtime.getRuntime().exec(REG_PATH);
                  InputStream inputstream = process.getInputStream();
                  InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                  BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
                  String line;
                  while ((line = bufferedreader.readLine()) != null) {
                           System.out.println(line);
                  }
          }
            catch (Exception e) {
                  System.out.println("I am in Exception");
        }
      }
      public static void main(String s[]) {
            getJREInfo();
        }
}

And here is my output:

E:\Program Files\Java\jdk1.6.0\bin>java NoofJRE
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
CurrentVersion REG_SZ 1.6
BrowserJavaVersion REG_SZ 1.6.0_01
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.4.2_17
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6.0
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6.0_01

Which sounds true in my case. It is clearly telling me that I have JRE 1.4.2_17, 1.6.0 and 1.6.0_01. I have old bad habit of not uninstalling JRE’s :).

Please do let me know if there is any other way to know how many and which JRE is/are installed in my system.

To know where the JRE is installed, one has to do a simple query of JAVA_HOME in the registry.

Published May 9th, 2008

Sorting different Language(Locale) in Java

I have given a little thought what should be my first post. Finally decided not to write anything complex. I am just posting about simple sorting fundas in Java. Sorting is always a tricky game in any programming language and it is responsible for 50-60 percent of the total CPU time for any application. We all have our native language like Hindi, Chinese, Japanese, French and so many. Most of the time world deals with sorting of Alphabets or English words but give a eye on other languages which is growing fast and off course today we are talking about internationalization.

I am showing you a typical sorting of French word and the blunder associated with it. These are some of the common French words:

String[] names = {”fácil”, “facil”, “fast”,”Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};

and here is the typical sorting code:

String[] names = {”fácil”, “facil”, “fast”,”Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};
        List list = Arrays.asList(names);
        Collections.sort(list);
        Iterator itr = list.iterator();
        while(itr.hasNext()) {
            System.out.print(itr.next()+ ”   “);
        }

And the result:
Où facil fast fácil ou specific spécifique êtes-vous

which is completely wrong according to French Rules. Because sorting is simply going via UNICODE rules not by French rules.

Now remedy: Java gives us a class called Collator class in java.text Package which takes care of locale while sorting. Here goes the code:

import java.text.*;
import java.util.*;
class CollatorTest {

    public static void main(String[] args) {
        String[] names = {”fácil”, “facil”, “fast”, “Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};
        List list = Arrays.asList(names);
        Collections.sort(list);
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
        Locale[] loc = Collator.getAvailableLocales();
        Collator myCollator = Collator.getInstance(new Locale(”fr”));
        myCollator.setStrength(Collator.PRIMARY);
        Collections.sort(list, myCollator);
        itr = list.iterator();
        System.out.println(”");
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
        myCollator.setStrength(Collator.TERTIARY);
        Collections.sort(list, myCollator);
        itr = list.iterator();
        System.out.println(”");
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
    }
}

And here is the result:

Où     facil     fast     fácil     ou     specific     spécifique     êtes-vous
êtes-vous     facil     fácil     fast     Où     ou     specific     spécifique
êtes-vous     facil     fácil     fast     ou     Où     specific     spécifique

which is perfectly valid. There are lot of option in Collator class which we will discuss sometimes later here only.