1

I have my program's jar packaged with maven. I have the manifest file set to know where my main class is. It executes with no issues on my machine. However, when I gave the jar to a friend to beta test, they are getting the error that the main class can't be found. We are running the same version of JRE

mine: enter image description here

theirs: enter image description here

which was the suggested issue in other stack overflow answers I've seen for this.

I've unzipped the jar to make sure the manifest file was actually setting the location of the main class correctly, and it is. So what could be the issue here?

edit: relevant portion of pom.xml for building the manifest

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.dreadylocks.MyCloset.MyCloset</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

When I unzip the jar, these are the contents of the META-INF/MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: saman
Build-Jdk: 1.8.0_251
Main-Class: com.dreadylocks.MyCloset.MyCloset

project structure:

enter image description here

ssteph
  • 89
  • 1
  • 9
  • My package is com.dreadylocks.MyCloset and main is in a class (.java file) called MyCloset – ssteph Jul 03 '20 at 19:25
  • I'm thinking your package is perhaps just com.dreadylocks. You wouldn't include the class name as part of the package name. – Woodchuck Jul 03 '20 at 19:26

2 Answers2

0

You need to configure the maven-jar-plugin in your pom.xml. This plugin is responsible for packaging and creating the manifest.MF.

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            ...
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <mainClass>your.package.MainClass</mainClass>
                </manifest>
              </archive>
            </configuration>
            ...
          </plugin>
        </plugins>
      </build>

Katy
  • 1,023
  • 7
  • 19
  • 1
    I have nearly the same, I edited my OP with the contents of the build portion of my pom.xml and the contents of the manifest.MF. I will try adding the classpath and see. – ssteph Jul 03 '20 at 19:21
0

Your maven-assembly-plugin configuration looks good with the exception that the mainClass package name looks strange. Typically you would not include your class name as part of your package.

I'd suggest removing the last ".MyCloset" from your mainClass package:

...
<manifest>
    <mainClass>com.dreadylocks.MyCloset</mainClass>
</manifest>
...

That is, unless you really do have a directory named MyCloset that contains your MyCloset class. Either way, there could be a discrepancy between where or how you and your friend are running the jar.

If you are both running using java -jar /path/to/jar/myjar.jar, perhaps you are running from com/mydreadylocks/MyCloset while they are running from com/mydreadylocks.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
  • That doesn't work. MyCloset is the package name but it's also the name of the class with my main method. If I remove the second MyCloset it doesn't work even on my machine. – ssteph Jul 03 '20 at 19:32
  • Is your MyCloset in a directory called MyCloset? If so, that's very unconventional practice. I would move MyCloset into the dreadylocks as well. See my edit above. – Woodchuck Jul 03 '20 at 19:34
  • I edited to show my project structure. I get that's unconventional (I'm new to java so naming packages isn't something I've learned best practices for), but could it really be causing this issue? – ssteph Jul 03 '20 at 19:37
  • No problem, but it could possibly account for why it works for you and not someone else if they are running from com/mydreadylocks while you are running from com/mydreadylocks/MyCloset. Maybe have them send you a screenshot like you posted above and see if it matches. – Woodchuck Jul 03 '20 at 19:38
  • in reply to the edit about the jar location: I thought a jar was standalone. I packaged up all the dependencies so it could run by double-click with only jre installed. I have tried moving the jar around to a bunch of different directory locations on my pc, and they all run fine. It's just on hers that's the issue. – ssteph Jul 03 '20 at 19:39
  • Ah, true. But another way to run a jar is with the command: java -jar c:\path\to\jarfile\myjar.jar My comment would apply if they are running in that way. – Woodchuck Jul 03 '20 at 19:45
  • I've checked the structure inside the jar using 7zip, the classes are all nested correctly in the folders com/dreadylocks/MyCloset/[all of my classes are here] – ssteph Jul 03 '20 at 19:45
  • Interesting. Sorry, I'm stumped – Woodchuck Jul 03 '20 at 20:12
  • yes. So I got it working for them. They had to add `-jar` when running the jar file from command line. Weirdly, when *I* run it using `-jar` as an additional argument, I get the main class not found error. I too am stumped, but at least it's a workaround. – ssteph Jul 03 '20 at 20:22
  • Hm yea I wouldn't have expected it would work without the -jar argument from the command line. – Woodchuck Jul 03 '20 at 20:25
  • So were they able to run it by double clicking as you had described? – Woodchuck Jul 03 '20 at 20:28
  • only by creating a shortcut so I could add `-jar` to the target. I do think there's a way to do that when you do the 'open with' file association thing, but I haven't gotten to googling it yet. – ssteph Jul 03 '20 at 21:32