Tuesday, August 30, 2005

Convert from byte[] to string in C#.NET

Recently I had to convert a byte[] array to a string in .NET. I knew that there should be some method somewhere which takes a encoding and returns the string accordingly.
I searched in the "byte[]" class, then in the "string" class, but could not find any appropriate method. Finally I found it in the System.Text.Encoding class. It has other subclasses such as
System.Text.ASCIIEncoding,
System.Text.UnicodeEncoding,
System.Text.UTF7Encoding,
System.Text.UTF8Encoding.

Code snippet:

System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString(myByteArray );

I would have preferred a String Constructor which takes a byte[] and a encoding string. Much more intuitive...

Thursday, August 25, 2005

Unix /dev/null

In quite a few unix programs ( shell scripts) I see the following command =

someprog > /dev/null 2>&1

The > /dev/null 2>&1 part means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Basically it runs the command without any output to a terminal etc.

Enabling FTP on Solaris

Recently I wanted to enable FTP on my Solaris box and had to make quite a few changes to start the FTP server. I got a summary of all the things that should be done/checked :

#vi /etc/services - uncomment
-> ftp 21/tcp
#vi /etc/inetd.conf - uncomment
-> ftp stream tcp nowail root /usr/sbin/in.ftpd in.ftpd
# vi /etc/ftpd/ftpuser - to uncomment "root" out
-> # root
#vi /etc/shells - put in all the shell as possible
-> /usr/sbin/ksh
#vi /etc/default/login - to uncomment
-> CONSOLE=/dev/console
check ftp.allow and ftp.deny files as well
#kill -HUP pid - to restart #/usr/sbin/inetd -s

If U need to enable FTP upload, then U need to make changes in one more file. (/etc/ftpd/ftpaccess) For more details see this link:
http://docs.sun.com/app/docs/doc/816-4555/6maoquinp?a=view

Wednesday, August 24, 2005

More info about VNC

I have been using VNC for years and I always assumed it to be a product of a company named RealVNC. It was only recently that I realised that VNC (Virtual Network Computing) is a open-source technology and had been first developmed on the Unix platform !!!...(I thought VNC works only on Windows)
We have VNC servers available for all platforms such as Unix X-window, Solaris CDE, Linux, Windows XP etc.

More info can be found at :
http://www.softpanorama.org/Xwindows/vnc.shtml
http://www.realvnc.com

Thursday, August 18, 2005

Debugging in Java

When I was first introduced to Java, most of the program debugging was done through console/file logging. (using the ubiquitous System.out.println).

But today, we have so many GUI debuggers at our disposal. IDEs contain their own debuggers (such as IBM VisualAge for Java, Symantec VisualCafe, and Borland JBuilder, IBM Eclipse), Stand-alone GUIs (such as Jikes, Java Platform Debugger Architecture javadt, and JProbe) ,Text-based and command-line driven (such as Sun JDB)
My favourite is the Eclipse Debugger and it serves most of my purposes.
But there are some other cool stand-alone debuggers available too. Here are links to a few of them:
http://www.bluemarsh.com/java/jswat/
http://www.debugtools.com/index.html
http://freshmeat.net/projects/jikesdebugger/

If any of the above still do not meet your needs, the Java platform has introduced the JavaDebugging APIs, which you may use to create a debugger that specifically meets yourneeds. The revised Java Debugger (JDB) serves as both a proof of concept for the Java DebuggingAPI, and as a useful debugging tool. It was rewritten to use the Java Debug Interface (JDI)and is part of the JDK.

JProbe - A cool cool tool

I had used JProbe in my earlier project for some basic memory profiling and performance bottleneck detection. JProbe suite consists of 3 important components:

JProbe Profiler: Identify Method and Line-level Performance Bottlenecks
JProbe Memory Debugger: Investigate Memory Leaks and Garbage Collection Activity
JProbe Coverage: Measure Code Coverage After Testing

Recently on visiting their site, I saw a free version of JProbe Profiler which I belive would be immensely useful to anyone doing development in Java.
http://www.quest.com/jprobe/profiler_freeware.asp

Tuesday, August 16, 2005

Java XML processing APIs

It's been some time that I have worked on any new XML processing program. My favaourite Java XML library had always been JDOM (promoted by Jason Hunter) JDOM was so intutive to use for Java developers and also made excellent use of Java collections. But today, there are quite a number of alternatives that one needs to study before deciding on which library to use.

Check out the following links to learn more:
http://www.artima.com/intv/jdomP.html
http://www-128.ibm.com/developerworks/xml/library/x-injava/index.html http://xml.apache.org/xerces-j/index.html
http://www.extreme.indiana.edu/soap/xpp/
http://xml.apache.org/crimson/index.html
http://jdom.org/index.html

Diff btw "java.exe" and "javaw.exe"

This is what the SUN documentation says:

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

So If Ur application is 100% GUI, then U can use javaw to launch it instead of java.exe

Tuesday, August 09, 2005

Java 1.4 and 1.5 compatibility problems

Recently I faced a peculiar problem when I had compiled a application using JDK 1.5 and then tried to run it inside TOMCAT running Java 1.4. And I got an runtime exception - Version mismatch -- 0.49 should be 0.48
I was bewildered, but later on searching the NET understood that the class files generated by the JDK 1.5 compiler(v 0.49) and JDK 1.4(v 0.48) compiler are not same.

My application was not using any of the new JDK 1.5 features, even then this problem was arising. I think the reason for this being the host of new features introduced in JDK1.5
For e.g. in Java 1.5, U can write:
Integer I=7; and it will compile with Java1.5 and also run inside a Java 1.5 JVM.

But if the above code is run inside a Java 1.4 JVM, then it will fail. Hence SUN has put a check to ensure that there is a runtime error shown if the class files are of different versions than that of the JVM.

Monday, August 01, 2005

Code coverage tools

Even if we are using JUnit and NUnit for executing our unit test cases, there are still some loopholes left. For e.g. how do we test that all paths in the code have been covered in the test cases? What code are the tests actually testing? What code isn't being tested? Is the test suite getting out of date?

Code coverage tools prove to be invaluable in such occasions. These tools instrument the code/binaries to discover those sections of the code that have not been tested.

Two such tools for Java and .NET are provided by Clover:
http://www.cenqua.com/clover/
http://www.cenqua.com/clover.net/

Check out the trail versions and see the benefits Urself..

Changing the bootstrap classes in javac.exe

Sometimes we may wish to change the default bootstrap classes that are loaded by the java compiler while compiling.

javac.exe provides us with a '-bootclasspath' option for that.

I found this option useful, when I was using a older version of CORBA interfaces, which were not compatible with the "org.omg.*" CORBA interfaces in JDK 1.5. So I included the old CORBA interfaces jar file before rt.jar in the bootstrap classpath and the problem was solved.

In Eclipse, the problem was solved just by changing the order of jar files in the "Build path" menu.

Specify java files names in a text file for javac.exe

Found out that there was an alternative way to pass Java source files names to the javac compiler :)

Here's what the Java docs say:
There are two ways to pass source code file names to javac:
- For a small number of source files, simply list the file names on the command line.
- For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.