Be alert for classpath Vulnerabilities

Source: Internet
Author: User
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:
  1. Import java. SQL. connection;
  2. Import java. SQL. drivermanager;
  3. Public class mysqlconnection {
  4. Public static void main (string [] ARGs ){
  5. Try {
  6. Class. forname ("com. MySQL. JDBC. Driver ");
  7. Connection Cn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306 ","*****","*****");
  8. CN. Close ();
  9. System. Out. println ("connection successful ");
  10. } Catch (exception e ){
  11. System. Out. println ("connection error ");
  12. }
  13. }
  14. }

It was correct during terminal compilation, but a major problem occurred during runtime: class not found:

  1. Exception in thread "Main" Java. Lang. noclassdeffounderror: mysqlconnection
  2. Caused by: Java. Lang. classnotfoundexception: mysqlconnection
  3. At java.net. urlclassloader $ 1.run( urlclassloader. Java: 200)
  4. At java. Security. accesscontroller. doprivileged (native method)
  5. At java.net. urlclassloader. findclass (urlclassloader. Java: 188)
  6. At java. Lang. classloader. loadclass (classloader. Java: 307)
  7. At sun. Misc. launcher $ appclassloader. loadclass (launcher. Java: 301)
  8. At java. Lang. classloader. loadclass (classloader. Java: 252)
  9. At java. Lang. classloader. loadclassinternal (classloader. Java: 320)
  10. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.