2011-05-18 50 views
1

我有一個實例化自定義jar文件中的類的問題。問題是包含這個類的項目能夠使用這個類,但是一旦我從項目中構建了一個jar並嘗試在另一個項目中使用這個jar,我就無法實例化它。無法從自定義類創建對象

例如,下面的代碼:

System.out.println("Reached here 1"); CustomClass customClass=new CustomClass(); System.out.println("Reached here 2");

豈不創建對象,並不會得到打印 「在這裏達到了2」。另一個問題是我不應該在這個類中處理異常,因爲我不打算改變早期的設計。

有什麼建議嗎?

+0

需要更多信息。在類路徑中是否包含CustomClass的jar?你也得到錯誤/例外? – Vinny 2011-05-18 21:54:14

回答

0

的問題是不是在.jar文件本身。這是在我的項目的設置。我應該將.jar文件包含在我沒有做的java目錄內的ext文件夾中。非常感謝大家的幫助。

1

這可能是有益的,至少看到什麼異常到底是不處理它:

try { 
    System.out.println("Reached here 1"); 
    CustomClass customClass=new CustomClass(); 
    System.out.println("Reached here 2"); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
    throw e; 
} 
+0

我試過這樣做,但沒有打印出來!雖然我確信發生異常.. – 2011-05-18 21:57:24

+0

可能是stderr丟失了:如何使用e.printStackTrace(System.out)來代替? – karmakaze 2011-05-18 22:10:41

1
try { 
    System.out.println("begin"); 
CustomClass customClass=new CustomClass();` 
} finally { 
    System.out.println("done"); 
} 

如果不打印「完成」,你的類初始化過程中掛起。那麼給我們更多。像類代碼:)

+0

不好意思打擾你們。問題是我不得不將新的.JAR包含在我的JDK目錄下的EXT文件夾下。 – 2011-05-18 23:18:38

0

假設你沒有得到錯誤信息(並且你沒有在你的代碼中做一些catch (Exception e) {}),因爲「到達這裏2」是從不打印的,我要檢查的一件事是構造函數CustomClass簡單地wait() s爲一些通知並且阻攔線程。

嘗試生成一個不同的線程它提供一個參考Thread.currentThread(),然後嘗試interrupt()這是一個超時後運行構造線程...