Amazon Contextual Product Ads

Sunday, 5 June 2011

Solving java.lang.InstantiationException exception when running Java Concurrent Program

 

Assume that you want to create new Java Concurrent Program , but you also want to be able to execute this class as a standalone class with “main” method. Here is an example of such a class

public class SampleJavaConcProg implements JavaConcurrentProgram{

private Connection _conn;

public SampleJavaConcProg(Connection conn)
{
_conn
= conn;
}
public void runProgram(CpContext cp)
{
_conn
= cp.getJDBCConnection();

}
public static void main(String [] param) throws SQLException
{
DriverManager.registerDriver(
new OracleDriver());
String connectionString
= param[0];
Connection connection
= DriverManager.getConnection(connectionString,"SCOTT","TIGER");

SampleJavaConcProg prog
= new SampleJavaConcProg(connection);
}
}

You can see that this class can initialize Connection object by using CpContex object that is provided to you by Java Concurrent Program in EBS or directly by “main” method. But!!! If you try to run this class as a concurrent you will get the following error.


java.lang.InstantiationException: xxx.xxx.xxx.xxx.xxxx.SampleJavaConcProg at java.lang.Class.newInstance0(Class.java:335) at java.lang.Class.newInstance(Class.java:303) at oracle.apps.fnd.cp.request.Run.main(Run.java:152)


It doesn’t work.  What is the reson? Don’t know, but I do know how to solve it. Just create a default constructor in your class.


Modified code will look like


public class SampleJavaConcProg implements JavaConcurrentProgram{

private Connection _conn;

//default constractor
public SampleJavaConcProg()
{

}
public SampleJavaConcProg(Connection conn)
{
_conn
= conn;
}
public void runProgram(CpContext cp)
{
_conn
= cp.getJDBCConnection();

}
public static void main(String [] param) throws SQLException
{
DriverManager.registerDriver(
new OracleDriver());
String connectionString
= param[0];
Connection connection
= DriverManager.getConnection(connectionString,"SCOTT","TIGER");

SampleJavaConcProg prog
= new SampleJavaConcProg(connection);
}
}

No more error messages….. 


2 comments:

  1. Hi mike,its a nice post.I do also have addressed this same issue in my blog.But i have dealt with this problem in a different context.This is my blog address craftingjava.blogspot.in. Hope you may find this useful

    ReplyDelete
  2. Very nice. Your explanation is more general and actually explains why the error I described showed at the first place, something I didn't research. Now the picture is complete

    ReplyDelete