zoukankan      html  css  js  c++  java
  • 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的上一个目录下了。

  • 相关阅读:
    iview-cli 采坑记录
    js关于可视区高度的一些运算
    时间戳格式化
    快应用入坑指南
    webpack开发环境配置和生产环境配置
    webpack深入场景——开发环境和生产环境
    react-redux状态管理思想
    禁用浏览器双击时,选中文字的特性
    宝塔7.4.7专业版安装Nginx防火墙后,网站报错:ERR_CONNECTION_REFUSED的解决。
    帝国CMS修改模板或数据报错“您的请求带有不合法参数,已被网站管理员设置拦截!”
  • 原文地址:https://www.cnblogs.com/wintergrass/p/3874043.html
Copyright © 2011-2022 走看看