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!

Thursday, March 20, 2008

Java : Parsing XML

So there I was needing to parse some XML in a Java program and not wanting to refresh my memory of the Java classes to do this.

Had a look around on the Internet and found JFig

This allows you to set up XML files and then read the parameters using a construct like:

JFig.getInstance().getValue(sectionName, key)

So for XML like:


#section name="Section1"*
#entry key="key1" value="val1" /*
#/section*

use:

JFig.getInstance().getValue("Section1", "key1")

Neat - and saves me doing the parsing.

But then I discovered some other neat features e.g. you have have a hierarchy of XML files and some "inherit" others.

Also, you have have substitution values e.g.

#entry key="key2" value="[Section1]{key1}" /*


which allows you to define some "constants" at the beginning of the XML file and then JFig will replicate them through for you.

... where # = "<" and * = ">"

Very neat!

Useful article here. Another solution is to use Jakarta Commons Configuration but JFig is much simpler.

Enjoy!

Monday, October 29, 2007

Java : Creating a date

The old format was

Date date = new Date ("2007/10/29") but that has long been deprecated.

Use this:



DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date date = dfm.parse("1990-01-01");
}

catch (Exception e)
{
System.out.println("Date exception is: " + e.getMessage());
e.printStackTrace();
}



If you wanted to add the time:



DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try
{
Date date = dfm.parse("1990-01-01 12:40:00");
}

catch (Exception e)
{
System.out.println("Date exception is: " + e.getMessage());
e.printStackTrace();
}



Enjoy!

Thursday, September 06, 2007

Java : Disassembler

The standard javap will do this for you.

javap -help shows all the commands.

For class x, javap - c x (Note: Don't type x.class - just type x) will decompile the class for you and display methods like:

public int getUserType();
public void setUserType(int);

Enjoy!

Wednesday, August 15, 2007

Eclipse : Matching brackets

Clicking to the right of a bracket shows the matching one in a "box".

To toggle between the two, use Cntl / Shift / P.

Enjoy!

Eclipse : Running command line to EJB inside Eclipse

I'm running Eclipse Europa (the J2EE package).

See the previous post about running a command line Java program inside a DOS box and the million paths etc. you have to set up.

It's a lot easier to run it inside Eclipse by right clicking the application in the Package Explorer and selecting "Run As" / "Java Application".

(Note : JBoss needs to be started first by selecting the Server view inside the J2EE perspective , right click, "Start".)

You get this error in the console:

Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial


As per the previous post, the solution then was to add a path to the jndi.properties file:

set CLASSPATH=C:\JBoss-4.0.5.GA\server\default\conf;%CLASSPATH%

In Eclipse, however, go "Project Properties" / "Run/Debug Settings" / select the launch configuration / "Edit" / click on the "Classpath" tab / "Advanced: / "Add External Folder" and then browse to the jndi.properties folder as above and "OK" out.

That's it - Eclipse takes care of all the other stuff!

Enjoy!

Tuesday, August 14, 2007

Java : Find which class is in which jar file

It's a real pain!

You keep getting ClassNotDefined and you have hundreds of Jar files and it takes ages to manually search them all.

The solution is to use Jarscan.

There's also a web version on the same page.

So e.g.

java -jar jarscan.jar -dir C:\JBoss-4.0.5.GA -class SafeClone

returns (for JBoss):

searching these jarfiles now ....

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jboss-serialization.jar
Library Path: C:\JBoss-4.0.5.GA\client\jboss-serialization.jar
===============================================

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jbossall-client.jar
Library Path: C:\JBoss-4.0.5.GA\client\jbossall-client.jar
===============================================

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jboss-serialization.jar
Library Path: C:\JBoss-4.0.5.GA\server\default\lib\jboss-serialization.jar
===============================================

Search took: 10360 milliseconds.


Neat!

Enjoy!

EJB : Running a command line client to a JBoss EJB

The EJB example running on JBoss is taken from the WTP (Web Tools Project) tutorials here plus XDoclet - all inside Eclipse.

Call the package ejbs.

We have an EJBTestBean:


/**
*
*
* @ejb.interface-method view-type="remote"
*
* @generated
*
* //TODO: Must provide implementation for bean method stub
*/
public String greet(String param) {
return "Hi dude " + param;
}


XDoclet creates all the Home and Util stubs.

Now we want to test this from a Java command line client.

So we create a Java class with a main() method (called CLTestClient):


try
{
EJBTestHome home = ejbs.EJBTestUtil.getHome();
EJBTest service = home.create();
String result = service.greet("Tom");
System.out.println();
System.out.println("Result is " + result);
}

catch (Exception e)
{
System.out.println();
System.out.println("Exception " + e.getMessage());
}


and we want to test from the command line:

java CLTestClient

and we get ClassNotDefined all over the place.

So we add the EJB files to the CLASSPATH:

set CLASSPATH=C:\JBoss-4.0.5.GA\
server\default\lib\jboss-j2ee.jar;C:\your path\EJBTestClient\build\classes;C:\your path\EJBTestClient\build\classes\ejbs;%CLASSPATH%

Then we get this error:

Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial


The solution is to add a path to the jndi.properties file:

set CLASSPATH=C:\JBoss-4.0.5.GA\server\default\conf;%CLASSPATH%

More ClassNotDefined errors:

set CLASSPATH=C:\JBoss-4.0.5.GA\client\jbossall-client.jar;C:\JBoss-4.0.5.GA\lib\jboss-common.jar;C:\JBoss-4.0.5.GA\server\default\lib\jboss.jar;C:\JBoss-4.0.5.GA\client\jnp-client.jar;C:\JBoss-4.0.5.GA\client\jboss-client.jar;%CLASSPATH%

and finally:

java CLTestClient

returns:

Result is Hi dude Tom

By the way, if you wanted to do this from a browser inside a jsp page:


LT%@page import="ejbs.EJBTestUtil"%GT
LThtmlGT
LTheadGT
LTtitleGTEJBWebTestLT/titleGT
LT/headGT
LTbodyGT
LT%
ejbs.EJBTest tb = null;

try
{
ejbs.EJBTestHome home = EJBTestUtil.getHome();
tb = home.create();
}

catch(Exception exception)
{

}

%GT

LTbGTLT%= tb.greet("Tom") %GTLT/bGT

LT/bodyGT
LT/htmlGT




Where LT = "less than" and GT = "greater than".

Enjoy!