28 February 2011

Different ways to write files in Java

If you ever wanted to write a file using Java, you can find different ways to do it. Some of them are as follow:
import java.io.*;
class FileWrite 
public static void main(String args[])
// Create file 
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
The above is a simple way of creating a file, writing into it and then closing it. There is one other way, which is nicer, as follow:
fileStream = new PrintStream(new FileOutputStream("out.txt",true));
// Redirecting console output to file
// Redirecting runtime exceptions to file
This way, you are setting the system.out to a file. After that, anything you put on the output buffer instead of printing it out on the console, it will be printed in a file system.

22 February 2011


Just find the oc4j.cmd in the bin directory and use the following command to set your ORACLE_HOME
set ORACLE_HOME=C:\Files\Develop\External\oc4j_extended_101340
add this line the to the first line of oc4j.cmd. Using this option, you don't need to set ORACLE_HOME in your environment variables any more.

PL/SQL Developer - Oracle Home

There are two ways to define Oracle home for the PL/SQL developer.
One way is to have it in the environment variables, and point it to the installed directory. This works globally, so any other program/software is going to look for ORACLE_HOME will use this one.
Another way is to use the ORACLE_HOME registry value. You would be able to find it under HKEY_LOCAL_MACHINE -> SOFTWARE -> ORACLE -> ORACLE_HOME ; Considering you have installed Oracle on your PC.
Using this registry needs some tweaks. You need to find the params.ini in your PL/SQL developer installed directory and uncomment the oraclehome and set it to ORACLE_HOME.
In the latter approach, PL/SQL developer first looks at the params.ini file for ORACLE_HOME, so you can have set your ORACLE_HOME variable in your environment variables to some other directories and use another one with your PL/SQL developer.

16 February 2011

OC4J Standalone

If you are running a standalone OC4J, make sure that you have set the ORACLE_HOME and JAVA_HOME properly. If an IT guy in your company has set up your computer, then you need to double check that ORACLE_HOME is not set to the oracle folder installation but the location where you unzipped OC4J.

10 February 2011

How to run Mule on your Eclipse

I have the following setting on my machine:
  • Java 1.6.0_23
  • Eclipse Hellios
  • Mule 2.2.1
You can find almost a good Mule startup guide at here. However, this configuration guide is not really reliable. So follow the following instructions:
Configure your Eclipse with 1.5 version of Java. You need to download Java 1.5 and install it somewhere on your PC, doesn’t matter where.
You only need to configure Eclipse once. With subsequent projects, you can skip these steps.
  1. Start Eclipse.
  2. In the Workspace Launcher, specify the location of the examples directory under your
    Mule home directory (such as C:\mule\examples), and click OK.
  3. Click the Workbench icon on the right to display the workbench.
  4. Choose Window > Preferences.
  5. Expand Java in the navigation tree, click Compiler, and then change the compiler
    compliance level to 1.5.
  6. Please note that you might get a warning message in your eclipse window saying that you need to create a JRE environment in your Eclipse.
Installing Mule IDE (Eclipse Galileo)
  1. Start Eclipse, and set up a workspace for your installation of Mule if you haven't already.
    (Make sure your workspace does not have a space in the directory path)
  2. In the workbench view, choose Help > Install New Software.
  3. Click Add next to the Work with text box, enter
    http://dist.muleforge.org/mule-ide/updates/3.4/, and click Enter.
  4. Click the Mule IDE check box and click Next, and after Eclipse processes for a moment,
    click Next again.
  5. Review the IDE license, select the option to accept the license, and then click Finish.
  6. Click Yes to restart Eclipse.
From this moment, it should be easy. Whatever project you need to create, just create as a Mule project. You need to have a Mule server installed for your Eclipse. Installing is as easy as downloading the mule from its website, unpacking it and then simply pointing to it via the Eclipse environment.
You also have the option to create a Mule configuration file. I suggest you to do that under a Mule project. Creating a project, you can simply be able to use the Mule examples as well. For example, you want to implement a simple web form, just click the Hello example and then you can change the code and have your own logic implemented.
When you use Eclipse, you are saving time. Whenever you change things in your source code and run it on the server, Eclipse will deploy all the jar files and you instantly see the results.
Just keep in mind that you need to run the configuration files and not java files using Eclipse Mule server configuration.

7 February 2011

How to find name of a database in Oracle

You can use any of these commands:
  • SQL> Select name from v$database;
  • SQL> select instance_name from v$instance;
  • SQL> select * from global_name;
  • SQL> select to_number(translate(substr(version,1,9),'1.$','1')) from v$instance;
  • SQL> select s.machine from v$session s where s.audsid userenv('sessionid');
  • SQL> select global_name from global_name;

2 February 2011

log4j properties / Java Wrapper Service


Debug and Trace in log4j to set here: log4jproperties
It usually can be found in package/conf or some simillar path.
then you set the log4j property as follow:

Java Wrapper Service

Java wrapper service is a java package that allow you to install and run your java programs as services in windows. When looking at logs generated log4j, you will get some of the followings:
INFO   | jvm 1    | 2011/02/02 10:57:08 | WrapperManager Debug: Send a packet PING : ping
DEBUG  | wrapperp | 2011/02/02 10:57:08 | read a packet PING : ping
DEBUG  | wrapperp | 2011/02/02 10:57:17 | send a packet PING : ping
INFO   | jvm 1    | 2011/02/02 10:57:17 | WrapperManager Debug: Received a packet PING : ping
Apparently this shows that the service is trying to see if there is a live JVM or not, and if not it will uses its own (I guess).