2012-10-26 80 views
1

嘗試使用到Teradata的JDBC連接。我在Linux中編譯時加載了tdgssconfig.jar和terajdbc4.jar文件,並使用javac將它們添加到類路徑中。但在編譯時我仍然遇到ClassnotFoundException。Teradata和JDBC驅動程序 - classnotfoundexception ...但它的存在?

我在一段時間內沒有用過java,但是我已經在網上搜索過了,它看起來應該可以工作。

簡單代碼:

import java.sql.*; 
class TDtest { 
    public static void main(String[] args) {  
     System.out.println(classpath); 

     Class.forName("com.teradata.jdbc.TeraDriver"); 
    } 
} 

* .jar文件肯定是有:

[[email protected] java]# ls -l /home/user1/test/java/libs/* 
-rwxrwxrwx 1 user1 user1 2405 Oct 26 12:00 /home/user1/test/java/libs/tdgssconfig.jar 
-rwxrwxrwx 1 user1 user1 873860 Oct 26 12:00 /home/user1/test/java/libs/terajdbc4.jar 

詳細的錯誤日誌 - 它看起來像類路徑是正確的,我說:

javac -verbose -cp ".:/home/user1/test/java/libs/tdgssconfig.jar:/home/user1/test/java/libs/terajdbc4.jar" TDtest.java 
[parsing started TDtest.java] 
[parsing completed 21ms] 
[search path for source files: .,/home/user1/test/java/libs/tdgssconfig.jar,/home/user1/test/java/libs/terajdbc4.jar] 
[search path for class files: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/resources.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/rt.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jce.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/charsets.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/classes,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/dnsns.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/gnome-java-bridge.jar,/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/sunjce_provider.jar,.,/home/user1/test/java/libs/tdgssconfig.jar,/home/user1/test/java/libs/terajdbc4.jar] 
[loading java/lang/Object.class(java/lang:Object.class)] 
[loading java/lang/String.class(java/lang:String.class)] 
[checking TDtest] 
[loading java/lang/Class.class(java/lang:Class.class)] 
[loading java/lang/Error.class(java/lang:Error.class)] 
[loading java/lang/ClassNotFoundException.class(java/lang:ClassNotFoundException.class)] 
[loading java/lang/Exception.class(java/lang:Exception.class)] 
[loading java/lang/Throwable.class(java/lang:Throwable.class)] 
[loading java/lang/RuntimeException.class(java/lang:RuntimeException.class)] 
TDtest.java:4: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown 
     Class.forName("com.teradata.jdbc.TeraDriver"); 

我已經嘗試瞭解Jdbc jar的jar,他們肯定有com/teradata/jdbc/TeraDriver.class。

我不知所措。任何想法我做錯了什麼?

回答

2

編譯器沒有從你的jar文件中尋找類com.teradata.jdbc.TeraDriver,它是對Class.forName()語句作出反應。

由於Class.forName()拋出ClassNotFoundException這是一個checked exception,您將需要處理它。

你既可以圍繞異常的'的try/catch」塊或拋出異常編譯:

class TDtest { 
    public static void main(String[] args) {  
     System.out.println(classpath); 

     try { 
     Class.forName("com.teradata.jdbc.TeraDriver"); 
     } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
     // more error handling.. 
     } 
    } 
} 
+0

非常感謝!自從我完成java之後,它已經很久了。 – rfusca

0

問題不在於你的類路徑。只是你想要用try/catch塊封裝你的Class.forName行並且捕獲檢查到的異常ClassNotFoundException

0

試試這個

try { 
    Class.forName("com.teradata.jdbc.TeraDriver").newInstance(); 

    } catch (Exception e) { 
    e.printStackTrace(); 

    } 
0

我得到這個

Exception in thread "main" java.lang.ClassNotFoundException: 
com.ncr.teradata.TeraDriver 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:191) 
at com.ebay.quality.bp.Z_Query_Teradata.main(Z_Query_Teradata.java:15) 

當我在Dependency Hierarchy選項卡下檢查我的pom.xml文件時,發現我測試了依賴關係。

<dependency> 
    <groupId>com.thirdparty.teradata</groupId> 
    <artifactId>terajdbc4</artifactId> 
    <version>14.10.00.17</version> 
    <scope>test</scope> 
</dependency> 

我刪除了行與測試範圍,它的工作。