0

I use java 1.8. The pom file contains

    ...
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>5.11.0</version>
    </dependency>

</dependencies>

I use it this way

String username = Advapi32Util.getUserName();

When I finaly build a runnable jar (which includes all the dependencies except the jna-platform) it can't find a required class, so the resulting jar produses the error

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/platform/win32/Advapi32Util
        at main.TestServer.main(TestServer.java:44)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.platform.win32.Advapi32Util
        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)
        ... 1 more

The way I build a resulting jar: in Intellij IDEA -> Build -> Build Artifacts -> Build (action).

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.thedeveloper</groupId>
    <artifactId>testserver</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.java.version>1.8</project.java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt</artifactId>
            <version>1.9.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>3.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.12.10</version>
        </dependency>

        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.11.0</version>
        </dependency>

        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
            <version>5.11.0</version>
        </dependency>

    </dependencies>
    
    <build>
        <sourceDirectory>src</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${project.java.version}</source>
                    <target>${project.java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>

                    <excludes>
                        <exclude>setup/*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>

    </build>
</project>

What did I miss?

rozerro
  • 3,982
  • 7
  • 30
  • 66
  • 2
    Without seeing the rest of your POM, we have no way of knowing. – chrylis -cautiouslyoptimistic- May 22 '22 at 07:11
  • 2
    Unless you're producing a fat jar / uber jar, you don't expect the jna-platform.jar to be packed inside a regular runnable jar, do you? – Kayaman May 22 '22 at 07:13
  • @Kayaman I do produce a jar with dependencies. – rozerro May 22 '22 at 07:21
  • 1
    *"I do produce a jar with dependencies."* - Show us how you do that, because clearly that is where the problem is. – Stephen C May 22 '22 at 07:39
  • I mean ... show us the part of the POM file that specifies how to build the JAR file. Or if you don't know, show us the entire POM file. – Stephen C May 22 '22 at 07:46
  • 3
    OK. So that POM file **does not** generate a fat jar / uber jar. It generates a JAR without the dependencies embedded in it. If you want to generate a JAR that includes the dependencies, read this: https://stackoverflow.com/questions/574594 OR https://stackoverflow.com/questions/16222748 – Stephen C May 22 '22 at 07:54
  • I would strongly recommend to follow conventions and not to changed things like `src`... why have you done that? To create a ueber etc or alike you should use the [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin/) to produce an ueber jar which contains all the dependencies...Why have configurate maven-compiler-plugin with this: `setup/*` ? – khmarbaise May 22 '22 at 08:31
  • @khmarbaise it will not include `setup` in jar when you generate it in eclipse – rozerro May 25 '22 at 09:04

0 Answers0