Java项目引入第三方Jar包
2015-09-22 11:14
435 查看
普通java Project 引入jar包:
1,copy jar to lib folder.
2, imported with Build path -> Add external jars, and it worked perfectly.
Java Web Project 引入jar包:
1,copy jar to WebContent/WEB-INF/lib/
2,right click jar Package Build path -> Add external jars。
说明:
http://stackoverflow.com/questions/30384473/how-to-import-jdbc-driver-into-dynamic-web-project
There are two ways to use and reference a jar file in an eclipse project.
One is at compile time and for compilation purposes. To make your project compile, you need to add your required libraries in the classpath. In eclipse, right click to your project, hover on 'Build Path', then select 'Configure Build Path'. In the dialog go to 'Libraries' tab and there you can see which jars/libraries you have. If you need to add more, you can use the buttons at the right side of the dialog. There you should select 'Add external jars' and select the MySql JDBC Driver from your file system.
The other one is at run time. This is when you deploy your web application to an application server. Now everytime your application needs to load a class from an external jar, it will look for the jar in the application server's class loader. The classloader conatins the paths to the available jar files in your application server, in configured resources and in your deployed application in the WEB-INF/lib/ folder. You can configure which place the classloader will check first.
In your very specific case, you need to add the MySQL JDBC Driver in any of classloader paths (since I asume your project compiles already) so you can either add the jar to Tomcat's /lib directory or to your application's /WEB-INF/lib/ directory. After that just redeploy or restart tomcat and you should be able to use MySQL JDBC connections.
UPDATE:
Also, when using a DriverManager interface to create a JDBC Connection, remember to always create an instance of your JDBC driver first in order to load it into your Classloader. You can see this in the MySQL JDBC Driver documentation. Ej:
Call this line before using
1,copy jar to lib folder.
2, imported with Build path -> Add external jars, and it worked perfectly.
Java Web Project 引入jar包:
1,copy jar to WebContent/WEB-INF/lib/
2,right click jar Package Build path -> Add external jars。
说明:
http://stackoverflow.com/questions/30384473/how-to-import-jdbc-driver-into-dynamic-web-project
There are two ways to use and reference a jar file in an eclipse project.
One is at compile time and for compilation purposes. To make your project compile, you need to add your required libraries in the classpath. In eclipse, right click to your project, hover on 'Build Path', then select 'Configure Build Path'. In the dialog go to 'Libraries' tab and there you can see which jars/libraries you have. If you need to add more, you can use the buttons at the right side of the dialog. There you should select 'Add external jars' and select the MySql JDBC Driver from your file system.
The other one is at run time. This is when you deploy your web application to an application server. Now everytime your application needs to load a class from an external jar, it will look for the jar in the application server's class loader. The classloader conatins the paths to the available jar files in your application server, in configured resources and in your deployed application in the WEB-INF/lib/ folder. You can configure which place the classloader will check first.
In your very specific case, you need to add the MySQL JDBC Driver in any of classloader paths (since I asume your project compiles already) so you can either add the jar to Tomcat's /lib directory or to your application's /WEB-INF/lib/ directory. After that just redeploy or restart tomcat and you should be able to use MySQL JDBC connections.
UPDATE:
Also, when using a DriverManager interface to create a JDBC Connection, remember to always create an instance of your JDBC driver first in order to load it into your Classloader. You can see this in the MySQL JDBC Driver documentation. Ej:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Call this line before using
DriverManager.getConnection(...)and you should now be able to create and use your JDBC Connections.
相关文章推荐
- java四种线程池的使用,觉得写得不错,值得借鉴
- maven3常用命令、java项目搭建、web项目搭建详细图解
- eclipse 与 Android Studio 获得数字签名的方法
- java 序列化
- JAVA学习之枚举
- JAVA常用jar包简介
- Java拿到工程的绝对路径相关问题-maven-resources-plugin
- POI读取excel文件通用代码(增强版--支持返回javaBean集合)---2003,2007
- Java POI 操作excel 无硬编码,反射,史上最牛
- java jsp+servlet实现登录网页设计
- java打开程序或文件
- Java操作Excel之理解JXL--读取Excel
- java 汉字转换英文小写工具类
- Java操作Excel之理解JXL--读取Excel模板动态写入数据并生成Excel
- Java中如何读写cookie
- spring-MVC配置问题
- mybatis整合spring配置
- JDK报错:Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
- [Ubuntu] install java
- Java中的copyOf()