Pages

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

Sunday, September 26, 2010

How To Do Sorting In Languages Other Than English 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:
Actul : Où facil fast fácil ou specific spécifique êtes-vous
Expected : êtes-vous facil fácil fast Où ou specific spécifique

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

How To Find The Number Of JREs Running On Windows Machine?

It will only run on Windows :) because we are using Runtime class. 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 HKLMSoftwareJavaSoftJava 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();
}
}


Output

! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment CurrentVersion REG_SZ 1.6
BrowserJavaVersion REG_SZ 1.6.0_01
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6.0
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6.0_01

Saturday, September 25, 2010

How To List All The Processes Running On A Machine From Java Code?

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);
}
} }
Mind you, the 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).

Output

Image Name                     PID Session Name        Session#    Mem Usage
======= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      4,024 K
smss.exe                       264 Services                   0      1,080 K
csrss.exe                      356 Services                   0      4,968 K
wininit.exe                    420 Services                   0      4,380 K
csrss.exe                      436 Console                    1     27,912 K
services.exe                   472 Services                   0      9,168 K
lsass.exe                      500 Services                   0     10,604 K
lsm.exe                        516 Services                   0      4,316 K
winlogon.exe                   524 Console                    1      7,044 K
svchost.exe                    640 Services                   0      9,124 K
svchost.exe                    748 Services                   0      9,168 K
svchost.exe                    836 Services                   0     19,404 K
svchost.exe                    876 Services                   0    107,948 K
svchost.exe                    908 Services                   0     37,764 K
stacsv64.exe                   968 Services                   0      9,072 K
svchost.exe                    596 Services                   0     11,328 K
DockLogin.exe                  960 Services                   0      3,940 K
svchost.exe                   1144 Services                   0     16,072 K
WLTRYSVC.EXE                  1224 Services                   0      3,152 K
wlanext.exe                   1232 Services                   0      4,932 K
conhost.exe                   1240 Services                   0      2,508 K
BCMWLTRY.EXE                  1308 Services                   0     31,472 K
spoolsv.exe                   1380 Services                   0     11,184 K
svchost.exe                   1408 Services                   0     14,008 K
AESTSr64.exe                  1496 Services                   0      2,600 K
svchost.exe                   1572 Services                   0     13,020 K
SeaPort.exe                   1724 Services                   0      9,948 K
SfCtlCom.exe                  1820 Services                   0      6,448 K
SftService.exe                1864 Services                   0      6,920 K
TeamViewer_Service.exe        2036 Services                   0      3,716 K
taskhost.exe                  2076 Console                    1      9,856 K
dwm.exe                       2280 Console                    1     38,260 K
explorer.exe                  2432 Console                    1     58,808 K
TmProxy.exe                   2908 Services                   0     16,824 K
DSUpd.exe                     2772 Console                    1     17,184 K
conhost.exe                   1904 Console                    1      4,536 K
WmiPrvSE.exe                  2788 Services                   0      6,888 K
STService.exe                 2728 Console                    1     12,372 K
Toaster.exe                   3268 Console                    1     32,212 K
Apoint.exe                    3348 Console                    1      8,780 K
igfxtray.exe                  3364 Console                    1      6,308 K
hkcmd.exe                     3372 Console                    1      6,176 K
igfxpers.exe                  3420 Console                    1      6,148 K
igfxsrvc.exe                  3484 Console                    1      6,176 K
ApMsgFwd.exe                  3572 Console                    1      4,424 K
WLTRAY.EXE                    3592 Console                    1     30,680 K
ApntEx.exe                    3608 Console                    1      5,104 K
hidfind.exe                   3616 Console                    1      4,292 K
conhost.exe                   3632 Console                    1      4,548 K
sttray64.exe                  3660 Console                    1     16,472 K
quickset.exe                  3700 Console                    1     12,428 K
UfSeAgnt.exe                  3804 Console                    1      1,404 K
googletalk.exe                3172 Console                    1     12,704 K
SearchIndexer.exe             3460 Services                   0     23,432 K
veohwebplayer.exe             3096 Console                    1     33,712 K
sidebar.exe                   3532 Console                    1     20,732 K
jusched.exe                   2856 Console                    1      4,176 K
DataSafeOnline.exe            3756 Console                    1     35,072 K
DellDock.exe                  3836 Console                    1     29,188 K
PDVDDXSrv.exe                  940 Console                    1      8,500 K
WebcamDell2.exe                832 Console                    1      7,520 K
RoxioBurnLauncher.exe         3880 Console                    1      8,376 K
sprtcmd.exe                   3900 Console                    1        548 K
DivXUpdate.exe                1672 Console                    1     15,776 K
chrome.exe                    4692 Console                    1     75,592 K
chrome.exe                    4808 Console                    1     15,032 K
chrome.exe                    4892 Console                    1     49,148 K
chrome.exe                    4164 Console                    1      9,120 K
chrome.exe                     764 Console                    1     28,016 K
chrome.exe                     196 Console                    1      8,948 K
googletalkplugin.exe          4416 Console                    1     13,676 K
sprtsvc.exe                   2704 Services                   0      1,400 K
wuauclt.exe                   4816 Console                    1      6,512 K
TMBMSRV.exe                   4220 Services                   0      9,108 K
audiodg.exe                   4016 Services                   0     19,620 K
wmplayer.exe                  4216 Console                    1     64,424 K
eclipse.exe                    760 Console                    1      4,164 K
javaw.exe                     3736 Console                    1    150,400 K
chrome.exe                    3128 Console                    1     29,088 K
SearchProtocolHost.exe        2668 Services                   0      4,956 K
chrome.exe                    1368 Console                    1     45,940 K
javaw.exe                     4496 Console                    1     11,000 K
cmd.exe                       2736 Console                    1      2,476 K
conhost.exe                   3148 Console                    1      3,292 K
tasklist.exe                  5032 Console                    1      5,272 K
WmiPrvSE.exe                  3236 Services                   0      5,956 K