2017-08-23 165 views
1

好吧,我是新來的,掙扎着,所以請忍受我!ClassNotFound - 將第三方Jar添加到Maven

我試圖把它添加到現有的web應用程序

https://github.com/RusticiSoftware/TinCanJava

頁面上的說明建議建立與Maven的Jar,我已經做到了。然後我試過多種方法可以編譯成的Jar我的web應用項目,例如

添加作爲一個依賴

<dependency> 
     <groupId>tincan</groupId> 
     <artifactId>tincan</artifactId> 
     <version>1.0</version> 
     <scope>system</scope> 
     <systemPath>${project.basedir}/src/main/resources/tincan-1.1.1-SNAPSHOT-jar-with-dependencies.jar</systemPath> 
    </dependency> 

或安裝

mvn install:install-file -Dfile=C:\workspace\tincanJava\TinCanJava\target\tincan-1.1.1-SNAPSHOT-jar-with-dependencies.jar -DgroupId=com.rusticsoftware.tincan -DartifactId=tincan -Dversion=1.0 -Dpackaging=jar

我能看到的文件和在的IntelliJ類,所有的元素都出現了,但是當我運行該應用程序我得到以下錯誤:

Caused by: java.lang.ClassNotFoundException: com.rusticisoftware.tincan.StatementsQueryInterface 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:509) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 

我做錯了嗎?我是否應該將源代碼添加到我的目錄以及依賴關係到我的POM中並以這種方式構建?

回答

1

範圍system與範圍provided相同,表示該jar不包含在您的依賴項中。你必須在運行應用程序

當從documentation將其添加到類路徑:

System

This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

Provided

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive

+0

愚蠢的問題 - 罐子應該在哪裏住?在src目錄中是否有正確的位置? – user5839

+0

@ user5839沒有src只用於源文件。如果你使用maven,最好的地方是你的mvn回購。或者是類路徑中的任何文件夾 – Jens