0

I'm trying to build a jar from my Maven project using IntelliJ, but i get Error: Could not find or load main class when running the jar.

I have 3 classes (one contains my main method and a simple print, the 2 others are empty). I have a pom file with one dependency:

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>

                        <mainClass>MyMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>

<repositories>
    <repository>
        <id>red-hat</id>
        <url>https://maven.repository.redhat.com/earlyaccess/all/</url>
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>org.apache.beam</groupId>
        <artifactId>beam-sdks-java-io-google-cloud-platform</artifactId>
        <version>2.22.0</version>
    </dependency>
</dependencies>

I've tried getting help from here, here and here, but nothing seems to help. I'm most likely missing some basic understanding of how the jar is being built. This is the project structure. Any help would be appreciated.

This is my project structure

artofdoe
  • 167
  • 2
  • 14
  • Do you build the jar using Maven or using IntelliJ IDEA artifact? What's inside the final jar, do you see the classes and the correct manifest file there? – CrazyCoder Jul 13 '20 at 18:36
  • i use IntelliJ artifact. inpsecting the JAR with `jar tf .\KafkaToBigQueryJAR.jar` I can see the manifast is there along with the class files `META-INF/MANIFEST.MF KafkaToBigQuery.class KafkaToBigQueryOptions.class META-INF/ Utils.class` – artofdoe Jul 13 '20 at 19:25
  • What's inside `META-INF/MANIFEST.MF`? How did you configure the artifact? See https://www.jetbrains.com/help/idea/artifacts.html. You may need to specify the location of the existing manifest file or specify the main class in the automatically generated manifest. Since you build the artifact with IntelliJ IDEA, your Maven configuration is not relevant at all and your question tags with the pom.xml contents just confuse the users trying to help you here. To build the artifact with Maven run the `package` goal in the Maven tool window, it will produce a different jar that may work for you. – CrazyCoder Jul 13 '20 at 19:28
  • thanks for the reply. I can create a simple jar that works when i don't have any maven dependency. i get the error as soon as i add a dependency in my pom, so i figured it was maven related too – artofdoe Jul 13 '20 at 19:40
  • See https://stackoverflow.com/a/42200519/104891 and https://stackoverflow.com/a/45169655/104891. Jar dependencies with digital signatures will break the final artifact. – CrazyCoder Jul 13 '20 at 19:40

1 Answers1

0

simply substitute <mainClass>MyMainClass</mainClass> with the actual main class from the classpath.

Gewure
  • 1,208
  • 18
  • 31
  • that's what i have already. i've just masked it. I've tried `KafkaToBigQuery`, `target.classes.KafkaToBigQuery` and `classes.KafkaToBigQuery` didn't change anything – artofdoe Jul 13 '20 at 18:04
  • your masking makes it very hard to answer the question. try YourPackageName.KafkaToBigQuery – Gewure Jul 13 '20 at 18:07
  • i've tried `main.java.KafkaToBigQuery` and the ones i mentioned in the previous comment. My package structure is just the default – artofdoe Jul 13 '20 at 18:19
  • common packagenames are like..: com.myproject.app – Gewure Jul 13 '20 at 18:26