0

I can't seem to solve this issue. Running com.benmyers.Main in IntelliJ with a Maven dependency works fine, and I receive no errors. When I use the Install tool in the Maven window to create an executable JAR file in the target directory, I receive the following error when attempting to run

java -jar "C:\Users\...\target\AncientGreekTranslator-1.0-SNAPSHOT.jar"

in Command Prompt:

Exception in thread "main" java.lang.NoClassDefFoundError: com/formdev/flatlaf/FlatDarculaLaf
        at com.benmyers.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException: com.formdev.flatlaf.FlatDarculaLaf
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

I believe there is an issue with the Maven plugin in 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>benlmyers.ancientGreekTranslator</groupId>
    <artifactId>AncientGreekTranslator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>0.43</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

I have tried the following:

  1. Adding <scope>provided</scope> to the dependency in pom.xml (from this answer), which causes no visible change when running the JAR in Command Prompt, and

  2. using <artifactId>maven-plugins</artifactId> instead of <artifactId>maven-jar-plugin</artifactId> among other artifactIds. Basically, I tried the answers from this question and this other question but had no success.

I've received a comment requesting for my Main.java:

public class Main {

    private static GreekDictionaryInterpreter dictionaryInterpreter;
    private static Translator translator;

    private static MainForm mainForm;

    public static void main(String[] args) {

        dictionaryInterpreter = new GreekDictionaryInterpreter(new ArrayList<Flag>());
        translator = new GreekTranslator(dictionaryInterpreter.getDictionary(), new ArrayList<Flag>());

        FlatDarculaLaf.install(); // <- Line that causes exception

        //LookAndFeelManager.setLookAndFeel();

        MainForm.main(dictionaryInterpreter, translator);
    }
}

There are other answers for this, and I've searched through several and none of them have proven to be successful in my case. Any help would be greatly appreciated.

Ben Myers
  • 1,173
  • 1
  • 8
  • 25
  • can you share your main.java class. if not just try https://stackoverflow.com/a/25011152/2137378. There are different types of reasons that can result noclassdeffounderror, but I have usually face it when there is a problem in loading Static block of my classes. Double check if there is any buggy or exception likely line in the Static part of your Main.java. Good luck – Amin Heydari Alashti Nov 17 '20 at 06:05
  • Can you extract the ```AncientGreekTranslator-1.0-SNAPSHOT.jar``` and check if the ```com.formdev``` jar really exist inside? – prostý člověk Nov 17 '20 at 06:08
  • Thank you for the suggestion, @tgdavies . Sadly, the line `maven-assembly-plugin` in that solution gives me the compiler error `"Plugin maven-assembly-plugin not found"`. – Ben Myers Nov 17 '20 at 06:12
  • I have edited my question to include `Main.java`, @epcpu . – Ben Myers Nov 17 '20 at 06:14
  • @ThangavelLoganathan When I extract the JAR and look inside `\com`, all I see is two directories: `benmyers` and `intellij`. I can imagine that it is supposed to be in there? – Ben Myers Nov 17 '20 at 06:15
  • Hmmm - works for me. You can try explicitly setting a version for the plugin in the pluginManagement section of your pom. See http://maven.apache.org/plugins/maven-assembly-plugin/usage.html for general docs. – tgdavies Nov 17 '20 at 06:21
  • Just out of interest, what does the manifest file contain? – Vlad L Nov 17 '20 at 08:36

1 Answers1

-1

In IDEA, you should add the dependency to your project Artifacts File -> Project Structure -> Artifacts add someone you want to Output Layout And then pack it.

YuHao Zhu
  • 44
  • 3
  • The OP is not having a problem in IDEA, it's the jar produced via maven which is the problem. – tgdavies Nov 17 '20 at 06:01
  • Commonly, idea will not package third party dependence into jar, Manual add dependence to jar is required, I've had the same problem When I use Idea from Eclipse – YuHao Zhu Nov 17 '20 at 06:28
  • Thank you for your answer, @YuHaoZhu . I went to Project Structure > Artifacts, but I don't see how this can help. – Ben Myers Nov 17 '20 at 06:32