Error - Could not find or load main class

后端 未结 4 1337
南笙
南笙 2021-01-29 07:27

I want to connect my java program to connect with database and retrieve the data. its compile perfectly but runtime im getting this Error : Could not find or load main cla

4条回答
  •  孤街浪徒
    2021-01-29 08:28

    Let's say your Java2Sql is inside test package.

    Folder Structure:

    enter image description here

    And the code you are trying to run is as below.

    Code:

    package test;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class Java2Sql {
        public static void main(String args[]) {
            Connection connection = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "username", "pwd"); // Test DB
                System.out.println("Connected.");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    Commands you need to compile and run would be:

    javac Java2Sql.java
    java -classpath .;test/mysql-connector-java-5.0.4-bin.jar test.Java2Sql
    

    Compilation and Execution:

    enter image description here

提交回复
热议问题