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);
}
}
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);
}
}
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
ReplyDeleteVery 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