對Servlet容器的補充和一個問題的請教

【0】READMEhtml

0.1)本文是對 一個servlet容器  的補充;java

0.2)發這個博文的最終目的是爲了請教各位前輩,幫我解決一個問題,問題描述在文末, 謝謝;web

 

【1】Servlet容器apache

1.1)經過一個簡單的servlet容器這篇博文,咱們看到:其中的核心代碼是 類加載器, 然而,在我follow 其代碼,分別在命令行 和 Eclipse 執行時,獲得了不一樣的執行結果; 參見個人提問 對於URLClassLoader,Eclipse執行正確,而命令行執行拋出異常 這個問題描述;url

1.2)拋出異常的緣由: 顯然是 類加載器沒有找到要加載類所在的dir, 然而,正如你所見,我在 URLClassLoader的源代碼中已經 指定了加載路徑;spa

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. try {  
  2.       // create a URLClassLoader, 建立類載入器(類加載器是乾貨代碼 )  
  3.       URL[] urls = new URL[1];  
  4.       URLStreamHandler streamHandler = null;  
  5.       File classPath = new File(Constants.WEB_ROOT);  
  6.       // the forming of repository is taken from the createClassLoader method in  
  7.       // org.apache.catalina.startup.ClassLoaderFactory  
  8.       String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;  
  9.       // file:E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\webroot\  
  10.       // the code for forming the URL is taken from the addRepository method in  
  11.       // org.apache.catalina.loader.StandardClassLoader class.  
  12.       urls[0] = new URL(null, repository, streamHandler);  
  13.       // urls[0] = file:E:/bench-cluster/cloud-data-preprocess/HowTomcatWorks/webroot/  
  14.       loader = new URLClassLoader(urls);  
  15.     }  

 

1.3)解決方法:正如你在 「一個簡單的servlet容器」 這篇博文中所見,我須要再 命令行運行該程序時,手動添加 類加載路徑到 classpath, 這樣才能執行成功,不然失敗。.net

 

Attention)我納悶的地方在於:明明在上述代碼中,我已經在 URLClassLoader的類構造器中指明瞭 類加載路徑,爲何我還要在命令行手動設置類加載路徑到 classpath,那豈不是我在URLClassLoader 中指明的 類加載路徑是 invalid ? 也就是上述我在技術問題中所提出的問題,可是這個問題(對於URLClassLoader,Eclipse執行正確,而命令行執行拋出異常 )一直沒有人來解答。命令行

相關文章
相關標籤/搜索