Prohibited package name: java

2020-01-25 10:23发布

问题:

I tried to get the data from my database name as jaane with user name Hello and Password hello. Error:

java.lang.SecurityException: Prohibited package name: java
        at java.lang.ClassLoader.preDefineClass(ClassLoader.java:480)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: java.Main.  Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

my code is

package java;

import java.awt.Container;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author ritesh
 */
public class Main extends JFrame{


    public Main() throws SQLException
    {super("Database of Students");
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        }
     Connection connection = DriverManager.getConnection( "jdbc:derby:/localhost:1527","hello","Hello" );
     Statement statement = connection.createStatement();


 // query database
       ResultSet resultSet =
 statement.executeQuery( "SELECT * FROM COLLEAGUES" );
       StringBuffer results = new StringBuffer();
 ResultSetMetaData metaData = resultSet.getMetaData();
 int numberOfColumns = metaData.getColumnCount();

 for ( int i = 1; i <= numberOfColumns; i++ ) {
 results.append( metaData.getColumnName( i ) + "\t" );
 }

 results.append( "\n" );

 while ( resultSet.next() ) {

 for ( int i = 1; i <= numberOfColumns; i++ ) {
 results.append( resultSet.getObject( i ) + "\t" );
 }


 }

 // close statement and connection
 statement.close();
 connection.close();

// set up GUI and display window
JTextArea textArea = new JTextArea( "Hello");
 Container container = getContentPane();

 container.add( new JScrollPane( textArea ) );

 setSize( 300, 100 ); // set window size
 setVisible( true ); // display window
}

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Main window = new Main();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // TODO code application logic here
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

回答1:

Change your package name. java as a package name is prohibited.

Edit: Move your Main.java file in a (source) directory that doesn't start with java or javax and use the directory structure names to change your package name in the code.



回答2:

java.lang.SecurityException: Prohibited package name: java

You cannot use java as the name of your package. Replace it to something else.



回答3:

You need to specify the Database Name, and there are two slashes before localhost. I assume hello is your username and Hello the password...

Connection connection = DriverManager.getConnection( "jdbc:derby://localhost:1527/DatabaseName","hello","Hello" );

...that is after you change your package name to something other than java!



回答4:

Look at why `java.lang.SecurityException: Prohibited package name: java` is required?

User code is never allowed to put classes into one of the standard Java packages. That way, user code cannot access any package-private classes/methods/fields in the Java implementation. Some of those package-private objects allow access to JVM internals. (I'm thinking of SharedSecrets in particular.)



回答5:

Never Keep your class in a root package as "java" or never create a package starting as java. ... you can use any other identifier as your package name.



标签: java package