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

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

源代码保存路径为:E:\JSP\HibernateDemo\HibernateDemoProject\src\sine\ConnectSQLServer.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:\JSP\HibernateDemo\HibernateDemoProject\src\sine>javac ConnectSQLServer.java

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

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

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

然后执行如下命令:E:\JSP\HibernateDemo\HibernateDemoProject\src>java sine.ConnectSQLServer

此时结果如下:

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

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

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

时间: 2024-10-13 08:43:10

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

运行jar包时出现异常:Exception in thread "main" java.lang.ExceptionInInitializerError

/usr/local/java/jdk1.8.0_60/bin/java SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/jstorm-2.2.1/wait_deploy/test-jstorm-bolt.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar

【异常】Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern c

在windows上执行的时候,option(“timestampFormat”, “yyyy/MM/dd HH:mm:ss ZZ”)必须带上,不然报错: Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern component: XX. 使用比如:val df1 = spark.read.format("json").option("timestampF

解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)

当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOException { ProcessBuilder builder = new ProcessBuilder(); Process process = builder.command("dir d:\\").start(); InputStream inputStream = process.g

【异常】idea执行Main方法出现 Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

一.异常复现步骤 1)首先得是一个Spring MVC项目 注:Spring Boot项目有内置的web 容器,不会出现该问题 2)main方法存在于使用HttpServletRequest类的类中 3)项目POM中引入有javax.servlet-api坐标或没有 <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-ap

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: Demo (wrong name: com/zhangyun/Demo)解决办法

介绍一个Java初学者可能会遇到的问题 首先,创建一个类,如下: 文件名:Demo.java package com.zhangyun; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); } } 然后我找到对应在磁盘的位置,如下: E

Exception in thread &quot;main&quot; java.lang.UnsupportedClassVersionError: com/crack

执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Andale Mono"; color: #29f914; background-color: #000000 } span.s1 { color: #afad24 } sp

java Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: main (wrong name: pm/main)

javac main.java 编译后没有问题 java main 出现下面在问题: Exception in thread "main" java.lang.NoClassDefFoundError: main (wrong name: pm/main)        at java.lang.ClassLoader.defineClass1(Native Method)        at java.lang.ClassLoader.defineClass(ClassLoader.

使用sqljdbc连接mssql数据库,maven生成jar运行后报&quot;Exception in thread &quot;main&quot; java.lang.SecurityException&quot;错误

错误信息如下: Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 网上查到,说是需要重新打一下sqljdbc    http://blog.chinaunix.net/uid-26706281-id-3210704.html 把该博客的内容copy下来: 插件打成的jar包运行出现这个异常,异常处理办法 1.

Exception in thread “main” java.sql.SQLException: The server time zone value ‘?й???????’ is unrecognized or represents more than one time zone.

Exception in thread "main" java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) t