maven spring mvc中的外部罐子

时间:2017-03-09 08:35:35

标签: spring maven spring-boot maven-2

您好我正在尝试使用 https://github.com/skrymer/qrbuilder

作为我项目中的外部依赖,所以我创建了一个Jar文件和qrbuilder-1.1.jar并将此jar添加到我的项目lib文件夹中,然后在pom.xml中添加了依赖项

<dependency>
    <groupId>com.my.app</groupId>
    <artifactId>qrbuilder</artifactId>
    <version>1.1</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/lib/qrbuilder-1.1.jar</systemPath>
</dependency>

添加,当我尝试使用给定的课程

    public  String generateQRCode(String data){
    QRCBuilder<BufferedImage> qrCodeBuilder = new ZXingQRCodeBuilder();

    String base64String = null; 
    try {
        qrCodeBuilder.newQRCode()
        .withSize(250, 250)
            .and()
        .withData(data)
            .and()
        .decorate(colorizeQRCode(Color.black.darker()))
            .and()
        .decorate(addImageOverlay(ImageIO.read(new File("src/main/resources/files/images/qr_logo.png")), 1f, 0.25f))
            .and()
        .doVerify(true)
        .toImage();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( (RenderedImage) qrCodeBuilder, "png", baos );
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        base64String= Base64.encodeBase64String(imageInByte);
        return base64String;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return base64String;
}

当我尝试使用此代码时,我发现类未找到异常

Caused by: java.lang.NoClassDefFoundError: com/skrymer/qrbuilder/QRCBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getDeclaredMethods(Class.java:1868)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 116 more

引起:java.lang.ClassNotFoundException:com.skrymer.qrbuilder.QRCBuilder     在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)     在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)     ......还有135个

在我的项目中添加jar时有什么问题,或者如何解决这个问题..

能够使用该类但在运行tomcat时遇到此错误。

2 个答案:

答案 0 :(得分:0)

正如您可以阅读maven范围的documentation

  

system除非您必须,否则此范围与提供的类似   提供明确包含它的JAR。工件总是如此   可用,不会在存储库中查找。

此范围内的库不会捆绑在人工制品中。您必须在运行时将其手动放入环境的类路径中。

答案 1 :(得分:0)

如果要将其用作依赖项,请使用以下命令将其添加到maven存储库:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

请参阅https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

或通过下载文件并将其添加到项目中将其添加到本地存储库。

<repositories>
      <repository>
         <id>localrepository</id>
         <url>file://${basedir}/repo</url>
      </repository>