Exception in thread main java lang classnotfoundexception

How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

The java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception occurs if the MySQL JDBC driver class can not found in the java class path. The Class.forName() fails to loads the mysql driver class that is not available in the class path. If the MySQL JDBC driver jar is not available, or if the jar is missing in the java class path, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown.

The MySQL JDBC driver is used to connect a Java application to a MySQL database. The mysql driver sends the database query to the database from Java. The Mysql database executes the query and returns the data. The MySQL JDBC driver gathers data from the MySQL database and sends it back to the Java application. If the MySQL JDBC driver is not available, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown by the java application.

Exception

The stack trace of the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be as below. The driver class is not found in the java class path.

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.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:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.yawintutor.DBConnection.main(DBConnection.java:8)

How to reproduce this exception

If the MySQL JDBC driver jar is missing or not available in the java class path, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown. The example below will show the exception.

package com.yawintutor; import java.sql.Connection; import java.sql.DriverManager; public class DBConnection < public static void main(String[] args) throws Exception < Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; Connection con = DriverManager.getConnection(url, user, password); if (con != null) < System.out.println("Database Connected successfully"); >else < System.out.println("Database Connection failed"); >> >

Solution 1

The MySQL JDBC driver class is available in the mysql driver jar. download the mysql driver jar from this path https://dev.mysql.com/downloads/connector/j/. The name of mysql driver jar is same as mysql-connector-java-8.0.20.jar. Add the jar to the java class path.

download the jar mysql-connector-java-8.0.20.jar from https://dev.mysql.com/downloads/connector/j/ ( goto https://dev.mysql.com/downloads/connector/j/ select "Select Operating System:" as your operating system and install or select "Select Operating System:" as "Platform Independent", download the zip and extract ) Add in your java project. In eclipse Right click in mysql-connector-java-8.0.20.jar -> Build Path -> Add to Build Path

Solution 2

If the java project is a maven project, add the mysql connector dependency in the pom.xml. The dependency will download the mysql-connector-java-8.0.22.jar in the repository and link to the project. The latest mysql connector dependency can be found in https://mvnrepository.com/artifact/mysql/mysql-connector-java

Читайте также:  Страница на печать

pom.xml

 mysql mysql-connector-java 8.0.22 

Solution 3

If the java project is build using the Gradle, add the dependency as below. When the project is build, the MySQL JDBC driver will be added to the java project.

Solution 4

If the java project is created with a basic editor and want to build and execute the java program, use the below command to compile and run.

javac -cp ./lib/mysql-connector-java-8.0.20.jar java -cp ./lib/mysql-connector-java-8.0.20.jar 

Solution 5

If the application is created as a web application, The jar file must be placed in the web server class path. For example, in tomcat, you can place either tomcat lib director or application WEB-INF\lib directory.

\lib\mysql-connector-java-8.0.20.jar or \webapps\\WEB-INF\lib\mysql-connector-java-8.0.20.jar 

Источник

How to Fix ClassNotFoundException in Java

How to Fix ClassNotFoundException in Java

The java.lang.ClassNotFoundException is a checked exception in Java that occurs when the JVM tries to load a particular class but does not find it in the classpath.

Since the ClassNotFoundException is a checked exception, it must be explicitly handled in methods which can throw this exception — either by using a try-catch block or by throwing it using the throws clause.

What Causes ClassNotFoundException in Java

Common causes of the java.lang.ClassNotFoundException are using the following methods to load a class:

In all the above cases, the class attempted to be loaded is not found on the classpath, leading to the ClassNotFoundException in Java.

ClassNotFoundException in Java Example

A very common example of ClassNotFoundException is when a JDBC driver is attempted to be loaded using Class.forName() and the driver’s JAR file is not present in the classpath:

public class ClassNotFoundExceptionExample < private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; public static void main(String[] args) throws Exception < System.out.println("Loading MySQL JDBC driver"); Class.forName(DRIVER_CLASS); >> 

Since the MySQL JDBC driver JAR file is not present in the classpath, running the above code leads to a java.lang.ClassNotFoundException :

Loading MySQL JDBC driver Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:340) at ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:6)

To fix the Java exception, the mysql-connector JAR should be included in the application classpath.

How to Resolve ClassNotFoundException in Java

The following steps should be followed to resolve a ClassNotFoundException in Java:

  1. Find out which JAR file contains the problematic Java class. For example, in the case of com.mysql.jdbc.driver, the JAR file that contains it is mysql-connector-java.jar.
  2. Check whether this JAR is present in the application classpath. If not, the JAR should be added to the classpath in Java and the application should be recompiled.
  3. If that JAR is already present in the classpath, make sure the classpath is not overridden (e.g. by a start-up script). After finding out the exact Java classpath used by the application, the JAR file should be added to it.

To fix the Java exception, the mysql-connector JAR should be included in the application classpath.

Track, Analyze and Manage Java Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates Java error monitoring and triaging, making fixing errors easier than ever. Try it today.

Источник

Exception in Thread Main Java.Lang.ClassNotFoundException in IntelliJ IDEA

Exception in Thread Main Java.Lang.ClassNotFoundException in IntelliJ IDEA

  1. the java.lang.ClassNotFoundException Error in Java
  2. Causes of java.lang.ClassNotFoundException in Intellij IDEA
  3. Solution to java.lang.ClassNotFoundException in Intellij IDEA

Today’s tutorial will discuss potential reasons for the java.lang.ClassNotFoundException whenever a Java program’s main method is executed.

the java.lang.ClassNotFoundException Error in Java

java.lang.ClassNotFoundException is triggered if the ClassLoader cannot find the class in its system. In the JVM (Java Virtual Machine) core library, ClassLoader is used to load and locate a class.

This error is thrown by ClassLoader if it cannot load a class from the application library.

In addition, you should be aware of the checked nature of this exception and the necessity to properly handle it when calling methods that can trigger the java.lang.ClassNotFoundException in Java, whether via a try-catch block or the throws condition.

Let’s have an example to understand better which throws java.lang.ClassNotFoundException in Java on Intellij IDEA 14.0. After that, we’ll discuss its causes and solution.

In this example, we constructed a basic Hello Programers! program. It’s producing incorrect output; hence, the java.lang.ClassNotFoundException exception will be triggered.

public class Hello   public static void main(String[] args)   System.out.println("Hello Programers!");  > > 
Exception in thread "main" java.lang.ClassNotFoundException: Hello at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:260) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116) 

Causes of java.lang.ClassNotFoundException in Intellij IDEA

  1. When we attempt to load a class by utilizing the class’s binary, we discover that it is not present in the classpath.
  2. If we use the loadClass() function of the ClassLoader class in Java.
  3. The java.lang.ClassNotFoundException occurs when the Java Virtual Machine attempts to load a class during runtime.

Solution to java.lang.ClassNotFoundException in Intellij IDEA

This is simply a problem with the Intellij IDEA. Therefore, please follow the steps below to fix it:

Источник

Оцените статью