Gradle - kein Hauptmanifestattribut


90

Ich bin verrückt nach diesem Fehler, den ich bekomme, wenn ich eine JAR-Datei aus Gradle ausführe. Der Fehler lautet "kein Hauptmanifestattribut in RxJavaDemo.jar". Ich habe versucht, die Manifest-Eigenschaft zu manipulieren, aber ich glaube, ich habe vergessen, die Abhängigkeiten oder etwas hinzuzufügen. Was genau mache ich falsch?

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

mainClassName = 'demo.MainDashboard'

dependencies {
    compile files ("H:/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar")
    compile files ("H:/Processes/Development/libraries/controls-fx/controlsfx.jar")
    compile files ("H:/Processes/Development/libraries/database_connections/sqlite-jdbc-3.8.6.jar")
    compile files ("H:/Processes/Development/libraries/guava/guava-18.0.jar")
    compile files ("H:/Processes/Development/libraries/rxjava/rxjava-1.0.12.jar")
    compile files ("H:/Processes/Development/libraries/rxjava-extras/rxjava-extras-0.5.15.jar")
    compile files ("H:/Processes/Development/libraries/rxjavafx/RxJavaFX-1.0.0-RC1-SNAPSHOT.jar")
    compile files ("H:/Processes/Development/libraries/rxjavaguava/rxjava-guava-1.0.3.jar")
    compile files ("H:/Processes/Development/libraries/rxjava-jdbc/rxjava-jdbc-0.6.3.jar")
    compile files ("H:/Processes/Development/libraries/slf4j/slf4j-api-1.7.12.jar")
    compile files ("H:/Processes/Development/libraries/tom-commons/tom-commons.jar")
}

sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDir "src/main/resources"
}

jar { 
    manifest {
    attributes(
        "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
    }
    from configurations.compile.collect { entry -> zipTree(entry) }
}

Antworten:


138

Versuchen Sie, Ihre Manifestattribute wie folgt zu ändern:

jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'hello.HelloWorld'
    )
  }
}

Und dann wechseln Sie einfach 'hello.helloWorld'zu '<your packagename>.<the name of your Main class>'(wo Ihre Hauptklasse eine Hauptmethode hat). In diesem Fall erstellen Sie in Ihrem Manifest ein Attribut, das auf diese Klasse verweist, und dann wird ein JAR ausgeführt.


1
@ Stanislav 'Hauptklasse' Wert ist die Hauptklasse? Was sind Hallo und Hallo Welt in Ihrem Beispiel?
Daniela Maia

2
@ DanielaMaia es ist nur ein voll qualifizierter Klassenname, sicher muss er als hallo geschrieben werden. Hallo Welt, wo hallo das Paket ist, in dem sich die HelloWorld Klasse befindet
Stanislav

9
Ich musste den collect {}Teil entfernen , damit er für mich funktioniert. Ihr Code setzt voraus, dass sich alle Abhängigkeiten im selben Ordner wie Ihre Hauptklasse befinden.
AutonomousApps

0

FWIW - Ich habe die folgende JAR-Aufgabe verwendet, um alle meine Kompilierungsabhängigkeiten in der JAR-Datei zusammenzufassen, und die obige Empfehlung verwendet, um den Klassenpfad richtig festzulegen

apply plugin: 'java-library'

jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'your.main.class.goes.here'
    )
  }

  // You can reference any part of the dependency configurations,
  // and you can have as many from statements as you need
  from configurations.compile  
  // I just copied them into the top of the jar, so it looks like the eclipse exported 
  // runnable jar, but you could designate a lib directory, and reference that in the 
  // classpath as "lib/$it.name" instead of it.getName()
  into ''   
}
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.