Eclipse: Exported library fail to instantiate oracle driver ojdbc.jar - java

I have a library that contains functionality to connect to on oracle database. When I export this library (as a JAR) and use it in the main project, it gives an exception when loading the driver with class.forname. It obviously cannot find the ojdbc driver. I included this driver in the build path and as exported library.
I tested and used the driver directly from the main project, and it work, it connects to oracle db.
Thanks.

The problem is that your driver is a jar file, and when you export the app as a jar file, that driver will be a jar file in a jar file. For that scenario you either need a special classloader or put the driver jar file in the classpath of your main program.

Explore your exportedjar by using WinRar and check if it contains a jar under jar.

Related

Connect Derby to java

I'm trying to connect to a database using Derby in Java (using NetBeans), bIt i keep getting this error when I try to create a new database:
An error occurred while crating the database java.lang.ClassNotFoundException; org.apache.derby.jdbc.ClientDriver.
I've tried using Derby 10.16.1.1 and Derby 10.14.2.1.
The error message you are seeing suggests that the class org.apache.derby.jdbc.ClientDriver is not being found by the class loader at runtime. This class is necessary for connecting to a Derby database.
There are few possible causes:
The Derby jdbc driver is not in the class path of your project or application.
The version of jdbc driver jar file isn’t correct version for the version of Derby you are using. Please, check versions.
There is a comprehensive derby documentation where you can find more info and step-by-step guide.
In order to add org.apache.derby.jdbc.ClientDriver to your class path you can do few things, depending on your development environment:
If you're using an IDE like Netbeans, you can add the JAR file to the project's build path by IDE UI, for example, that thread explains how to do so.
If you're building your project with a build tool like Maven or Gradle, you can add the JAR file as a dependency in your project's pom.xml file or build.gradle file. You can read more about that approach here and here
If you're running your project from the command line, you can add the jar file to the classpath by specifying it as a command-line argument when running your project's main class. For example, if the jar file is located in /lib/derby-jdbc.jar, the command would be:
java -cp /lib/derby-jdbc.jar YourMainClass

No suitable driver found when exporting to a JAR-file?

I am trying to get my project into a JAR so I can run it as a CLI.
I have two JDBC connectors that I am using, one for MySQL one for PostgreSQL. Both are located in the same directory and work fine if I run them in the IDE.
When I create the JAR the MySQL connector still works fine, however when trying to establish a connection to PostgreSQL the following error appears.
What really irritates me is that the connector seems to be included in the build of the jar.
Both the MySQL connector and PostgreSQL connector are listed in the build.
How can I go about fixing this?
The problem is that all JDBC-4-compliant JDBC drivers contain a file /META-INF/services/java.sql.Driver that lists the java.sql.Driver implementations in the JAR files. This is used by the java.sql.DriverManager to load the available JDBC drivers.
The process you used for merging apparently doesn't merge the different files from the drivers into a single file, so it only has the content of one of the drivers. As a result, the other driver isn't loaded automatically.
Possible solutions:
Don't merge JAR files into a single JAR, but instead use the Class-Path attribute of META-INF/MANIFEST.MF to specify the JARs you use, and execute your program with java -jar your.jar
Make sure META-INF/services/java.sql.Driver is merged correctly (or maybe provide your own), depending on how you merge, there might be an option to configure which files need to be merged
Explicitly load the drivers using Class.forName("com.mysql.cj.jdbc.Driver") and Class.forName("org.postgresql.Driver") (do it for both to prevent problems if order of merging files changes, and the other file wins)

Spring application Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]

Why application is not able to read the class from the build path? I did added ojdbc6.jar in the build path of the application.
Now when I kept this jar in the WEB-INF/lib directory, it worked fine.
Can someone please explain why it was not read from the project build path.
When you add it on the build path in your IDE , it is used for compiling the code (if there is a dependency on it).
Unless you package this and deploy on the server , the server has no reference to these files that were used for compilation. When you place it on WEB-INF/lib and package it , the jar file is also packaged in your .war file that is deployed and hence at runtime you will not get a Class missing or Driver missing error.

jdbc driver not found when execute from exported Jar

I have a Java applet, it works correctly when executed from eclipe, but I want to export it to a Jar and use it. but when I do that, i get jdbc driver not found, it seems like when exporting, jaybird is not exported.
For exporting I use eclipse export and choose Java/JAR File, in build path i have jaybird mark to export.
Any suggestions? Thanks in advance
Use Fat Jar to build runnable JARs that contain all dependent libs
You are most likely missing the files from the META-INF folder of the Jaybird jar file. These files are required for Jaybird to work. Another possibility is that you are missing one of the required dependencies of Jaybird (connector-api-1.5.jar, for Jaybird 2.2 or mini-j2ee.jar for earlier versions).
Even if you get this to work though, you will most likely experience an error later on, as Jaybird wasn't developed with support for applets in mind, see http://tracker.firebirdsql.org/browse/JDBC-254 and NoClassDefFoundError with jdbc applet
BTW: Why don't you just use the jar as is. IMHO creating fat jars including all your dependencies is ugly and inflexible.
I finally get the answer, I had to sign the jaybird jar also because that jar was doing read/write operation in HDD
By using Eclipse you can simply solve the problem.
By going to Eclipse -> File -> Export -> Runnable JAR file and selecting Extract required libraries into generated JAR option, Eclipse will extract required libraries beside your project and creates the required MANIFEST.MF file for you and then will pack them all together in your JAR file.

I need some help concerning using 2 jar files in Java

Well I have 2 .jar files. The main jar file is the jar file for my whole project and the other .jar file being the MySql JDBC Connector.
Well basically whats happening right now is that when I build the project I have the one main .jar file with everything but the MySql JDBC Connector .jar file is inside the main jar file when it builds in NetBeans.
Now when I am just running the project from within NetBeans the MySql JDBC driver can be found inside the src/com/game/mysql folder that I have it in. But when I build the project the Java application cannot locate the JDBC driver from within the main jar file.
When I open the main jar file with WinRar I can see that the JDBC jar file is still in its /com/game/mysql/ folder. But why cant the Java application access it?
I have heard that nested .jar files are not supported in Java so Im thinking this might be the reason although Im not sure if thats true. Is there a way that I can make it so that the application can find the JDBC .jar file within the main jar file?
Also I have done the thing in NetBeans where you add the .jar file through right clicking project -> properties -> Library -> Add Folder/Jar. Thats what makes it work in the NetBeans run but still not the App build.
I have heard that nested .jar files are not supported in Java
More precise, classes in a JAR file which is packaged as a child JAR inside a main JAR are indeed by default invisible to classes in the main JAR.
You have basically 2 options:
Ship your application with 2 loose JARs: your.jar and mysql.jar and define the relative path to the mysql.jar in the Class-Path entry of the MANIFEST.MF file of your.jar.
Class-Path: mysql.jar
When you put both JARs in the same folder and execute your.jar by java -jar your.jar, then it will work.
Let your IDE repackage all loose classes of mysql.jar inside your.jar or add a special classloader which preloads the classes of any embedded JARs. Since I don't do Netbeans, I can't tell whether it supports it and if so, how to do it. In Eclipse, however, this is definitely possible. See also this answer.

Categories