ClassNotFoundException: oracle.jdbc.driver.OracleDriver - java

I wrote a java servlet program but when i run it, It was showing the Exception java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
My code
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(url, "username", "pass");
I am using Oracle 10.2.0. I added also ojdbc14.jar and ojdbc14_g.jar.
When I give the below command to command line. I get Error: Main method not found in class oracle.jdbc.driver.OracleDriver

I added also ojdbc14.jar and ojdbc14_g.jar
When adding third party libraries to your application, you must be sure they are in the Build Path of your application. In case of web applications, every third party library must be inside the WEB-INF/lib of the application so when deployed to the server (Tomcat, JBoss, etc.) they can be recognized and loaded when running your application.

Steps to rectify (if running from command prompt)
Step 1- Copy the ojdbc6 jar file from
C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib (Note- this path may differ as per the installation).
Step 2- Paste the ojdbc6 jar file in the Location
C:\Program Files\Java\jre1.8.0_45\lib\ext (Note- this path may differ as per the installation).
Step 3- Now run the programme java -cp . . It should successfully run without any error.

Related

How to share database access between multiple Java wars? [duplicate]

I have 2 java web apps that need to connect to SQL Server Database using Windows Integrated Authentication.
The first one that is loaded works fine but the second one throws the exception:
Native Library sqljdbc_auth.dll already loaded in another classloader
The error above occurs when I place the sqljdbc_auth.dll in one of the folders:
C:\WINDOWS\system32\
C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\
If I place the sqljdbc_auth.dll in one of the folders below:
/WEB-INF/lib directory of each web application
C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\
Both apps throw the exception:
Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
I am using this code to load the driver:
Class.forName("jdbc:sqlserver://<HOST>;databaseName=<DBNAME>;integratedSecurity=true;");
How can I solve it?
Each web application has its own Classloader (isolating them). When you call the Class.forName() method, there is a static block which is trying to load the shared library (dll file) - so both your web apps are trying to load the shared lib, hence the error message when the second one attempts to load.
The JDBC jar you have for sqlserver should be moved from being bundled with your wars, to the tomcat 7.0/lib folder and copy the sqljdbc_auth.dll to tomcat/bin folder - this way it will be in the tomcat parent classloader, and the class will only be loaded once.
|----------------------------------|
| sqljdbc*.jar --> tomcat*/lib |
|----------------------------------|
| sqljdbc_auth.dll --> tomcat*/bin |
|----------------------------------|
I think you are on the right track.
For command line startup you can easily solve the no sqljdbc_auth in java.library.path issue by setting the environment variable
CATALINA_OPTS=-Djava.library.path=/path/to/dll
If you are running tomcat as a service, change the Options parameter under
HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat7\Parameters\Java
to include:
-Djava.library.path=/path/to/dll
The same error occurs in Jasper Studio.
Probably not the best solution but place the sqljdbc4.jar in C:\Program Files (x86)\TIBCO\Jaspersoft Studio-6.1.0.final\features\jre.win32.win32.x86.feature_1.7.0.u80\jre\lib\ext and the sqljdbc_auth.dll in C:\Program Files (x86)\TIBCO\Jaspersoft Studio-6.1.0.final\features\jre.win32.win32.x86.feature_1.7.0.u80\jre\bin
And it will work.

Weblogic 12.2.1.3 Error: Could not find or load main class com.oracle.cie.wizard.WizardController

