运行jar时Gradle NoClassDefFoundError

时间:2017-06-01 10:54:50

标签: java intellij-idea gradle velocity noclassdeffounderror


我正在尝试设置一个带有一些Velocity功能的Gradle项目。

到目前为止,我有以下文件:

的src /主/ JAVA / COM / veltes / velotest.java:

package com.veltes;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.*;

public class velotest {
    public static void main(String[] args) {

        try {
            VelocityEngine ve = new VelocityEngine();
            ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
            ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
            ve.init();
            VelocityContext context = new VelocityContext();
            context.put("name", "World");
            Template t = ve.getTemplate("com/veltes/velotest.vm");
            StringWriter writer = new StringWriter();
            t.merge(context, writer);
            System.out.println(writer.toString());

            File logFile = new File("C:/users/xxxx/Desktop/velotest.html");

            try {
                writeFile(logFile, t, context);
            }
            catch (IOException io) {
            }
        } catch (Exception e) {
        }

    }

    private static void writeFile(File logFile, Template t, VelocityContext context) throws IOException {
        Writer logWriter;

        logWriter = new BufferedWriter(new FileWriter(logFile));
        try {
            t.merge(context, logWriter);
        }
        catch (ResourceNotFoundException rnfe) {
        }
        catch (ParseErrorException pee) {
        }
        catch (MethodInvocationException mie) {
        }
        catch (Exception e) {
        }
        logWriter.flush();
        logWriter.close();
    }

}

的build.gradle:

group 'velocitytest'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'
apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.11'

    compile 'velocity:velocity:1.4'
}


现在,当我运行gradle assemblegradle build时一切都很好,但是当我尝试运行项目时(同样用于在build / libs /中运行构建的jar以及在IntelliJ中运行velotest类),我收到以下错误:

  

线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / collections / ExtendedProperties
      在org.apache.velocity.runtime.RuntimeInstance。< init>(RuntimeInstance.java:183)
      在org.apache.velocity.app.VelocityEngine。(VelocityEngine.java:60)       在com.veltes.velotest.main(velotest.java:23)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      在java.lang.reflect.Method.invoke(Method.java:497)
      在com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  引起:java.lang.ClassNotFoundException:org.apache.commons.collections.ExtendedProperties
      在java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      在java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)
      在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 8更多

有点奇怪的是,build / tmp /
中没有jar 你们中有谁知道解决方案吗?

1 个答案:

答案 0 :(得分:1)

如果您希望能够运行它,则需要创建一个可运行的jar。

您可以使用shadojar插件或扩展jar任务以将运行时deps打包到工件中。

jar {
    archiveName = 'Name.jar'

    manifest {
        attributes 'Main-Class': 'your.main.class',
                'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
                'Implementation-Version': project.version
    }

    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {}
}

对于intelliJ问题:

apply plugin: 'idea'

然后运行gradle idea任务,这将刷新项目中的.iws .ipr .iml文件并同步类路径。或者,如果您使用intelliJ支持(这还不是理想的),请尝试在那里刷新它。我认为在2017.1.3版本中,gradle集成要好一些。