[JAVA] [Gradle] The story that the class file did not exist in the jar file

Event

When I built using the build.gradle file as shown below, a jar file was generated, but the class file did not exist in it.

build.gradle


apply plugin: 'java'

group = 'com.test.foo'
version = '1.0.0'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

compileJava {
    options.encoding = 'UTF-8'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation('org.slf4j:slf4j-api:1.7.21')

    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
}

Cause and solution

The cause of this event was that the java file existed under / src.

I am using a Java plugin this time, but if this plugin is left as default, it will build java files under / src / main / java.

So, when I moved the java file under / src / main / java and built it, the class file was successfully generated in the jar file.

This time, there was a background of changing the project that was originally built with Ant to build with Gradle, and I was not very conscious of the directory structure of the source, so there was such a condition in the Java plugin. Did not know.

By the way, you can also set the build target directory by using sourceSets as shown in here. I will.

This time, I took the policy of moving the source under / src / main / java in consideration of the unity with other projects, but I think this policy is also smart.

Summary

If the class file does not exist in the jar file generated using Gradle, please check the directory structure and what the directory to build Gradle is.

If you are using a Java plugin, the java files under / src / main / java are built by default, so match the directory structure to this or set the directory to be referenced when building. Be sure to set it explicitly.

reference

Gradle official documentation

Recommended Posts

[Gradle] The story that the class file did not exist in the jar file
Create a jar file that can be executed in Gradle
Solved the problem that the scroll event did not fire in JQuery
Add a time stamp to the JAR file name in Gradle
The story that the build error did not stop when using Eclipse 2020
Ignore parameters that do not exist in the model with ObjectMapper # readValue
The story that the Servlet could not be loaded in the Java Web application
I checked the package name referenced by the class contained in the JAR file
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
What to do if the changes are not reflected in the jar manifest file
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.
Processing when an ID that does not exist in the database is entered in the URL
The story that did not disappear when I tried to delete mysql on ubuntu
[Java] Get the file in the jar regardless of the environment
[Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found
How to debug the generated jar file in Eclipse
The story that the variable initialization method called in the Java constructor should not be overridden
A story that did not work when trying to handle events in Notification Center
The story that the forced update could not be implemented
The story that .java is also built in Unity 2018
The story that led to solving the error because postgres did not start with docker-compose up
The story when the test folder was not created in Rails
The case that @Autowired could not be used in JUnit5
Set up Gradle multi-project in IntelliJ to build JAR file
[Docker] The story that an error occurred in docker-compose up
About the File :: Stat class
HttpServletRequest class file not found
The story that the DB connection and other timeouts did not time out according to the set value
[Rails] A story that continued to incorrectly verify the reason why the update action did not pass (update)
Create a static file that expands variables using the ERB class
Handling when calling a key that does not exist in hash
Symbol not found error when new class in another Java file
The story that Tomcat suffered from a timeout error in Eclipse
[Swift] Variable shapes that were not taught in the introductory book
Replace the contents of the Jar file
Unzip the zip file in Java
Make multi-project executable JAR in Gradle
Include image resources in jar file
The story of forgetting to close a file in Java and failing
[Java] The story that the expected array was not obtained by the String.split method.
[Rails] About the error that the image is not displayed in the production environment
Sample program that returns the hash value of a file in Java
A story that was embarrassing to give anison file to the production environment
Let's stop the god entity that expresses all stats in one class!
[Java] Implement a function that uses a class implemented in the Builder pattern
After verifying the Monty Hall problem with Ruby, a story that I could understand well and did not understand well