简体   繁体   English

运行jar时的Gradle NoClassDefFoundError

[英]Gradle NoClassDefFoundError when running jar


I'm trying to set up a Gradle project with some Velocity functions in it.我正在尝试建立一个 Gradle 项目,其中包含一些 Velocity 功能。

So far I have the following files:到目前为止,我有以下文件:

src/main/java/com/veltes/velotest.java: src/main/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:构建.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'
}


Now, when I run gradle assemble and gradle build everything is fine, but when I try to run the project (same for running the built jar in build/libs/ and for running the velotest class in IntelliJ), I get the following error:现在,当我运行gradle assemblegradle build一切都很好,但是当我尝试运行项目时(在 build/libs/ 中运行构建的 jar 和在 IntelliJ 中运行 velotest 类也是如此),我收到以下错误:

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

It's a bit strange that there is no jar in build/tmp/ build/tmp/里面没有jar有点奇怪

Does anyone of you knows a solution?你们中有人知道解决方案吗?

You need to create a runnable jar if you want to be able to run it. 如果您希望能够运行它,则需要创建一个可运行的jar。

You can use shadojar plugin or extend the jar task to pack the runtime deps into an artifact. 您可以使用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) }) {}
}

For intelliJ problem: 对于智能问题:

apply plugin: 'idea'

Then run gradle idea task, this will refresh .iws .ipr .iml files in your project and sync the classpaths. 然后运行gradle idea任务,这将刷新项目中的.iws .ipr .iml文件并同步类路径。 Or if you use intelliJ support (which is not yet ideal) try to refresh it there. 或者,如果您使用智能支持(尚不理想),请尝试在那里进行刷新。 I think in version 2017.1.3 the gradle integration is a bit better. 我认为在2017.1.3版中,gradle集成要好一些。

Adding from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {} to build.gradle file fixed it for me, like this:from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {}添加到build.gradle文件为我修复了它,如下所示:

jar {
    manifest {
        attributes(
            'Main-Class': 'gradle22.Library'
        )
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM