Wednesday, August 18, 2010

Recursively listing all the files in directory java

/**
* Get all the files in a directory. This recursively gets the files from
* sub directories as well
*
* @param parentDir
* @return
* @throws FileNotFoundException
*/
private static List getFiles(File parentDir)
throws FileNotFoundException {
List result = new ArrayList();
File[] filesAndDirs = parentDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
result.add(file);
if (file.isFile() == false) {
List deeperList = getFiles(file);
result.addAll(deeperList);
}
}
return result;
}

Using Apache Lucene Example.

Recently I downloaded apache lucene project and struggled to run their examples. Probably this blog my help you to save 3hr of effort.

Aim: if you want to search a word inside the files. Please follow below steps

1. Index the directory first
2. Create a query file
3. Search the directory

• Indexing the dir ‘workspace’
o java -cp lucene-core-3.0.2.jar;lucene-demos-3.0.2.jar org.apache.lucene.demo.IndexFiles C:\workspace
• Create a query file “queries.txt” and save all the keys you want to search. For Example ‘hello’
o Search for ‘hello’ word in all the files under dir ‘workspace’
• Search using lucene
o java -cp lucene-core-3.0.2.jar;lucene-demos-3.0.2.jar org.apache.lucene.demo.SearchFiles -index C:\index C:\ queries.txt

queries.txt file should have string ‘hello’. If you want to search multiple words have multiple words in the file separated by \n

Sunday, July 11, 2010

Log statements in JSP

Hi,
Sometimes it is helpful have log statements in JSPS. Unfortunately if we have lot of business logic embedded in JSP’s. It is better to have log statements.

To add log statement in JSP
• Import logger
o <%@page import="org.apache.log4j.Logger"%>
• Provide the logger name, FQN name of the JSP
o private static final Logger logger = Logger.getLogger("portlet.ext.watching_portlet.view.jsp");
• Add the log statements as mentioned in the guide lines
o logger.warn("Unable to find thread associated to subscription");

Best practice:

Please don’t rely on toString method to get the exception stack trace.
Poor: logger.warn("Unable to find thread associated to subscription"+e);
Good: logger.warn("Unable to find thread associated to subscription",e);//exception stack trace will be logged, even if api doesn’t override the toString method

Changing JSP Editor font Eclipse:

By default JSP editor uses text editor for font settings . Change the setting by

Window->preferences->general-appearance->colors and fonts-Basic->Text font

Tuesday, May 25, 2010

Getting the method name in the method annotated with “@BeforeMethod”

Hi All,

Below is an example that helps to get the test method name, before executing

A quick example for the people who are very lazy like me 


@BeforeMethod
@Parameters("selenium.screenShotFolder")
protected void captureScreenShotBefore(String folder,Method method) {
String data = getSelenium().captureScreenshotToString();
String fileName=method.getName()+"_before.png";
Reporter.log(""+fileName+"");
FileUtil.writeToFile(data,folder+fileName);
}


Thanks & Regards
Balaji

Wednesday, April 21, 2010

java.io.IOException: Cannot run program CreateProcess error=87, The parameter is incorrect

Problem:
Recently we observed our ant scripts are failing to fork a JVM process and they are failing with error “CreateProcess error=87, The parameter is incorrect"”

Solution:
Please reduce the classpath size for the javac/java

Root Cause:
In windows operating system if the arguments to process is too long . The process cannot be launched by the Operating system, In our case while ant forks the java/javac process and provides the classpath as argument . The classpath size exceeds the maximum size OS can handle. The solution for this problem is to reduce classpath. (i.e) reducing the size of arguments to process.

GWT compiler fails with Bad class version

Solution: This might happen because of incorrect version of Javac used by the GWTcompiler or the incorrect version of the jar files in project space.

We observed ant in netbeans is using the default JVM of the netbeans ide instead of JVM set for the project. Please change the default JDK used by neatbeans by changing netbeans.conf file.

For more info please refer http://wiki.netbeans.org/FaqJdkHome

Regards
Balaji

Followers