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.