This question already has an answer here:
UCanAccess Initializer Error (compile/run without IDE)
(1 answer)
Closed 6 years ago.
I am trying to make a basic JDBC program using MS Access. I downloaded the Ucanacess.zip file and I got in total 6 .jar files namely:
ucanaccess-3.0.7,
ucanload,
commons-lang-2.6,
commons-logging-1.1.1,
hsqldb, and
jackcess-2.1.3
I added them to the classpath as Environment Variables(Computer->Properties->Advanced System Settings->Environment Variable).
But when I run my Code it gives an Exception
java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver
Here is the code
class DB {
public static void main(String[] args) {
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}
catch(ClassNotFoundException ex) {
System.out.println(ex);
}
}
}
You have to run :
javac DB.java
java -cp . -cp ucanaccess-3.0.7.jar -cp ucanload.jar ... DB
The former to compile DB.java.
The latter to run java by setting classpath, "." is the directory where resides the compiled "DB.class"
Related
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
I am writing a simple program in Java using to demonstrate insertion of data in MySQL table using JDBC. But the program is generating ClassNotFoundException while registering driver.
import java.sql.*;
public class test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: "+e.getMessage());
}
}
}
I have added the driver JAR file in the same directory.
To compile the program:
javac -cp ".:~/Programs/D/friends/assignment/driver.jar;" test.java
To execute the program:
java -cp ".:~/Programs/D/friends/assignment/driver.jar;" test
O/p:
ClassNotFoundException: com.mysql.jdbc.Driver
Note: The issue is caused by ; at the end of the driver.jar and also not using fully qualified path.
Windows based OS uses ; separator whereas Unix-based OS uses : separator.
Solution :
First compile the code : javac test.java (Run this command)
Run the code without semi-colon : java -cp .:<fully-qualified-path>/driver.jar test
Sample output :
anish#Anishs-MacBook-Pro ~ % javac Test.java
anish#Anishs-MacBook-Pro ~ % java -cp .:/Users/anish/driver.jar Test
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Note : I'm using mysql-connector-8.0.15.jar. If you are using the same or greater, then change from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver as that class is deprecated.
1.at the end of the classpath there seems to be an extra semi-colon:
/assignment/driver.jar;"
try without it
2. Are you sure driver.jar is the correct file?
normally they are called like mysql-connector-java-8.0.23.jar
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
How to use a wildcard in the classpath to add multiple jars? [duplicate]
(4 answers)
Closed 3 years ago.
I am creating an application that is utilizing JDBC and am currently having problems running the code without getting java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. I have done extensive research into trying to solve this issue and nothing i have tried seems to work.
For one, I would like to note that my code works fine when it is run from my IDE (intellij). The database connection works and all the queries execute as necessary. The problem comes when I try to compile and run using javac and java in the terminal. Becuase this is a school project, I will need to enable my professors to be able to run it locally.
Secondly, I am compiling the application using the same jar files that are also included within intellij:
javac -classpath lib/\* DatabaseDriver.java
The lib folder contains my JDBC jar file. You can find the two jar files in this folder here (json.org) and here (connector j). Everything compiles correctly but it seems that once the following execution occurs in the code (see below for the full code) the is a runtime excetion that says the class can not be found:
Class.forName("com.mysql.cj.jdbc.Driver"); // also tried com.mysql.jdbc.Driver
The following code is what I am trying to compile (not the full DatabaseDriver, just enough to reproduce the issue):
import org.json.JSONObject;
import java.sql.*;
public class DatabaseDriver {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
public static String runStatement(String sql) throws Exception {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
return "";
}//end main
public static void main(String[] args) throws Exception {
DatabaseDriver.runStatement("SELECT * FROM employees LIMIT 10");
}
}
This is the full stack that I get when I run the code:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at DatabaseDriver.runStatement(DatabaseDriver.java:15)
at DatabaseDriver.main(DatabaseDriver.java:40)
Problem Summary: I am unable to run this script such that the jar file gets picked up even though it is the same jar file that my IDE is able to properly use. There must be something that I am forgetting or missing to do because all of the solutions I am finding only have to deal with people forgetting to include the correct jar files.
Run your class as given below:
In Unix based systems
java -cp ".:lib/*" DatabaseDriver
In MS Windows
java -cp ".;lib/*" DatabaseDriver
I'm trying to set up a simple Java application to connect to a SQLite database but keep receiving:
ClassNotFoundException: org.sqlite.jdbc.
I've downloaded the sqlite jdbc driver jar and placed it in the same directory as the .java, and I'm compiling on the command line with:
javac -cp sqlite-jdbc-3.7.2.jar sqlite3test.java
At runtime I then get the above exception. Below is the code:
import java.sql.*;
public class sqlite3test
{
public static void main(String args[])
{
Connection c = null;
try
{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:cs261.db");
}
catch ( Exception e )
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
How do I fix this issue?
Thanks.
Check the database version installed, in my case it was: SQLite version 3.8.2
I am using sqlite-jdbc-3.8.9.1.jar which is the greater version number than of the database installed and it is working perfectly.
You have to make sure the jar file version should be greater than or equal to the database version installed in your system.
From the latest version of java (from Java 6+) you don't need to load your class file.
Remove the following line and try it should work.
Class.forName("org.sqlite.JDBC");
Note: I suggest you to rename your class name from sqlite3test to Sqlite3Test though its not mandatory. According to the JAVA code convention class name should always start with Caps letter.
There are different ways to load the driver into your classpath:
place the jar into the $JAVA_HOME/jre/lib/ext folder
using maven dependencies
using java -cp
if you are using the IDE like eclipse then right click the project -> build path -> configure build path -> libraries -> add external jars and locate your jar there.
For your reference : http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/
I have a Baseloader.java class which makes use of Postgresql database, and I have a postgresql42.jar file which handles the jdbc stuff.
Baseloader.java
import java.sql.Connection;
import java.sql.DriverManager;
public class Baseloader
{
public static void main(String[] args)
{
System.out.println("Hello, sir!");
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/my_base",
"postgres", "123");
System.out.println("Done, sir!");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
So, when I compile and run this code through Netbeans (the postgresql.jar was included using GUI into Libraries folder) everything goes OK, the output is
Hello, sir!
Done, sir!
Opened database successfully
But when I try to run the compiled class file from cmd I get an error.
I placed Baseloader.class and postgresql42.jar into the same folder and cd'ed into that folder in cmd. Then I use the java -classpath postgresql42.jar Baserunner command which was proposed on one of the answers here, but it gava me Could not find or load main class Baseloader error.
If I type just java Baseloader it gives Exception in thread "main" java.lang.UnsupportedClassVersionError.
What I'm doing wrong and how to fix it?
EDIT: I've changed the java version in cmd: now java -version gives 1.8.0_73'.Then I checked which version Netbeans used: System.out.println(System.getProperty("java.version")) it also gave 1.8.0_73.
In environmental variables the JAVA_HOME is C:\Program Files\Java\jdk1.8.0_73, while System.out.println(System.getProperty("java.home")) gives C:\Program Files\Java\jdk1.8.0_73\jre. Can that difference be the case?
EDIT2: Changed JAVA_HOME to C:\Program Files\Java\jdk1.8.0_73\jre with no result.
Also javac -version gives the same newest 1.8.0_73 now.
I have a script that I would like to use to interface with a PostgreSQL database but I'm struggling a bit to include the driver when executing.
The Java code is very basic at the moment
import java.sql.*;
import java.util.*;
public class l_connect {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
} catch(Exception log) {
System.out.println(log);
}
}
}
If I execute this
# java l_connect
it does what I expect it to; outputs the exception log that it can't find the driver
I downloaded the postgresql driver and placed it in a directory in my project and then tried to execute it
# java -cp ".;../assets/postgresql-9.4-1202.jdbc4.jar" l_connect
and I get the error
# Error: Could not find or load main class l_connect
Why would this be happening? Is my usage of java -cp not correct?
UPDATE
I forgot to mention that my system is Fedora 22 Linux and I am not using an IDE I am using the terminal
May be this would help some one.
export CLASSPATH=/myapp1.jar:/myapp2.jar