Tuesday, May 19, 2009

Invoking external program from Java

To invoke the external programs from Java, the simplest way is to use the capabilities of the Runtime class. Following sample program should illustrate the same:
 import java.io.*;  
 public class Main {  
   public static void main(String args[]) {  
    try {  
    Runtime rt = Runtime.getRuntime();  
    //Process pr = rt.exec("cmd /c dir");  
    Process pr = rt.exec("c:\\helloworld.exe");  
    BufferedReader input = new BufferedReader(new   
     InputStreamReader(pr.getInputStream()));  
    String line=null;  
    while((line=input.readLine()) != null) {  
     System.out.println(line);  
    }  
    int exitVal = pr.waitFor();  
    System.out.println("Exited with error code "+exitVal);  
    } catch(Exception e) {  
     System.out.println(e.toString());  
     e.printStackTrace();  
    }  
   }  
While hunting for this solution, I came across a very elaborate link regarding the same.
http://www.rgagnon.com/javadetails/java-0014.html

Tuesday, May 5, 2009

Converting the exception stracktrace to string

Method 1 (using log4j):
 logger.error(ex, ex);  
Method 2 (without log4j):
 public String stackTraceString(Exception e) {  
      StringBuffer s = new StringBuffer();  
      StackTraceElement[] frames = e.getStackTrace();  
      for (int i=0; i < frames.length; i++) s.append(frames[i].toString()+"\n");  
      return new String(s);  
 }  
Method 3 (without log4j):
 /** Converts the exception stracktrace to string. */  
 public StringBuffer getExceptionStackTrace(Exception ex) {  
 StringWriter strWriter = new StringWriter();  
      if (null != ex) {  
           PrintWriter out = new PrintWriter(strWriter);  
           ex.printStackTrace(out);  
      }  
      return strWriter.getBuffer();  
 }  
 /** May be used to set the exception as fault content. */  
 public String getExceptionAsXmlText(Exception ex) {  
      if (null != ex) {  
           String message = ex.getMessage();  
           String stackTrace = getExceptionStackTrace(ex).toString();  
           String exXmlText = "<exception>" + 
           "<message>" + message + "</message>" +  
           "<stack-trace>" + stackTrace + "</stack-trace>" +  
           "</exception>" ;  
           return exXmlText;  
      }  
      return "";  
 }  

Import / Export full Orcale data

Using 'exp' command:

To export the entire database to a single file dba.dmp in the current directory.
- Login to server
- Use following command:
exp SYSTEM/password FULL=y FILE=dba.dmp LOG=dba.log CONSISTENT=y
or
exp SYSTEM/password PARFILE=params.dat

{oracle.home}/product/10.2.0/bin/expDB/prodDB FULL=Y FILE=forITMc.dmp LOG=export.log CONSISTENT=Y COMPRESS=Y
where params.dat contains the following information:
FILE=dba.dmp
GRANTS=y
FULL=y
ROWS=y
LOG=dba.log

To dump a single schema to disk (we use the scott example schema here)
- Login to server which has an Oracle client
- Use following command:
exp / FIlE=scott.dmp OWNER=scott

To export specific tables to disk.
- Login to server which has an Oracle client
- Use following command:
exp SYSTEM/password FIlE=expdat.dmp TABLES=(scott.emp,hr.countries)
- The above command uses two users : scott and hr

exp / FILE=scott.dmp TABLES=(emp,dept)
the above is only for one user

Using 'imp' command:

To import the full database exported in the example above.
imp SYSTEM/password FULL=y FIlE=dba.dmp

To import just the dept and emp tables from the scott schema
imp SYSTEM/password FIlE=dba.dmp FROMUSER=scott TABLES=(dept,emp)

To import tables and change the owner
imp SYSTEM/password FROMUSER=someUser TOUSER=scott FILE=someUser.dmp TABLES=(unit,manager)

To import just the scott schema exported in the example above
imp / FIlE=scott.dmp

{oracle.home}/product/10.2.0/bin/impDB/pwd4NewDB FILE=forITMc.dmp

Monday, May 4, 2009

Setting and echoing the classpath information using Ant

Setting classpath is a must (although not 'required' by default) to get anything useful done with Ant's javac/java tasks. One way of setting classpath is by using the 'path' element as follows:
 <path id="application.classpath">  
      <pathelement path="${java.class.path}"/>  
      <fileset dir=".">  
           <include name="lib/*.jar"/>  
      </fileset>  
 </path>  
Then comes situations where you want to see the list of jars files contained in this classpath. Following simple Ant target will echo the content of the classpath variable ('application.classpath'):
 <target name="printClassPath">  
      <property name="appClasspath" refid="application.classpath"/>  
      <echo message="classpath= ${appClasspath}"/>  
 </target>