Using Oracle’s JPublisher
Posted by jt - 07/06/09 at 09:06 amOracle has a handy little utility called JPublisher. This tool can be used to generate Java source code for PL/SQL packages and types. I’ve been using it for the last year or so to generate class files over PL/SQL packages. The tool works nicely by generating a Java class with methods mapped to each procedure or function in the targeted package. The method’s types correspond to the signature of the procedure or function. The Java developer can construct the class with a JDBC connection, then invoke the methods as needed.
JPublisher has been integrated into JDeveloper. Within JDeveloper, in the database browser, you can navigate to packages, then right click and select ‘Generate Java’ to bring up a wizard which runs JPublisher to generate Java class files. I got this to run fine in the 10G version of JDeveloper. The only limitation that I found was the database browser in 10G was limited to the signed in schema. The 11g version allows you to select different schemas, but I have never been able to get it to work. The 11g JDeveloper wizard just does nothing when executed. I left a post on the Oracle forums for JDeveloper. Their development team was not able to recreate the problem. Go figure. I’ve been able to replicate this issue on 4 different machines now.
The Oracle documentation for JPublisher is good, but if you just want to build a class over a package, it can be difficult to find the information you need to get started.
To run JPublisher you will need source in 3 jar files. All of these are included in the Oracle client install.
set CLASSPATH=c:\oracle\ora92\sqlj\lib\translator.jar;%CLASSPATH% set CLASSPATH=c:\oracle\ora92\jdbc\lib\classes12.jar;%CLASSPATH% set CLASSPATH=c:\oracle\ora92\sqlj\lib\runtime.jar;%CLASSPATH%
On my windoz machine I just added these to the environment.
The command line interface can be rather long. For ease, I created a little batch file to run it. Then I could edit the command in Notepad++. Here is an example:
java oracle.jpub.Doit -user=xxx -url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP) (HOST=xxxx) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xxxx)))" -numbertypes=oracle -usertypes=oracle -package=com.jtsblog.db.packages -case=mixed -input=gen.txt
The Oracle documentation examples show running JPublisher as
jpub ...
from the command line. I was never able to get that to work. I’m not sure what I missed, but running the Java class directly works fine for me (oracle.jpub.Doit). The user parameter is the user to connect to the database with. The url must be a valid JDBC url for the database.
The type mapping between Java and PL/SQL can be a little tricky. You have a number of options on setting up how JPublisher behaves for this. See this link for more info.
The last parameter is for an input file. I called mine ‘gen.txt’ and stored in the same directory as the batch file. In this file, you will store the objects you want JPublisher to generate source for.
Example:
SQL jt.my_pkg AS MyPkg
This tells JPublisher to map the package jt.my_pkg and generate a Java class called MyPkg. Since my batch file command has the parameter
-package=com.jtsblog.db.packages
, the class will be generated in the package ‘com.jtsbolg.db.packages’. You can map multiple packages and types simply by adding additional lines to the input file.
My little example here only touches on a small part of what JPublisher can do. You can learn more about JPublisher from Oracle’s documentation here.
A bunch of random technology stuff that has my attention. I work with a lot of Oracle, Java, and dabble with various open source software packages.
October 11th, 2010 at 6:45 am
how can I download oracle jpublisher ?
I am begginner in o jpb
please help me,
October 11th, 2010 at 7:15 am
I believe its part of JDeveloper, but you should also be able to find it in the Oracle DB installation too.
December 14th, 2010 at 6:07 pm
I really want to thank you very much for you clear and simple explanation about jPublisher, it’s just what i was looking for. i had much trouble trying to work with the command line “jpub” like in Oracle official documentation
thank you, and looking for more of your nice posts
December 14th, 2010 at 9:29 pm
Thanks for the feedback!! And I need to get off my duff and blog some more. Been slacking on that.
Cheers – JT
June 30th, 2011 at 8:37 am
Hi ,
I tried executing the JPub as described above. I am getting follwoing error :Exception in thread “main” java.lang.NoClassDefFoundError: oracle/jpub/Doit.
All 3 jars are present in the Oracle Install folder and is present in the class path as well.
Please help me .
July 1st, 2011 at 6:54 am
You need to check your classpath. For some reason the jars are not being found by the JVM. Also check the case of your commands. Java is case sensitive.
July 4th, 2011 at 7:25 am
Hi ,
.Instead of setting the realtive path for classpath, i specified the absolute path.
Thanks for replying.
Was able to remove the “NoClassDefFoundError”.
But now I am getting :
java.io.IOException: Cannot run program “javac”: CreateProcess error=2, The syst
em cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at sqlj.tools.Sqlj.runCompilation(Sqlj.java:1409)
at sqlj.tools.Sqlj.statusMain(Sqlj.java:543)
at oracle.jpub.DoitImpl.processSqlj(DoitImpl.java:840)
at oracle.jpub.DoitImpl.mainSql(DoitImpl.java:406)
at oracle.jpub.DoitImpl.main(DoitImpl.java:118)
at oracle.jpub.Doit.main(Doit.java:30)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
… 10 more
Error in Java compilation: Cannot run program “javac”: CreateProcess error=2, The system cannot find the file specified
Please help.
Thanks in advance
July 9th, 2011 at 3:25 am
Hi,
I have installed JDeveloper 11g Release 2. But when i go to “Types” in the Database Navigator and I right click on any of the object, i do not see the “Generate Java” option. Only option i see is “Generate Sql Script”. Do i need to do some setting for it to appear ?
Please Help.
January 24th, 2012 at 1:33 pm
Can you provide a few lines of sample code on how to use a JPublisher generated classes as an input to a stored procedure in Java?
See this for more details: http://bit.ly/AcZrJT
December 7th, 2012 at 5:34 am
In order for the “Generate Java” option to apprear you have to create a Java project.