2
votes

I have a Intellij Gradle project for HelloWorld. The program runs in the IDE but running the jar files with an invalid or corrupt jar error. I made several changes to the build.gradle and Manifest and still this does not run. I am using Ubuntu 16.04.

I think the issue is related to the manifest.

IDE -

Intellij - 2019.2 Gradle Project

Java - java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

build.gradle -

plugins {
    id 'java'
}

group 'com.HelloWorldTesting'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

jar {
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes(
                'Implementation-Title': 'Hello World',
                "Main-Class": "com.HelloWorldTesting.Hello"
        )
    }
    archivesBaseName = 'app'
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Class -

public class Hello {
    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }
}

Error: Invalid or corrupt jarfile HelloWorldArt.main.jar

tree -

.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           └── Hello.class
│   └── tmp
│       └── compileJava
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── META-INF
│   └── MANIFEST.MF
├── out
│   └── artifacts
│       ├── HelloWorldArt_jar
│       │   └── HelloWorldArt.jar
│       └── HelloWorldArt_main_jar
│           └── HelloWorldArt.main.jar
├── settings.gradle
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── Hello.java
│   │   │   └── META-INF
│   │   │       └── MANIFEST.MF
│   │   └── resources
│   └── test
│       ├── java
│       └── resources
1
Can you try to remove from {configurations.runtimeClasspath.collect...} from jar section from gradle.Sambit
@Sambit - same error after that change.Trevor Lee Oakley
Recreate the JAR file.user207421
@user207421 works now, issue was two manifest files.Trevor Lee Oakley

1 Answers

0
votes

In your manifest you have defined your main class as com.HelloWorldTesting.Hello. Looking at your project tree I can see that the Hello.java class is on your source folder root and is not in the package com.HelloWorldTesting.

Try setting your main class like this: "Main-Class": "Hello"

Also you have a manifest file in your src folder and you are generating a manifest with gradle:

manifest {
    attributes(
            'Implementation-Title': 'Hello World',
            'Main-Class': 'Hello'
    )
}

Having multiple manifest files leads to this unexpected behaviour. Delete the manifest from your source folder, run gradle clean and the build should work after that.