I
I
ilavio2020-11-17 16:59:22
Java
ilavio, 2020-11-17 16:59:22

JDBC application won't start, why?

Hello!
Finished learning the basics of java, decided to move on with JDBC. I began to create a simple connection to Postgresql according to the video lesson (in my opinion, it’s easier than a steamed turnip) I work through Eclipse. But then I get an error that I can't figure out how to solve:

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java
  at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:898)
  at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
  at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
  at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
  at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
  at java.base/java.lang.Class.forName0(Native Method)
  at java.base/java.lang.Class.forName(Class.java:398)
  at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:760)
  at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:655)


The connection code itself:
package java;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Contest {
  
  static final String ur = "jdbc:postgresql://127.0.0.1:5432/postgres";
  static final String log = "postgres";
  static final String pas = "lll";
  
  public static void main (String [] args ) {
    
    System.out.println("Testing connection to PostgreSQL JDBC");
     
    try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
      System.out.println("PostgreSQL JDBC Driver is not found. Include it in your library path ");
      e.printStackTrace();
      return;
    }
   
    System.out.println("PostgreSQL JDBC Driver successfully connected");
    Connection connection = null;
   
    try {
      connection = DriverManager.getConnection(ur, log, pas);
   
    } catch (SQLException e) {
      System.out.println("Connection Failed");
      e.printStackTrace();
      return;
    }
   
    if (connection != null) {
      System.out.println("You successfully connected to database now");
    } else {
      System.out.println("Failed to make connection to database");
    }
  }

}

and a window appears before the error:

5fb3d6108761b715942984.png

I also tried to google (swapped bin with javapath in environment variables). And importantly, I loaded the jdbc driver into the project manually NOT through maven.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-11-17
@ilavio

Failed to paste "Prohibited package name: java" into https://translate.google.com ?

A
Alexey P, 2020-11-17
@ruddy22

Give your package a different name.
A package named java already exists.

package <new_package_name>;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question