This is a simple problem, but if you don't pay attention to it, we will waste a lot of time looking for the root cause of the problem. Why? Because we usually don't pay attention to this problem, it is a "." problem.
Although I am not a master, I have been learning Java for almost a year and have encountered more or less problems. Just like the classpath problem we encountered today, we knew it at the very beginning, but since we haven't used classpath for a long time, we gradually forgot about it. Just in the day of database connection testing, I wrote a/home/admin/Java/mysqlconnection. java file:
- Import java. SQL. connection;
- Import java. SQL. drivermanager;
- Public class mysqlconnection {
- Public static void main (string [] ARGs ){
- Try {
- Class. forname ("com. MySQL. JDBC. Driver ");
- Connection Cn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306 ","*****","*****");
- CN. Close ();
- System. Out. println ("connection successful ");
- } Catch (exception e ){
- System. Out. println ("connection error ");
- }
- }
- }
It was correct during terminal compilation, but a major problem occurred during runtime: class not found:
- Exception in thread "Main" Java. Lang. noclassdeffounderror: mysqlconnection
- Caused by: Java. Lang. classnotfoundexception: mysqlconnection
- At java.net. urlclassloader $ 1.run( urlclassloader. Java: 200)
- At java. Security. accesscontroller. doprivileged (native method)
- At java.net. urlclassloader. findclass (urlclassloader. Java: 188)
- At java. Lang. classloader. loadclass (classloader. Java: 307)
- At sun. Misc. launcher $ appclassloader. loadclass (launcher. Java: 301)
- At java. Lang. classloader. loadclass (classloader. Java: 252)
- At java. Lang. classloader. loadclassinternal (classloader. Java: 320)
- Cocould not find the main class: mysqlconnection. program will exit.
After carefully checking the source files, you can't find any errors and compile and run them in eclipse, but the errors are correct. I was puzzled and even thought about whether it was a JVM problem. However, it can run normally in eclipse, and my eclipse JRE is the jre I configured. Therefore, the JVM cannot be faulty.
Take a closer look at the error message, since the class can not be found, it must be related to the class path, think of yesterday I began to learn JSP, so set the classpath pointing to the servlet-api.jar in Tomcat. I suddenly thought of the question "." When I first learned JDK configuration, I tried it:
[Admin @ localhost Java] $ Java-CP. mysqlconnection
Connection successful
If it succeeds, it indicates that it is really a classpath problem and the classpath =.: $ tom_cat/lib is reset.
Restart and run again. The problem is also successful.
The cause of this problem is the classpath vulnerability. If classpath is not set, the Java Virtual Machine will find $ java_home/JRE/lib and $ java_home/JRE/lib/EXT when installing the class, and then find the class in the current directory.
However, if classpath is set, the problem will be different. The JVM will not find the current directory, unless the classpath has set the class to be searched in the current directory, that is, ". ", so we must set the current directory when setting classpath. Because this problem does not conform to our habits, we call it a vulnerability.