I was trying to configure Weblogic 12.2.1.3 on Linux account.
After going through oracle documentation, I understood we setup Weblogic by running config.sh script located in /opt/weblogic12213/wlserver_12.2.1.3/installation/oracle_common/common/bin folder.
But this script is giving error as :
Error: Could not find or load main class
com.oracle.cie.wizard.WizardController
Below is the last command which it executes in setup script and it is giving an error :
/usr/java/jdk1.8.0_192/bin/java -Dpython.cachedir=/tmp/cachedir
-Xms32m -Xmx1024m -Dweblogic.alternateTypesDirectory=/opt/weblogic12213/wlserver_12.2.1.3/installation/wlserver/../oracle_common/modules/oracle.oamprovider,/opt/weblogic12213/wlserver_12.2.1.3/installation/wlserver/../oracle_common/modules/oracle.jps
com.oracle.cie.wizard.WizardController -target=config-oneclick
scripts provided by weblogic doesn't set CLASSPATH correctly.
com.oracle.cie.wizard.WizardController comes under com.oracle.cie.wizard_7.8.2.0.jar .
Steps done.
1. Copied config_internal.sh script on some local path. (Because I didn't have the root permission.)
2. Appended this jar in CLASSPATH varibale like this :
CLASSPATH="${FMWCONFIG_CLASSPATH}${CLASSPATHSEP}${DERBY_CLASSPATH}:/opt/weblogic12213/wlserver_12.2.1.3/installation/oracle_common/modules/com.oracle.cie.wizard_7.8.2.0.jar"

On other pc java.sql.SQLException: No suitable driver found for jdbc:sqlserver

I wrote an app and it works fine by me. But on other pc-s it throws an exception.
java.sql.SQLException: No suitable driver found for jdbc:sqlserver
I give it (only the app.jar file) to my college, and he gets this error. I give him a jdbcdriver.jar file and, nothing changed.
However I use jre7, I added a line to the code:
Class.forName("com.mysql.jdbc.Driver");
And than he gets another exception:
Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
What can I do to run this app properly on other pc-s?
Put the jar file, containing the MySql Driver on the classpath when starting your application.
The switch is called -cp or -classpath like this:
java -cp ".;mysql.jar" my.package.Main
assuming you start your application from the root directory which is named my.package.Main. The classpath separator is the one for Windows. For unix based OSs you need to use : i guess.

Unable to connect Oracle 11g via thin

I am trying to connect to Oracle 11g DB through my below java code, in an web application using Tomcat 4 server. ojdbc6.jar in Eclipse build path, but getting this below Error.
BUT when I tried this piece of same Java code in a Java file (public static void main()) and having ojdbc6.jar in Eclipse build path, it was able to connect to d DB, Very Strange!!
Then why this same code is not working in my web application??
Server: Tomacat 4
JDK Version: JDK 1.6
Java Code:
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#hostName:portNum:SID","user", "pass");
Getting this below Error:
java.lang.ArrayIndexOutOfBoundsException: 7
at oracle.security.o3logon.C0.r(C0)
at oracle.security.o3logon.C0.l(C0)
at oracle.security.o3logon.C1.c(C1)
at oracle.security.o3logon.O3LoginClientHelper.getEPasswd(O3LoginClientHelper)
at oracle.jdbc.ttc7.O3log.<init>(O3log.java:289)
at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:251)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:246)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:365)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:260)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Since your standalone program is running correctly, it means your program runtime classpath is fine. However for Tomcat 4 its failing which raises a suspicion on the tomcat classpath. My guess is that since your tomcat is pretty old, it might be containing old driver class (may be classes12.jar or older ojdbc jar file in libraries of your tomcat (especially look into the $CATALINA_HOME/common/lib). If that is the case remove all other jars except ojbdc6 from your classpath. Also make sure your web application libraries do not contain the classes12.jar or older ojdbc jars.
Also you can try to use -verbose:class in your tomcat jvm startup to print the information from where the classes are loaded. This will however output lots of information in the log files.Search for the driver class and it will show you where this was loaded from.
Loaded oracle.jdbc.driver.OracleDriver from file: xxx.jar

Why is DB2 Type 4 JDBC Driver looking for native library db2jcct2?

I thought the Type 4 JDBC driver was pure Java and wouldn't require native libraries.
When I put db2jcc4.jar in the WEB-INF/lib directory of my Tomcat app packaged as a .war file, I get the following error when attempting to use the app: Got SQLException: com.ibm.db2.jcc.am.SqlException: [jcc][10389][12245][4.12.55] Failure in loading native library db2jcct2, java.lang.UnsatisfiedLinkError
The relevant application code is as follows and the exception is thrown due to the last line in the listing:
import com.ibm.db2.jcc.DB2SimpleDataSource;
// ...
DB2SimpleDataSource main_db2_data_source = new DB2SimpleDataSource();
main_db2_data_source.setUser(main_database_user);
main_db2_data_source.setPassword(main_database_password);
main_db2_data_source.setServerName(main_database_host);
try {
Integer main_database_port_integer = Integer.parseInt(main_database_port);
main_db2_data_source.setPortNumber(main_database_port_integer);
} catch (NumberFormatException exception) {
throw new WebException("...");
}
Connection main_connection = null;
try {
main_connection = main_db2_data_source.getConnection();
I suspect the problem is that you haven't told it to use the type 4 driver - the same jar file contains both type 4 and type 2 drivers, I believe.
Try:
main_db2_data_source.setDriverType(4);
The db2 driver needs another jar that includes the license.
This license controls the connection type. If you are going to use "db2 connect" to connect to a mainframe as an i series you should use the corresponding license. If you are going to connect to a Linux UNIX or Windows server, the license is included when you get the "Data server client for JDBC"
Also try this:
Goto Configure Build Path --> Libraries
--> JRE System Libraries
--> Native Library Location : Set this to %DB2HOME%/BIN
(which is where db2jcct2.dll is saved)
Recently i have faced this issue, when i was connecting to DB2 from Glassfish server. for this i have followed below steps and resolved this issue.
Please check it the below steps
step1) i have checked DB2 details in Domain.xml file.there i have seen only
username,pwd,databaseName,serverName ,portnumber, But i havent see DriverType.
Means Type of driver is 2 or 4.
2)for adding Type of driver i have logged into the Glassfish server admin console
Resources-->JDBC-->Connection pool -->our poolname --.add extra property
here i haved added as drivertype is 4.
Hence my problem has been solved
Thanks,
Ramaiah Pillala.

Categories