java.lang.ClassNotFoundException或找不到或加载主类错误 [英] Either java.lang.ClassNotFoundException or Could not find or load main class error

查看:308
本文介绍了java.lang.ClassNotFoundException或找不到或加载主类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次尝试在Windows下运行一些Java代码,我得到了:

First time trying to run some Java code under Windows here, and I got:

D:\Tmp>java JDBC_SQLServer
Picked up _JAVA_OPTIONS: -Xmx512M
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        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 java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at JDBC_SQLServer.main(JDBC_SQLServer.java:19)

即,java找到main,但没有找到lib.现在,-

I.e., java finds main but didn't find the lib. Now, --

D:\Tmp>java -classpath "C:\Program Files\Java\jdk1.8.0_162\lib" JDBC_SQLServer
Picked up _JAVA_OPTIONS: -Xmx512M
Error: Could not find or load main class JDBC_SQLServer

即,java找到了sqlserver.jdbc.SQLServerDriver库,但现在找不到主库.

I.e., java finds the sqlserver.jdbc.SQLServerDriver lib but can't find main now.

JDBC_SQLServer只是来自 https://docs.microsoft.com/zh-我们/sql/connect/jdbc/connection-url-sample

The JDBC_SQLServer is just a hack from https://docs.microsoft.com/en-us/sql/connect/jdbc/connection-url-sample

如何解决?谢谢.

推荐答案

classpath需要包含当前目录(为了找到您的类及其main -method和jar文件(不仅是包含罐子),因此呼叫可能如下所示:

The classpath needs to contain the current directory (in order to find your class and its main-method and the jar-file (not only a directory with jars in it), so a call could look like this:

D:\Tmp>java -cp "C:/mssql/lib/sqljdbc41.jar;." JDBC_SQLServer

您不需要在路径中使用反斜杠,并且由于反斜杠通常是由您使用的shell专门解析的,因此我个人尽量避免使用反斜杠.

You don't need to use backslashes for the paths and because backslashes are often parsed specially by the shell you use, I personally try to avoid them as much as possible.

这篇关于java.lang.ClassNotFoundException或找不到或加载主类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