1

I've been looking at several different threads, and found no solution. I have made a project, which retrives data from google sheets, using java and gradle. I have created an artifact and built the jar file through intelliJ. When i try to run the jar through the terminal using "java -jar filename.jar" i get "no main manifest attribute, in filename.jar"

My MANIFEST.MF is located in main/java/META-INF and has the following simple structure

Manifest-Version: 1.0
Main-Class: Main

The build.gradle file has the following structure:

plugins {
    id 'java'
}

group  'filename'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.api-client:google-api-client:1.30.4'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.30.4'
    compile 'com.google.apis:google-api-services-sheets:v4-rev581-1.25.0'
}

Clearly i have stated in what class the main method is, and this is why this errormessage, makes no sense to me. Does anyone have a solution for this?

  • Do you see this at the root of your jar -> META-INF/MANIFEST.MF (after unzip) – CodeScale Apr 15 '20 at 18:28
  • yes i do. It contains loads of information. Nothing about the main class though – Christian Munklinde Leth-Espen Apr 15 '20 at 18:57
  • You define something like this in your gradle file ? ```jar { manifest { attributes( 'Main-Class': 'Main' ) } } ``` ? – CodeScale Apr 15 '20 at 19:20
  • If i am correct. This creates another jar.file when run right? am i supposed to use this jar file, and not the one i create with artifacts through intelliJ? I do have that exact in my build.gradle file yes. The jar file created with that command, does however not contain any of the dependencies or anything like that. It contains only the files in my project, and the manifest file – Christian Munklinde Leth-Espen Apr 15 '20 at 19:30

1 Answers1

1

I have followed the steps from the Sheets API quickstart to set up the application. After that, I followed the steps from this other answer to generate the .jar file, that explains that you should declare your manifest file inside your build.gradle file within the jar property.

After this, when running the generated .jar file I got an “Invalid signature file” error which I was able to fix by adding the below line in the jar property as explained here:

exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'

After generating the .jar file you can find it in the build/libs directory of your project. My end build.gradle file looks like this:

apply plugin: 'java'
apply plugin: 'application'

group 'egs'
version '1.0-SNAPSHOT'

mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.api-client:google-api-client:1.30.4'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.30.4'
    compile 'com.google.apis:google-api-services-sheets:v4-rev581-1.25.0'
}

jar {
    manifest {
        attributes 'Main-Class': 'SheetsQuickstart'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
Andres Duarte
  • 3,166
  • 1
  • 7
  • 14