19
votes

Is there a tutorial explaining how to properly create a Java Gradle project and build the .jar file?

When I create a Java project and add Gradle: File -> New -> Module -> Gradle -> ... I receive errors about Java EE websocket's not available (I'm using Ultimate Edition). However, I can successfully create a project by selecting File -> New -> Project-> Gradle -> which gives me a Java project with Gradle that I can debug. However, when I try to create an artifact (.jar file) I receive errors. I assume the errors stem from mistakes I made in the project structure settings.

Buildfile: build.xml does not exist!
Build failed

or

Error: Could not find or load main class Main

My project is such a mess at this point that I think the best option is to create another project and copy/paste the Main.class and Gradle's dependencies from the old project onto it.

But, how do I create the project correctly this time?

4

4 Answers

30
votes
  1. Create new Gradle (not Java) project. Be sure to select Java option in the dialog. Click Next. enter image description here

  1. fill GroupId, ArtifactId and version enter image description here

  1. Choose your gradle distribution. You can safely use the reccommended option. enter image description here

  1. Choose project name and location enter image description here

  1. Click finish

  1. You put your java classes to src/main/java folder. When using third party libraries, make sure you declare them as Gradle dependencies. enter image description here

  1. You build your sources using Gradle tab → tasks → build (double click) enter image description here

  1. You can see the result of the build in the Run tab enter image description here

  1. In case of error, you can see details by clicking the following button: enter image description here
11
votes

Just incase you run into no main manifest attribute problem where the executable Jar file is not running, I do the following.

1: go to your build.gradle and add the following at the bottom.

jar {
    manifest {
        attributes 'Main-Class': 'your_package_name.YOUR_MAIN_CLASS'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

enter image description here

  1. go to View > Tool Windows > Gradle

enter image description here

  1. A panel at the right side will be shown, now click the build for more task. Under the build task double click the build option and wait until the task is done.

enter image description here

  1. Now, go to your project directory and open the build > libs and the executable Jar file will be there.

I'm not sure if this is the right way.
No need to accept if it works, hope this help the others.

0
votes

If you are using Intellij you can just open the Gradle plugin (it is on the right side of your IDE) and execute a command: bootRepackage. With this you will have a jar in: your_project_folder/build/libs.

enter image description here

0
votes

Step 1: Add these lines on your build.gradle

jar {
from {
    configurations.runtime.collect {
        it.isDirectory() ? it : zipTree(it)
    }
    configurations.compile.collect {
        it.isDirectory() ? it : zipTree(it)
    }
}
manifest {
    attributes 'Main-Class': 'YOUR MAIN CLASS'
}

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

Step 2: clean and build .

I'm sure it will work , Regards