• Java 控制台执行带自定义包定义的类,出现“Exception in thread "main" java.lang.NoClassDefFoundError: ConnectSQLServer (wrong name: sine/ConnectSQLServer)”


    1、先说明一下代码实现:自定义package sine,

    源代码保存路径为:E:JSPHibernateDemoHibernateDemoProjectsrcsineConnectSQLServer.java

     1 package sine;
     2 import java.sql.*;
     3 
     4 public class ConnectSQLServer {
     5 
     6     public static void main(String args[]){
     7         String url="jdbc:sqlserver://localhost:1433;DatabaseName=pubs";
     8         
     9         String user_name="sa";
    10         String user_password="sine88";
    11         String sqlStr="select * from jobs;";
    12         
    13         try{
    14             Connection con=DriverManager.getConnection(url,user_name,user_password);
    15             System.out.println("Connect Success!");
    16             Statement st=con.createStatement();
    17             System.out.println("Create Statement Success!");
    18             ResultSet rs=st.executeQuery(sqlStr);
    19             System.out.println("Query Success!");
    20             System.out.println("-----------------------");
    21             
    22             while(rs.next()){
    23                 System.out.println(rs.getString("job_desc"));
    24             }
    25             rs.close();
    26             st.close();
    27             con.close();
    28         }
    29         catch(Exception err){
    30             System.out.println("Connect failed!");
    31             err.printStackTrace(System.out);
    32         }
    33     }
    34 }

    2、切换到java源代码所在的文件夹目录下,编译java代码。

    E:JSPHibernateDemoHibernateDemoProjectsrcsine>javac ConnectSQLServer.java

    3、字节码的解释与运行:java ConnectSQLServer 出现如下错误信息:

    E:JSPHibernateDemoHibernateDemoProjectsrcsine>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、解决方法

    先将控制台上的目录切换到包名的上一个文件夹处,例如,这里的package名称为sine,那么这里就切换到sine的上一层目录,也就是src目录下,

    然后执行如下命令:E:JSPHibernateDemoHibernateDemoProjectsrc>java sine.ConnectSQLServer

    此时结果如下:

    E:JSPHibernateDemoHibernateDemoProjectsrc>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

    届时,问题解决! 同理,加入包名是:org.sine,那么执行的时候就需要切换到org的上一个目录下了。

  • 相关阅读:
    P2679 子串
    线段树优化建边
    P2444 [POI2000]病毒
    P3966 [TJOI2013]单词
    4327: JSOI2012 玄武密码
    UVA1449 Dominating Patterns
    P1250 种树
    P2255 [USACO14JAN]记录奥林比克
    SP283 NAPTIME
    P3436 [POI2006]PRO-Professor Szu
  • 原文地址:https://www.cnblogs.com/wintergrass/p/3874043.html
Copyright © 2020-2023  润新知