24

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: Menu FileNewModuleGradle → *... I receive errors about Java EE websocket's not available (I'm using Ultimate Edition).

However, I can successfully create a project by selecting menu FileNewProjectGradle → 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. Maybe I should create another project, and then copy/paste the Main.class and Gradle's dependencies from the old project onto the new project.

If this is my best option, how do I correctly create the project this time?

4 Answers 4

33
  1. Create a new Gradle (not Java) project. Be sure to select the Java option in the dialog. Click Next.

    Enter image description here

  2. fill GroupId, ArtifactId and version

    Enter image description here

  3. Choose your Gradle distribution. You can safely use the recommended option.

    Enter image description here

  4. Choose the project name and location

    Enter image description here

  5. Click Finish

  6. You put your Java classes in the src/main/java folder. When using third party libraries, make sure you declare them as Gradle dependencies.

    Enter image description here

  7. You build your sources using Gradle tab → tasks → build (double click)

    Enter image description here

  8. You can see the result of the build in the Run tab

    Enter image description here

  9. In case of an error, you can see details by clicking the following button:

    Enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer (especially images). However, I get an error: i.imgur.com/gfsAhBD.png
can you please post the actual console output?
Did you add gradle dependencies for libraries you are using?
Try clean task and then build again?
Alright! Build successful! Now, where is my .jar file? Is it in <project>/build/libs? I can see there a jar file but its size is only 7 KB.
|
14

Just in case 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 file 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

  2. Go to menu ViewTool WindowsGradle

    Enter image description here

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

    Enter image description here

  4. Now, go to your project directory and open the BuildLibraries and the executable JAR file will be there.

I'm not sure if this is the right way.

Comments

2

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 file in: your_project_folder/build/libs.

Enter image description here

5 Comments

This is Android SDK location setting.
There is a plugin also for intellij:
Question is on Java EE. No Android framework is mentioned.
Yes and the picture is changed and it is for a java project with intellij with gradle.
mine is a spring boot project, for me bootRepackage option is not showing. gradle assemble works fine(building jar) for me
1

Step 1: Add these lines in your build.gradle file:

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.