I recently wanted to get the usage of my CPU in C#. After searching the internet I found a way of doing it using the PerfomanceCounter class. I made a couple of changes and I created the following class, which seems to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;</code>

{
   public class SystemUtils
   {
      public static long getCpuUsage()
      {
        PerformanceCounter cpuCounter;
        cpuCounter = new PerformanceCounter();

        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        cpuCounter.NextValue();
        System.Threading.Thread.Sleep(1000);
        return (int) cpuCounter.NextValue();
      }
    }
}

Using the statement: SystemUtils.getCpuUsage() you should be able to get an integer number showing the CPU usage.

From time to time I develop something on Eclipse. Apart from the usual Eclipse operations (build and test) that I do with the Eclipse Ant and JUnit plugins (they ship with Eclipse for Java), I also wanted to carry out some dependency analysis on existing code. Of course JDepend is the obvious option, but I found a nice article from IBM that suggests some additional plugins that help the coder carry out day to day tasks such as Complexity monitoring and Coding standard analysis.

The following is a summary of the plugins proposed by the article. Of course all of them are opensource/free.

Tool Purpose URL for Eclipse plugin
CheckStyle Coding standard analysis http://eclipse-cs.sourceforge.net/update/
Coverlipse Test code coverage http://coverlipse.sf.net/update
CPD Copy/Paste detection http://pmd.sourceforge.net/eclipse/
JDepend Package dependency analysis http://andrei.gmxhome.de/eclipse/
Metrics Complexity monitoring http://metrics.sourceforge.net/update

The full IBM article can be found here: http://www.ibm.com/developerworks/java/library/j-ap01117/

I tried using the Apache XMLConfiguration to save the settings of an application I am developing to XML format. When I used it with Java 1.5 it all worked perfectly; however if I tried using it with Java 1.6 I was getting the following exception.

Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at org.apache.commons.configuration.XMLConfiguration.save(XMLConfiguration.java:880)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration$FileConfigurationDelegate.save(AbstractHierarchicalFileConfiguration.java:454)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:546)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:513)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:491)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:403)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.save(AbstractHierarchicalFileConfiguration.java:199)

After doing some search on google I found that this can be a problem with the xerces XML library (xercesImpl.jar). Checking the 3rd party libraries I am using in my project, I found a library that was using and distributing an earlier version of xercesImpl.jar.

I downloaded the latest version (2.91) of xercesImpl.jar from the Apache xerces project (http://xerces.apache.org/mirrors.cgi#binary) and replaced in the 3rd party library I was using the xercesImpl.jar file with the one I downloaded. Now everything seems to work fine, even in Java 1.6.  🙂

This is another script I found while cleaning up my hdd. Now, we all know that java .class files can be decompiled to (a version) of their source code. The first java decompiler out there is jad. I tried to decompile some of my class files using jad.exe, but I couldn’t find a way of decompiling multiple .class files at the same time. So instead of digging into the documentation, I decided to quickly create a script that allows me to decompile a number of .class files at the same time.

Here’s how it works! I created a VBScript that goes through a directory (and subdirectories) of .class files and creates a dos batch file. The dos batch file can now be used to run and actually decompile all the .class files!

The VBscript can be found under this link: Decompile_vbs.

You need to edit it and change the paths to the .class and source directories.

When you execute this file, it will create a dos batch file like this one: Example_decompile.batPlease note this batch file will be generated under the directory where jad.exe exists! You can now run this batch file, which will actually decompile the .class files.

It is time for spring cleaning my hard disk at home. I am sorting/deleting files that I no longer use. I will post here files that I do not need now, but might need in the future.

So… I found a small linux shell script  that individually compiles every java file in a directory and all its subdirectories. The code is:

#!/bin/bash

for d in `ls`
do
javac $d/*.java > $d/compile.txt

done

The text file can be found here:  batchcompile