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:-)





25 comments:

  1. I am having hard time compiling, do I need 32bit version or 64bit version of JRE 7 and I should use Java 8 (64 bit) to compile? DO you have have jdbc.jar for download?

    ReplyDelete
  2. I was able to create jdbc.jar and copied all the files as instructed,
    I also created CLASSPATH = C:\Program Files\Java\jdk1.8.0_60\jre\lib\jdbc.jar
    Now I am getting error: java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver.

    ReplyDelete
  3. Thanks for your help, I was able to create my excel file successfully. Few clarification I want to make.

    1) First make sure if your going for 64 bit environment choose Java JRE 7 or SDK 7 64 bit version.
    2) Once you have Separated the files in c:\sun folder run the command from c:\jar -cvf jdbc.jar sun.
    3) Once your jdbc.jar is created in my case I had to put it in C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext folder, instructions said to put it in lib which gave me the error class not found

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String connUrl = "jdbc:odbc:DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ="+Filename+".xls;DriverID=22;readonly=false"; // good 64 bit
    conn=DriverManager.getConnection(connUrl,"","");

    Thanks again you saved me tons of time and re coding.

    ReplyDelete
  4. Not Helpful I am mot able to create jar so can u please elaborate it more about the folder structure and how to fire command in cmd for creating final jar file??

    ReplyDelete
  5. please elaborate instructions...

    ReplyDelete
    Replies
    1. if you want I can send you a 64 bit jar I build in Email.

      Delete
    2. Please send me the compiled jdbc.jar to diptacs@gmail.com

      Also, please write the step-wise instruction how to do the whole thing to copy-paste

      Delete
    3. Hi I have created the jar file but I am still having the issue. Could you send me the jar file to my mail, v.vijay64@gmail.com . It would be great help to me.

      Thanks in advance

      Delete
    4. Hi I have created the jar file but I am still having the issue. Could you send me the jar file to my mail, v.vijay64@gmail.com . It would be great help to me.

      Thanks in advance

      Delete
    5. hi all, i am stuck in the same situation, can you please help to send the jar file to me at dhuynh@nlstech.net
      Appreciate.

      Delete
  6. Hi, I'd be happy if you could send me the jar file at j.lebomin@gmail.com. I found the directories you mention, but they contained no jdbc.
    I'm on a Mac, using java 1.7.0_79-b15 64 bits

    Thanks in advance

    ReplyDelete
    Replies
    1. Finally I got it from a windows installation package. For some reason, the mac one didn't contain the jdbc jar. I was on a 64 bits Windows 7 system, but even there I had to use the 32 bits version because I was getting null pointer exception when the JDBC was initializing... Thanks for the instructions, it was very helpful

      Delete
    2. Glad that it works for you:-)

      Delete
  7. Hi i am unable to create the jar file .. can you please send the jar file to my email vikkywaits@gmail.com

    ReplyDelete
  8. Hello, It has stopped showing the exception as "ClassNotFoundException" .
    I also added the classpath to the " lib" folder where i have located my jdbc.jar file.
    But after i run the program i am getting the error "Could not find or load main class"
    Please help me.

    ReplyDelete
  9. I am able to create jar file and placed in jdk and jre lib location.

    ReplyDelete
  10. I am able to create jar file and placed in jdk and jre lib location.but still i am getting ClassNotfoundException.

    Also followed "Kalim Khan" comments as well. Could you send me working inputs.

    Thanks in advance.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Guys, Can any one send java7 jre 64bit and 32bit of "jdbc.jar" and "jdbcodbc.dll" to triveni.bika@gmail.com.

    Please help me.

    Will it be any difference if I consume java7 jre jars from windows7 to windows10 platform (or) vice versa ?

    ReplyDelete
  13. Hi All,
    Can any one send compiled jdk7 jars and detailed steps to follow to "triveni.bika@gmail.com" .Please help me on this.

    ReplyDelete
  14. please send me also the jdk 7 jar file 64-bit to me to 4731sudhanshussb@gmail.com

    ReplyDelete
  15. Copy sun\jdbc and sun\security\action folders out,
    please tell me the meaning of this sentence.
    ASAP.

    ReplyDelete
    Replies
    1. That means, copy them to some other location (but maintain the same folder structure as it is).

      Delete
  16. Can you please tell me how do i restart the JVM, as mentioned in th last point?

    ReplyDelete