2

Running out of ideas on the cause of the issue. I have a java jar (built from gradle under intellij if that matters).

  • Jar has a manifest.

  • Launching java -jar MyJar.jar from directory containing jar gives "could not find or load main class companyname.mainclass"

  • Java is reading from manifest as Editing the manifest file inside jar to mainclass2 will change the name in the error message

  • Manifest contains only version on a line, then main class attribute on a line, then 2 blank lines

  • All dependencies are in a /lib folder relative to jar so no classpath specified in manifest

  • Opening jar in zip file program shows folder companyname with file in it mainclass so the qualified name is reflected in jar structure. Capitalisation is also correct.

  • Manually specifying class name java -cp MyJar.jar companyname.mainclass gives the same class not found error

  • The class in question does have a main method, and does run fine under the IDE when unpackaged.

What am I missing (apart from more and more chunks of hair off my head)?

Edit Additional details: - using Java 8

jar structure:

-META-INF
  MANIFEST.MF
-companyname
  -module
    -interfaces
      interface1.class
    -commands
      baseCommand.class
      register.class
      specialCommand.class
    moduleSpec.class
    moduleProcessor.class
  mainclass.class
log4j.xml
springconfig1.xml
springconfig2.xml

Manifest file:

Manifest-Version: 1.0
Main-Class: companyname.mainclass
(newline char here)
(newline char here)
SeeMoreGain
  • 1,263
  • 16
  • 36

3 Answers3

2

First, make sure you're creating the executable jar correctly. See this answer for info on creating an executable jar using gradle.

If you're creating and packaging your jar correctly, it's possible that it can't load your main class because it has dependencies that cannot be found. You said:

All dependencies are in a /lib folder relative to jar so no classpath specified in manifest

If you mean that the lib folder is in the same folder on the filesystem as the .jar file, then your dependencies won't be found. The contents of that lib folder will not be automatically added to the jar's classpath. You can manually add them to the classpath of the java command, or you can add them to the manifest. If you add them to the classpath, you can use wildcards:

java -cp "MyJar.jar;lib/*" com.mycompany.MyMainClass

If you add them to the manifest, you can't use wildcards, so you need to specify each individual jar dependency under your lib directory. (See the Oracle tutorial on adding jars to the classpath for more info.)

If the lib directory is inside the jar (a "fat jar"), then there's no classpath that will help you: the default classloader will not load dependencies from within the executable jar. However, there are multiple tools that allow you to create a fat jar that includes all its dependencies, and distribute it as a single executable jar. Since you're using gradle, the gradle shadow plugin may be of use to you.

Community
  • 1
  • 1
Jason Hoetger
  • 7,543
  • 2
  • 16
  • 18
  • I had read the direct opposite somewhere about classpath and reading from /lib dir, hence the comment. Need to remember not to trust everything I read on the internet!!! – SeeMoreGain Sep 15 '16 at 03:31
0

The manifest will be of use here. Also, if you try

java -jar -verbose:class MyJar | grep mainclass 

It'll confirm if your class is being loaded. Oh - Also, are there a mix of JVMs at play here?

Ensure java -version shows a Java 8, since that's what you hint that Intellij/Gradle is building with.

Barry O'Neill
  • 435
  • 6
  • 16
  • So the pipe only showed the error message. Looking at entire verbose output, it is definitely loading java 8 jre. But not loading ANY of my classes. It loads java classes (all in .jar) then the error message appears and last 2 java libraries (`java.lang.Shutdown`) load. So any clues then? – SeeMoreGain Sep 15 '16 at 02:52
0

Cause can be in case if your main class has a static initialization block which throws some errors at runtime.

Sergey Bespalov
  • 1,746
  • 1
  • 12
  • 29
  • this is not the kind of answers the site is looking for, this is a comment at best and a poor one at that. –  Sep 16 '16 at 08:29