Wednesday, September 22, 2010

Unable to start Tomcat from Eclipse plug-in

I just found a Tomcat for Eclipse plug-in from eclipsetotale, which is very convenient for me to start and stop Tomcat just right in the Eclipse. It is not a Tomcat, but it provide a shortcut to trigger Tomcat. Thus you still need to point the Tomcat directory in order for this plug-in to trigger Tomcat.

Anyhow, sometimes it might failed due to the Tomcat service has already been started before you start it (again) in the Eclipse. There will be an error showing int the stack trace:

Address already in use: JVM_Bind:8080

To verify whether the Tomcat is started, go to Tomcat directory and execute the tomcatw.exe, this will pop up Tomcat service window. The status info will be shown in the window. You may stop it if it already started. Try start again the Tomcat from Eclipse once the Tomcat service has stopped.

Monday, July 12, 2010

Be aware when extending the base class

Kindly take note when extending the base class from a sub class:
  1. Subclass static method can not override Baseclass instance method, otherwise an error static method can't hide instance method from Baseclass will be prompt.
  2. Subclass instance method can not override Baseclass static method, otherwise an error instance method cannot override the static method form Baseclass will be prompt.
Happy programming @!

Sunday, July 11, 2010

To execute a java program

I don’t know that a java program can be executed in two ways. Here we go:

First Method

Compile java source into java byte code, and then execute it right from the byte code.

javac -sourcepath src -d build\classes src\org\huahsin68\HelloWorld.java
java -cp build\classes org.huahsin68.HelloWorld

Second Method

Build a jar file from the byte code and then execute it from the jar.

jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
java -jar build\jar\HelloWorld.jar

Using Ant to execute Selenium

Prerequisite requirement:
  1. Get a copy of TestNG from this site.Please note that I am not using the Eclipse plugin. The moment of this writingis 5.12.1 
  2. Get the ant from apache’s site. 
  3. Get the JDK from Sun’s site. 
Setup:
  1. Add a new environment variable called JAVA_HOME under the System Variable group. Set the path to the JDK installationdirectory. Without this the compiler will complain that tools.jar not foundduring the Ant process. 
  2. Add the ant’s bin directory and jdk’sbin directory into Path environment variable under the System Variable group. 
The JAVA code:
  1. Create a JAVA project using Eclipse. 
  2. Add the testng-<version>.jar intobuild path and update all necessary info of the jar file (eg. SourceAttachment, JavaDoc, library location) for easier debugging purpose. 
  3. Create the source code below under thatsrc directory and name it as SimpleTest.java. The code is actually copy fromTestNG’s site. 
  4. 
    package package1;
    
    import org.testng.annotations.*;
    
    public class SimpleTest {
    
          @BeforeClass
          public void setUp() {
             // code thatwill be invoked when this test is instantiated
          }
    
          @Test(groups = { "fast" })
          public void aFastTest() {
    
             System.out.println("Fast test");
          }
    
          @Test(groups = { "slow" })
          public void aSlowTest() {
    
             System.out.println("Slow test");
          }
    }
    
  5. Create the Ant file using code below:
  6. 
    
          
                
                
          
    
          
          
          
             
             
             
             
          
    
          
             
                
             
          
    
       
    
    
  7. There are a couple of thing to take note here:
    • the Ant must be told that where the location of the testng<version>.jarfile and where are the binary file in pathelement location.
    • the destdir in javac indicate where the output of the binary class file shouldlocate after the compilation.
    • the correct location of the binary class must be set in classfileset otherwisean error complain that the build.xml not found.
  8. After this, the testng-output directory will be generated. Notice that thetestng-results.xml is created automatically for generating the testng-xsltreport purpose.

Friday, June 25, 2010

Some weird stuff about Ant

Currently I was using Ant with TestNG, please take note that when taskdef was missing in the code, the error is misleading.

<taskdef name="testng" classpathref="compilePath" classname="org.testng.TestNGAntTask" />

Below are the error generated:

Cause: The name is undefined.

Action: Check the spelling.

Action: Check that any custom tasks/types have been declared.

Action: Check that any <presetdef>/<macrodef> declarations have taken place.

Sunday, June 13, 2010

std::nothrow new

Sometime during the new operation it might fail the process due to certain reason, this isn’t a big deal because exception could be a good friend to help out. Unfortunately exception still have its own limitation where it might not able to find the matching catch() clause due to the mistake of programmer. Or it could be in other situation; CRT’s new will return a NULL pointer whereas standard C++ will throw a std::bad_alloc exception when memory allocation failed.

Instead of worrying:-
1. How does the programmer know about how the exception going to catch?
2. The programmer has to be aware that he/she is dealing with CRT’s version of new or standard version of new.

Let’s put in alternative point of view:
What if the programmer knows that the new operator is NOT going to throwing any exceptions?

Thus, std::nothrow new would be the best solution. This will guarantee that the new will only return either a NULL pointer or a valid pointer.

Here is the sample usage:
Foo *p;
p = new(std::nothrow) Foo();
if( !p ) { exit(1); }

reference
1. msdn reference
2. C++ reference guide

Saturday, June 12, 2010

How to get a file size?

stat structure is the guy to retrieve the file information, and this structure has a field called st_size where this field will retrieve the file size in bytes.

Here is the sample code:

struct stat FileStat;
if( stat( "MyFile", &FileStat ) == 0 ) { cout << FileStat.st_size << endl; }

Who is popen()?

I found a new function called popen() in stdio.h. This is so interesting that this function wasn't in the C Programming Language (ANSI) but can be found in the Standard C Library book, according to this page. This function is useful where it can direct the *NIX (e.g. ls -l) command into a FILE pointer, and read the desire output from that pointer.

For example, ls -l command was type in the console like this:
huahsin68@huahsin68-laptop:~/Development/TestLab$ ls -l
total 12
drwxr-xr-x 3 huahsin68 huahsin68 4096 2010-06-12 19:45 Debug
-rw-r--r-- 1 huahsin68 huahsin68 241 2010-03-27 15:00 MyLog.000.txt
drwxr-xr-x 4 huahsin68 huahsin68 4096 2010-03-27 12:19 src
the same thing can be done using popen(), each line of the output will be direct to FILE pointer. Use fgets() to retrieve each line of the output. Below is the sample code:
FILE *fp;
char line[130];

fp = popen("ls -l", "r");
while ( fgets( line, sizeof line, fp)) {
   printf("%s", line);
}
pclose(fp);