2

I know this question has been asked a lot, but I have tried a few suggestions and am still getting this error.

I am running the jar as follows:

java -jar MyJar-1.0.jar com.me.ldap.ActiveMain,

where my ActiveMain.java file looks like this:

package com.me.ldap;

public class ActiveMain {
    public static void main(String[] args) throws Exception {
    ...
    }
} 

I have also tried simply java -jar MyJar-1.0.jar with the same Error: Could not find or load main class error. I've also looked into the class path option but I don't think that applies.

I am creating it in Intellij as a Maven project. Maven > Lifecycle > package.

formicaman
  • 1,317
  • 3
  • 16
  • 32

2 Answers2

5

In order for:

java -jar myfile.jar

to work, there must be a manifest file in the jar file that points to a main class.

In order for you to specify the main class on the command line, you need to specify a classpath, not a jar file. Like:

java -cp myfile.jar com.me.ldap.ActiveMain

You are conflating these two things. Either create a manifest that specifies your main class and use the -jar switch, or simply use the -cp switch and specify your main class on the command line.

Doug Forrest
  • 486
  • 2
  • 6
1

Have you tried removing the signature files? I have seen this error pop up sometimes when due to signature files as documented in "Invalid signature file" when attempting to run a .jar and Generated JAR throws ClassNotFoundException for main class is also in the similar vein.

Sid
  • 420
  • 1
  • 6
  • 11
  • This should be a comment, not an answer. If it is a duplicate question, [vote to close](/help/privileges/close-questions) as such and/or leave a comment once you [earn](//meta.stackoverflow.com/q/146472) enough [reputation](/help/whats-reputation). If not, *tailor the answer to this specific question*. –  May 24 '22 at 21:31
  • I amended the answer to say try removing the signature files as a solution. – Sid May 24 '22 at 21:33