0

I'm having an issue that's been holding up my project for a few days now.

I'm new to Java and its ecosystem but all I'm trying to do is create an executable jar file, yet I keep getting an error message regarding the main class within META-INF/MANIFEST.MF. Ultimately I want to upload my Java application to a Docker image on GCP but before I do that I want to be able to execute my jar locally on my machine.

I've read through various posts on Stack Overflow and have gone through the Oracle documentation and nothing seems to work. Please don't close this as a duplicate as none of the solutions mentioned in the following posts have worked.

My process:

1) Create a Manifest.txt file with 2 lines of content. The first line sets my main class and the second line is blank but necessary according to Oracle.

Main-Class: com.ikea.us.test.MyClass

2) Create a jar file by running the following command in my terminal:

jar cfm app.jar Manifest.txt *

3) Inspect and verify the contents of my jar:

jar tf app.jar

The result:

META-INF/
META-INF/MANIFEST.MF
Dockerfile
Manifest.txt
build/
build/classes/
build/classes/java/
build/classes/java/main/
build/classes/java/main/com/
build/classes/java/main/com/ikea/
build/classes/java/main/com/ikea/us/
build/classes/java/main/com/ikea/us/test/
build/classes/java/main/com/ikea/us/test/MyClass.class
build/generated/
build/generated/sources/
build/generated/sources/annotationProcessor/
build/generated/sources/annotationProcessor/java/
build/generated/sources/annotationProcessor/java/main/
build/generated/sources/headers/
build/generated/sources/headers/java/
build/generated/sources/headers/java/main/
build/libs/
build/libs/test-1.0-SNAPSHOT.jar
build/resources/
build/resources/main/
build/resources/main/gcp-credentials.json
build/tmp/
build/tmp/compileJava/
build/tmp/compileJava/previous-compilation-data.bin
build/tmp/jar/
build/tmp/jar/MANIFEST.MF
build.gradle
cronjob.yaml
gradle/
gradle/wrapper/
gradle/wrapper/gradle-wrapper.jar
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
settings.gradle
src/
src/main/
src/main/java/
src/main/java/com/
src/main/java/com/ikea/
src/main/java/com/ikea/us/
src/main/java/com/ikea/us/test/
src/main/java/com/ikea/us/test/MyClass.java
src/main/resources/
src/test/
src/test/java/
src/test/resources/

4) Inspect and verify the content of META-INF/MANIFEST.MF:

unzip -p app.jar META-INF/MANIFEST.MF

The result:

Manifest-Version: 1.0
Main-Class: com.ikea.us.test.MyClass
Created-By: 19.0.2 (Oracle Corporation)

5) And finally, try and run the jar...

java -jar app.jar 

...where I get back an error message saying my main class can't be found.

Error: Could not find or load main class com.ikea.us.test.MyClass
Caused by: java.lang.ClassNotFoundException: com.ikea.us.test.MyClass

What am I doing wrong? This should be super simple yet nothing seems to work. I've tried various things inside of my manifest file:

Main-Class: com.ikea.us.test.MyClass.java
Main-Class: source.main.java.com.ikea.us.test.MyClass
Main-Class: source/main/java/com/ikea/us/test/MyClass

None of these worked.

Stack Overflow mentioned adding something to build.gradle and that didn't work either. The manifest file doesn't even include a Main-class line when I try this method,

plugins {
    id 'java'
}

jar {
    manifest {
        attributes 'Main-Class': 'com.ikea.us.test.MyClass'
    }
}

Any help would be greatly appreciated!

Darth_Zitro
  • 79
  • 2
  • 8

0 Answers0