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
Related
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
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 5 years ago.
I want to run selenium program from command line. The code I have is
class Akash
{
public static void main(String args[])
{
org.openqa.selenium.WebDriver driver = new org.openqa.selenium.firefox.FirefoxDriver();
}
}
Filename : Akash.java (Path : C:\Users\anigam\Desktop\Testing)
The required jars are present here : C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib
I compiled the file as :
javac -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*" Akash.java
The compilation was successfull and I got Akash.class generated here :
C:\Users\anigam\Desktop\Testing
When I am trying to execute this class file as :
java -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*" Akash
I am getting an error :
Error: Could not find or load main class Akash
Can someone please help.
Thanks much.
Your program is able to load dependant libraries from the classpath you provied but unable to load the class file which you compile and generate(Akash.class).
So try adding the current directory where Akash.class resides also in the class path.
1) Open Command prompt
2) cd C:\Users\anigam\Desktop\Testing
3) Running below command would help
java -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*;." Akash
Hope it helps.
-Sandeep
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"
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
This question already has answers here:
Running java in package from command line
(5 answers)
Closed 8 years ago.
I have already come across this error. I couldn't figure out today also.
package com.example.cassandra;
public class test
{
public static void main(String[] a)
{
System.out.println("test");
}
}
This is my java file. My working directory is
com/example/cassandra
Compile command is
javac test.java
Changed working directory to parent directory of com
cd ../../..
Run command
java test
Says
could not find or load main class test
Any body please explain what's the problem here?
You will need to actually specify the package name:
java com.example.cassandra.test