[英]Error message "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" is shown when run java on command line
I am trying to run the java program on command line but this message is shown:我正在尝试在命令行上运行 java 程序,但显示此消息:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver
Then I have searched a few hours and most solutions are about checking the然后我搜索了几个小时,大多数解决方案都是关于检查
For 1, I checked the java version I am using is 1.8对于 1,我检查了我使用的 java 版本是 1.8
So for 2, I have downloaded the sqljdbc41.jar, put the jar into a lib folder, and run the command with:所以对于 2,我已经下载了 sqljdbc41.jar,将 jar 放入 lib 文件夹,然后运行命令:
java -cp C://Testing/lib/* -classpath . sampleJava
For 3, I have edited the classpath to C://Testing/lib/sqljdbc41.jar对于 3,我已将类路径编辑为 C://Testing/lib/sqljdbc41.jar
Anyone knows do I miss something so that the same error message still shows?任何人都知道我错过了什么,所以仍然显示相同的错误消息吗?
Below is the sample program I run:下面是我运行的示例程序:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
public class sampleJava {
int x = 5;
public static void main(String[] args) {
sampleJava myObj = new sampleJava();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
System.err.println(e);
System.out.println(e.toString());
e.printStackTrace();
}
finally {
System.out.println("Done");
}
System.out.println(myObj.x);
}
}
This should help这应该有帮助
java -cp C:/Testing/lib/*; sampleJava
or或者
java -cp C:/Testing/lib/sqljdbc41.jar; sampleJava
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.