1

I am trying to compile my project which uses Gradle and Maven. The project use another library on my local maven repository. The library is referenced in Gradle and Maven:

Gradle:

dependencies{
    compile 'cf.charly1811.java.utils:MimeType:1.0'
}

Maven:

<!-- Dependencies -->
<dependencies>
    <dependency>
        <groupId>cf.charly1811.java.utils</groupId>
        <artifactId>MimeType</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

When I try compile the project using mvn clean package or gradle clean jar it success. The error arises when the MimeType class is called:

        Exception in thread "Thread-4" java.lang.NoClassDefFoundError: cf/charly1811/java/utils/MimeType at cf.charly1811.java.web.RequestHandlerThread.process(RequestHandlerThread.java:120)
            at cf.charly1811.java.web.RequestHandlerThread.handleRequest(RequestHandlerThread.java:85)
            at cf.charly1811.java.web.RequestHandlerThread.run(RequestHandlerThread.java:64)
    Caused by: java.lang.ClassNotFoundException: cf.charly1811.java.utils.MimeType
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 3 more

I don't know what causing this error... Please help.

EDIT: MimeType pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <name>MimeType</name>
    <description>Library to easily get the type of a file</description>
    <groupId>cf.charly1811.java.utils</groupId>
    <artifactId>MimeType</artifactId>
    <version>1.0</version>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
            <comments>A business-friendly OSS license</comments>
        </license>
    </licenses>
</project>

HttpServer (Main project) pom.xml

<project>
<modelVersion>4.0.0</modelVersion>

<name>HttpServer</name>
<description>A simple HTTP Server written in JAVA</description>

<groupId>cf.charly1811.java.web</groupId>
<artifactId>HttpServer</artifactId>
<version>1.0</version>

<!-- Dependencies -->
<dependencies>
    <dependency>
        <groupId>cf.charly1811.java.utils</groupId>
        <artifactId>MimeType</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

<!-- License -->
<licenses>
    <license>
        <name>Private use</name>
        <url>http://choosealicense.com/licenses/no-license/</url>
        <comments>This Software is for private use Only</comments>
    </license>
</licenses>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>cf.charly1811.java.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

EDIT I forgot to clarify 1 thing: I use IntelliJ IDEA. When I try to compile the project using "Run" It compile everything and I don't get this error. It only happen when I use Gradle or Maven to compile the project

Charles-Eugene Loubao
  • 1,080
  • 2
  • 12
  • 22
  • Please delete your manifest file and do a maven clean install. Pretty sure that the package is not imported in your manifest. If this problem continues then import the package manually and investigate as to why isn't it getting pulled in on its own. – StackFlowed Sep 10 '14 at 13:16
  • I deleted my Manifest file, Recompiled the MimeType library and recompiled my main project. Still getting the same error. I have imported manually the MimeTYpe library and tried again and now it's working. Maybe the problem is coming from the MimeType pom file? I updated the Question with the pom files of the Library and the project – Charles-Eugene Loubao Sep 10 '14 at 13:46
  • No. I think might be an issue with the build delete your local repo. delete the project from work space ... check it out again and build it. If problem persists then you need to investigate further.Are you using this import in groovy which acts weird in imports for the manifest. – StackFlowed Sep 10 '14 at 13:50
  • I deleted all Maven and Gradle files, Recreated the pom.xml for MimeType, recompiled, recreate pom for HttpServer, added MimeType as dependency. Same result... – Charles-Eugene Loubao Sep 10 '14 at 14:40
  • Try to add debugger loader unloader logs and figure out which jar is a problem. – StackFlowed Sep 10 '14 at 14:44
  • In run configuration you should see an option for tracing or debugging inside that enable osgi / loader – StackFlowed Sep 10 '14 at 14:50
  • I don't Use eclipse I use IntelliJ IDEA. And there's no osgi option – Charles-Eugene Loubao Sep 11 '14 at 07:20
  • Let me check in intellij and get back to you ... – StackFlowed Sep 11 '14 at 12:58
  • Sorry can't help much with intellij ... may be ask someone on intellij forums – StackFlowed Sep 11 '14 at 19:04
  • I found a solution which works. See my answer – Charles-Eugene Loubao Sep 11 '14 at 20:25

3 Answers3

0

try to do mvn clean install it will try to compile all the classes and try to save the generated jar of your project in the local repository if some problem occurs you should be able to resolve it otherwise post it will see

sachingupta
  • 709
  • 2
  • 9
  • 30
0

Please delete your manifest file and do a maven clean install. Pretty sure that the package is not imported in your manifest. If this problem continues then import the package manually and investigate as to why isn't it getting pulled in on its own.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

After reading this NoClassDefFoundError on Maven dependency I noticed the following:

"By default, Maven doesn't bundle dependencies in the JAR file it builds, and you're not providing them on the classpath when you're trying to execute your JAR file at the command-line. This is why the Java VM can't find the library class files when trying to execute your code."

I think it's the same thing for Gradle

Now I should find a way to bundle my dependencies.

EDIT I Finally found a solution (Gradle)

  1. List item

Create your build.gradle applythe following plugin:

apply plugin:'java'
apply plugin: 'application'
  1. Add your dependencies (See Java Plugin)
  2. Compile the project using this command

    gradle clean distZip

If you just want to run your program :

gradle clean run

EDIT

If you use Maven. you can use Apache Maven shade Plugin to bundle your dependencies in the Jar file

Community
  • 1
  • 1
Charles-Eugene Loubao
  • 1,080
  • 2
  • 12
  • 22