1

I am using maven with netbeans. Whenever I run the program it gives me java.lang.NoClassDefFoundError exception even after adding javaee-api 8.0 to dependencies.

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>example</groupId>
    <artifactId>JavaApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
            <debug>false</debug>
        </configuration>
        </plugin>
    </plugins>
    </build>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>12</maven.compiler.source>
    <maven.compiler.target>12</maven.compiler.target>
    </properties>
    <dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
    </dependencies>
</project>

I can see the javax.json.bind.* in Dependencies. I don't get why am I not able to run the program. Any pointers?

  • please add which class exactly you got `NoClassDefFoundError` for – star67 Mar 29 '19 at 16:16
  • try to add ` javax.json javax.json-api 1.1.4 ` as dependency – Mohsen Mar 29 '19 at 16:17
  • @Spara I have even downloaded the source code along with binaries of javaee 8 and I can see `javax.json.bind.JsonbBuilder` in dependencies. My point is, why do I get the error even after adding dependency to `javaee-api`? –  Mar 29 '19 at 16:19
  • @star67 `javax.json.bind.JsonbBuilder` –  Mar 29 '19 at 16:20
  • you add javaee-api dependecy in the **provided** scope which is not adding library in runtime to your libs. so you need to add the dependency which I said in the previous comment which is adding in the compile scope which is the default one – Mohsen Mar 29 '19 at 16:21
  • @Spara I had to add `javax.json`, `javax.json.bind`, `org.eclipse.yasson` and `org.glassfish.javax.json` in order to make it work. –  Mar 29 '19 at 16:27
  • @Spara It will work for sure. Can I just use `javaee` with some other scope to make it work? –  Mar 29 '19 at 16:28
  • Hello, with Netbeans, have you created an enterprise application or a web application or just a java appliction? – Ismail Mar 29 '19 at 16:28
  • @M.Ismail Just a java app using maven. –  Mar 29 '19 at 16:29
  • 1
    Let me describe it for you as an answer – Mohsen Mar 29 '19 at 16:32
  • try delete the scope provided in the pom.xml file. – Ismail Mar 29 '19 at 16:35
  • @Spara Sure :)) –  Mar 29 '19 at 16:37

1 Answers1

2

The problem you face is that you need to add libraries which you use in your project manually. As you said javaee-api has dependency to javax.json.bind.* but the scope of that is provided which means that it will provide by your jdk or a container (application server for example) in the runtime( for more information about scopes check this). So you need to add libraries you need as compile scope(the default scope) which you need them in runtime.

Ismail
  • 2,322
  • 1
  • 12
  • 26
Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • One last question. Why can't I add `compile` scope to `javaee-api` and get it to work? –  Mar 29 '19 at 16:41
  • Because the default value of scope is compile, so it is added by default. – Ismail Mar 29 '19 at 16:41
  • @baba It will add lots of library to your libs which is not good and also they should provide by your container. – Mohsen Mar 29 '19 at 16:43