Monday, July 07, 2008

Eclipse : Compiling JVM 1.4 with Eclipse Europa

Eclipse Europa won't work with anything less than JVM 1.5.

But what happens if your client needs a Java 1.4 program? Do you have to download an earlier version of Eclipse?

Actually - no!

You can compile 1.4 programs inside the Europa 1.5 workspace.

Under "Windows - Preferences - Java - Installed JREs", you need to add the path to the 1.4 JVM.

Right click on the 1.4 project.

Under "Properties - Java Build Path - Libraries", ensure that the JRE System Library points to the 1.4 version.

Under "Properties - Java Compiler", click "Enable project specific settings". Set "Compiler compliance level" to 1.4.

That's it.

Enjoy!

Thursday, June 26, 2008

Java : Implementing the C# foreach

This is a common construct:



for (int i = 0; i < "object".length; i++)
{
}



It's messy and in C# you can easily do this far more elegantly with the "foreach" command.

How do you do this in Java?

Use something like e.g. for Reflection fields:



import java.lang.reflect.Field;

Field[] fields = "object".getClass().getDeclaredFields();

for (Field field : fields)
{
...
field[i].getName();
...
}



Enjoy!

Java : Iterating and printing out elements of objects

It's a common problem.

You have an object aa with variables bb, cc and dd and for logging and for debugging purposes, you want to print out the current values of all three so you have:



System.out.println ("value of bb is " + aa.bb);
System.out.println ("value of cc is " + aa.cc);
...



which gets tedious when it's a large object.

Reflection to the rescue!



import java.lang.reflect.Field;

Field[] fields = aa.getClass().getDeclaredFields();

for (int i = 0; i < fields.length; i++)
{
try
{
fields[i].setAccessible(true);
System.out.println (fields[i].getName() + " = " + fields[i].get(xicmd));
}

catch (Exception ex)
{
System.out.println ("Reflection exception = " + ex.getMessage());
}
}



Note that if the variables in the object are declared as "private", this code will throw an exception. Hence the:

setAccessible(true);

method which overrides this.

Enjoy!

Tuesday, June 24, 2008

.NET : Timing averages

Following on from my previous post about TimeSpan, I needed to do some performance testing and calculate the average time each call took.

This was for a Windows Form project.



for (int i = 0; i < iterations; i++)
{

DateTime startTime;
DateTime endTime;

int iterations;
int alteredIterations = 0;

TimeSpan total = new TimeSpan(0);
TimeSpan average = new TimeSpan(0);

alteredIterations = i + 1;

startTime = DateTime.Now;

... some method ...

endTime = DateTime.Now;

TimeSpan duration = endTime - startTime;

txtDuration.Text = duration.ToString();

total = total + duration;

average = TimeSpan.FromMilliseconds(total.TotalMilliseconds / alteredIterations);

txtAverage.Text = average.ToString();

Application.DoEvents();

}



The "Application.DoEvents();" is to allow Windows some time to update the counts on the screen.

Enjoy!

C# : Calculating timing differences

I needed to put some timings in my program. So I need to get a start time, an end time and then calculate the difference between them. TimeSpan to the rescue.



static public DateTime startTime;
static public DateTime endTime;

TimeSpan duration = new TimeSpan();

startTime = DateTime.Now;

...

endTime = DateTime.Now;

duration = endTime - startTime;

"Print" ("Duration is " + duration);



Enjoy!

Eclipse : Starting with a different VM

For various reasons, a PC I use has Java 1.4 installed and the latest Eclipse Europa requires 1.5. I am unable to update the classpath, environment etc. so although 1.5 was installed, Eclipse would not find it.

The solution was to use the command line option:

C:\eclipse\eclipse.exe -vm "C:\Program Files\Java\jdk1.5.0_15\jre\bin\java.exe"

This forces Eclipse to load with the 1.5 VM.

I placed the command above in a shortcut so it's easily accessible from the desktop.

Enjoy!

Thursday, June 05, 2008

XP : Find that missing USB drive

I have a USB drive that simply will not load on one of my PC's.

The PC simply wouldn't recognise the drive no matter what I did.

So Mr. Google to the rescue.

I found the answer here.

The problem was "More than likely the cause is that Windows renamed the drive to a letter that is already in use. This will happen if you have several card readers, thumb drives or external hard drives attached. It will also happen if you are on a network and have mapped drives".

After following the advice, all was well.

Enjoy!

Friday, May 30, 2008

Visual Studio : Snippets

This is for VS 2005.

Type a common keyword e.g.

for

Then hit the Tab key twice.

VS generates:



for (int i = 0; i < length; i++)
{

}



There's a whole lot of others listed here:

Enjoy!

.NET : Carraige return in textbox

I can never remember whether the CR symbol is "/n" or "\n".

Then I discovered that you could say:

txtResult.Text = txtResult.Text + "blah" + Environment.NewLine;

Much easier to remember.

Enjoy!

.Net : Active Directory error 0x80005000

Busy with an Active Directory project in Visual Studio 2005 when I got this error:

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)

Bit of research on Mr. Google and I discovered that my url was something like:

string connectionString = "ldap://blah.local/CN=xxxx Users

when I should have had:

string connectionString = "LDAP://blah.local/CN=xxxx Users

i.e. "LDAP" needs to be in caps.

Enjoy!

Tuesday, May 27, 2008

C# : Simple logging framework

Java of course has Log4J and C# has Log4NET but if you are after a simple, cut down, no bloat framework have a look at:

a .NET logging framework

As the author states:


Why should I use this framework?

How popular is it?
Real companies building real applications use this framework. Most people who evaluate this framework choose to use it.

There is another popular framework with a download of several megabytes(!!), while yours is only about 100K (the DLL itself is only 40K). What gives?
With this framework you get a framework. Out of the box you'll be able to accomplish 99% of your logging tasks. Yet it isn't bloated with unnecessary complexities. For the 1% of other logging tasks you might have, you can easily extend it.


Having used it, yes it works, yes it's simple, yes it did what I wanted!

Enjoy!

Wednesday, May 21, 2008

.NET : Debugging a dll in Visual Studio 2005

So I wrote a .dll in C# (a class library project) which is called by a desktop application.

I set the Visual Studio debugger on the dll code, started the application and ...

nothing happened.

So off to Mr. Google and the answer is that you:

* Right click on the dll project in Solution Explorer.
* Click "Properties"
* Click "Debug"
* Click "Start External Program" and then browse to the application
* Save

Now when you hit F5 (Start Debugging), Visual Studio will start the application and the dll code will breakpoint.

Enjoy!