The perplexity of Classpath

Source: Internet
Author: User
Tags command line mssqlserver sleep tomcat

Yesterday at home to tidy my baby hard drive, found before a JSP based on the structure of the electronic trading platform source code, but also has a database, anyway, it is okay, so I want to get it, let it run. Next, download the JDBC driver that installs Tomcat and SQL Server (installed before JDK), after a series of configurations, the test JSP can run properly, JavaBean can run correctly, and the servlet can run correctly, it seems that the environment should have no problem. Then the key part is coming. I put SQL Server's JDBC-driven three jar packages into the classpath of the system, then started Tomcat, and couldn't wait to enter http://localhost:80/IC/in the browser's address bar, silently waiting for the system to appear. The system has finally run up, but see with my imagination is not the same, the interface of any access to the database where there are "Thisconnectionhasnotbeenestablishedyet." It is obvious that the problem is connected with the database.

The analysis shows that the system access to the database is realized through a javabean. So I copied the JavaBean source code into a text editor like Notepad, and added the main method to test whether the class was working properly. After compiling the class file, use Javaxxx to execute, from the output, there is no problem. But where exactly is the problem? So I copied this code into an Eclipse project, and after compiling it, the error of the report was the same as the error of accessing the JSP, which was further tracked and found in Class.forName ("Com.microsoft.jdbc.SQLServerDriver"); Times class did not find, that is to say, did not find class SQLServerDriver, and then open Mssqlserver.jar a look, sqlserverdriver impressively in the stand, the path is not wrong. I've got three jar packs in Classpath, why do I find them in the command line and not in eclipse? It's so confusing! The good mood of the weekend is completely disturbed by this problem, and secretly determined, must solve and then quickly. Next, is on the forum to ask people, there is QQ and MSN to ask someone, at the same time, they are also analyzing possible causes, to try.

Lenovo is often used in the work of the jar package will be added to the project's classpath, so I also add three jar packages to the project's classpath, and then recompile execution, found that the problem is not, the output is normal. At this point, I concluded that the problem was that the package loaded when the class was executed in eclipse was inconsistent with the classpath specified by the system, that is, the system's classpath specified class was not loaded into memory at all, so it was reported that the class was not found, but at the command line, The package that is loaded is the classpath specified by the system. Although the problem is clearly defined, but the specific reasons, I still can not explain. In the JSP, how do I load three jar packages into memory before starting Tomcat? I wrote a batch file, the content is set Classpath=e:\xxxxx\msbase.jar; E:\ Xxxxx\mssqlserver.jar; E:\ Xxxxx\msutil.jar; I execute this batch at the command line, and then, after executing the command to start Tomcat, I find that the error continues to indicate that there is a problem loading the jar pack. Here, I almost want to give up, unexpectedly was this classpath get coke head disorderly forehead, depressed ah! But I am not convinced that I should give up! Let's take a break and come back later!

A turn in the living room, got something to eat, feeling a little sleepy, so he went to sleep. Sleep for two hours, up when the sky is dark, so, and then back to the computer, think how to deal with it! So, in the brain, search for some of the problems associated with classpath and how to solve them. Vaguely remember, in the JDK Jre\lib\ext jar package, Java is automatically loaded, so I put three jars to the box, restart Tomcat, in the browser address bar input access address, silently waiting, the system finally did not report before the error, The database connection is OK, finally ran up. I have a heavy mouth long gas, not easy ah.

The system ran up here, but Classpath didn't get it all figured out, I just bypassed the problem. Today, when I go to work, I would like to ask South elder brother, but considering the Monday morning everyone will be busier, I refrained. I asked the question when I was about to eat at noon. South elder brother's explanation is that, in the command-line way, Java itself will load the system in the classpath specified in the package, and in the development environment such as eclipse, the system loads the development environment classpath. The explanation, which fits perfectly with my experiment, doesn't sound like a problem. To further confirm the difference between the Classpath loading content in both scenarios, Nancy advises me to use system.getproperties () to see what the difference is. When I compare the output of two situations, it bothers me the classpath problem of the day, thoroughly understand: the command line way, to Java XXXX, will load the system Classpath package, the way is Java.class.path= "", At the same time also through the sun.boot.class.path= "" way to load the JDK under the Jre\lib\ jar package, for the package of ext load, should be done through the Java.ext.dirs=w:\jdk\jre\lib\ext, However, there is no specific package name; When you execute a Java program from the IDE environment, you load the jar package specified by the environment.

PostScript

On the command line, by performing the batch processing of the loading environment first, and then starting Tomcat and the two system attributes that are directly initiated by Tomcat, compare the Java.class.path to see that the two are exactly the same, that is, in the current usage scenario, you want to load a jar package with a batch. It's not going to work. Further attempts to load the jar package with batch processing in a simple command-line manner, and then execute Java xxxx, trace the Java.class.path of the system properties to the exact same load as the batch request. On the other hand, Create your own application under Tomcat WebApp such as test, and then in the Web-inf directory of test, create a directory lib, put your jar bag here, or let Tomcat find the class you want to load. In the paper, put the jar in the Jre\lib\ Ext\ the system can run, the description is not a problem, in the use of Tomcat, it is recommended to put the Web-inf in the Lib directory. The obvious advantage of this is that it enhances the portability of the system.

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.