Saturday, March 19, 2016

How to enable JDBC-ODBC bridge for JDK 8

So JDK8 has removed JDBC-ODBC bridge, for whatever the reason. But if you don't have the source code of your existing product and don't want to spend time to reverse engineering them,  they just don't work in JRE 8. 

Are you stuck with JRE 7 or older forever? Not necessarily. Follow the step below, you can enable JDBC-ODBC bridge in JDK 8.

1. Download a JDK 7 or JRE 7.
2. Goto JRE\lib folder and find the rt.jar
3. Unzip it (if you have WinRAR installed) or you can rename it to rt.zip and unzip it.
4. Copy sun\jdbc and sun\security\action folders out, keep the folder structure. i.e., you should have the folder structure like below:
     Sun --> Security --> Action --> JDBC

5. Open a CMD window. Go to the parent folder of Sun folder. Run the command:
     jar -cvf jdbc.jar sun

6. The above command will create a file named jdbc.jar
7. Copy JDBC.jar to your JDK8 or JRE8 lib folder
8. Copy   jdbcodbc.dll from JRE\bin  of your JRE 7 installation to JRE\bin of your JRE 8 installation.
9. Restart your JVM.

Common errors and how to avoid them:

1. If you're using 64 bits Java 8, make sure that you create Java 7 jar file using Java 7 64 bits.  If you're using 32 bits Java 8, make sure that you create Java 7 jar file using Java 7 32 bits.

2. Make sure that your java.exe is the one you want to run. Often there are more than one java executables on your machine. They may be different version or different bits (32 vs. 64). Type java -version to confirm the java runtime. It would be the best if you have a new machine and install Java 8 SDK on the machine to test. Create your Java 7 jar file on a different machine and copy over. 

3. "Path not found" issue. This can be simply solved by setting the classpath. You need to set the class path something like this:
classpath=.;[file path to your jar file, for example, c:\JDK8\lib\jdbc.jar]; [All the other existing classpath]

You must have ".;" at the beginning of your classpath because otherwise, you may get "Could not find or load main class" error. 

Remember, after you set your classpath, double check it on the console to make sure that it is set correctly. On Windows, you need to close and reopen a new cmd window if you set it in the system environment setting.  

That's it. If you have questions, ask:-)