The "Java" FAQ console executes the class with the custom package definition, which appears in the Exception in thread main Java.lang.NoClassDefFoundError__Java

Source: Internet
Author: User
The Java console executes the class with the custom package definition, which appears in the "Exception in Thread" main java.lang.NoClassDefFoundError:ConnectSQLServer (wrong name: Sine/connectsqlserver) "

1, first explain code implementation: custom Package sine,

the source code save path is: E:\JSP\HibernateDemo\HibernateDemoProject\src\sine\ConnectSQLServer.java

Package sine;

Import java.sql.*; public class Connectsqlserver {public static void main (string args[]) {string Url= "Jdbc:sqlserver://localhos
        
        T:1433;databasename=pubs ";
        String user_name= "SA";
        String user_password= "Sine88";
        
        String sqlstr= "select * from jobs;";
            try{Connection con=drivermanager.getconnection (Url,user_name,user_password);
            System.out.println ("Connect success!");
            Statement st=con.createstatement ();
            System.out.println ("Create Statement success!");
            ResultSet rs=st.executequery (SQLSTR);
            System.out.println ("Query success!");
            
            System.out.println ("-----------------------");
            while (Rs.next ()) {System.out.println (rs.getstring ("Job_desc"));
            } rs.close ();
            St.close ();
        Con.close (); The catch (Exception err) {System.out.println ("CoNnect failed! ");
        Err.printstacktrace (System.out); }
    }
}

2, switch to the Java source code in the folder directory, the compilation of Java code.

E:\jsp\hibernatedemo\hibernatedemoproject\src\sine>javac Connectsqlserver.java

3, byte code interpretation and operation: Java connectsqlserver The following error message:

E:\jsp\hibernatedemo\hibernatedemoproject\src\sine>java Connectsqlserver
Exception in thread "main" java.lang.NoClassDefFoundError:ConnectSQLServer (wrong Name:sine/connectsqlserver)
At Java.lang.ClassLoader.defineClass1 (Native method)
At Java.lang.ClassLoader.defineClass (Unknown Source)
At Java.security.SecureClassLoader.defineClass (Unknown Source)
At Java.net.URLClassLoader.defineClass (Unknown Source)
At java.net.urlclassloader.access$100 (Unknown Source)
At Java.net.urlclassloader$1.run (Unknown Source)
At Java.net.urlclassloader$1.run (Unknown Source)
At Java.security.AccessController.doPrivileged (Native method)
At Java.net.URLClassLoader.findClass (Unknown Source)
At Java.lang.ClassLoader.loadClass (Unknown Source)
At Sun.misc.launcher$appclassloader.loadclass (Unknown Source)
At Java.lang.ClassLoader.loadClass (Unknown Source)
At Sun.launcher.LauncherHelper.checkAndLoadMain (Unknown Source)

4. Solution method

Switch the table of contents on the console to the previous folder on the package name, for example, where the package name is called sine, then switch to the sine directory, which is the SRC directory,

Then execute the following command: E:\jsp\hibernatedemo\hibernatedemoproject\src>java sine. Connectsqlserver

At this point the results are as follows:

E:\jsp\hibernatedemo\hibernatedemoproject\src>java sine. Connectsqlserver
Connect success!
Create Statement success!
Query success!
-----------------------
New Hire-job not specified
Chief Executive Officer
Business Operations Manager
Chief Financial Officier
Publisher
Managing Editor
Marketing Manager
Public Relations Manager
Acquisitions Manager
Productions Manager
Operations Manager
Editor
Sales representative
Designer

by then, the problem will be solved. Similarly, adding a package name is: Org.sine, then you need to switch to Org's previous directory when executing.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.