禁止的软件包名称:java

2022-09-02 02:14:48

我试图从我的数据库名称中获取数据,如jaane,用户名Hello和Password hello。错误:

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)

我的代码是

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

更改包名称。 作为软件包名称是被禁止的。java

编辑:将文件移动到不以 开头的(源)目录中,然后使用目录结构名称更改代码中的包名称。Main.javajavajavax


答案 2
java.lang.SecurityException: Prohibited package name: java

不能使用 java 作为包的名称。将其替换为其他内容。


推荐