Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my jar file not not contain any class files?

I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:

version = "0.0.1"
apply plugin: 'java'

task('gen', type: Jar) {
}

task gen2(type: Jar)

Running

gradle jar

generates a JAR-file that contains .class-files, while running

gradle gen

or

gradle gen2

generate a JAR-file that does NOT contain any .class-files.

Whats wrong with my class definition?

like image 325
Edward Avatar asked Oct 19 '22 13:10

Edward


People also ask

Does JAR file contain class files?

The JAR file contains the TicTacToe class file and the audio and images directory, as expected. The output also shows that the JAR file contains a default manifest file, META-INF/MANIFEST. MF, which was automatically placed in the archive by the JAR tool.

How do I find the class inside a jar?

To find the . jar files that contain a class, you can use the FindClass.sh script. First go to a UNIX installation of Sterling Platform/MCF. If the FindClass.sh script already exists it should be in your $YFS_HOME directory or your $YFS_HOME/lib directory.

Why can't I run .JAR files?

If you do not have Java installed, and the PATH variable is not set correctly, attempts to run a JAR file on Windows or Ubuntu will result in a 'Java not recognized' error. To run a JAR file, you must install the Java JDK or JRE on your computer.


1 Answers

To build a jar with all the classes from main, as a default jar task would, do this:

task gen2(type: Jar){
    baseName = 'gen2Jar'
    from sourceSets.main.output
}

You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.

Alternatively, to copy settings from the default jar task:

task gen(type: Jar){
    baseName = 'genJar'
    with jar
}

Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main

like image 79
RaGe Avatar answered Oct 29 '22 18:10

RaGe