Friday, April 29, 2022

eclipse: maven: Error: Could not find or load main class antlr.debug.misc.ASTFrame

 


>PROBLEM


Attempt to run an Eclipse project returns the following error:


  Error: Could not find or load main class antlr.debug.misc.ASTFrame

  Caused by: java.lang.ClassNotFoundException: antlr.debug.misc.ASTFrame


>SOLUTION

NOTE:
If you are quite sure about your project configuration, try to go straight to the 6th step.


0. Pre-requisite: make sure your pom.xml configuration is correct.


1. On Eclipse, using the Project Explorer (or Navigator), delete the following configuration files:

  .settings

  .classpath




2. Eclipse will complaing throwing an error pop-up. Ignore closing it.


3. Using the Project Explorer (or Navigator), point to the project's root and save it again.

This will generate again the deleted files.


4. Check if there are errors and fix them.




5. Clean the project (menu, Project, Clean...).



Wait to finish. Pay attention on the bottom bar.




6.  Fix the the Eclipse's configuration to run the application.





7. Run the application again and choose the proper option for the project.
In this case is Spring Boot.




8.  Configuration fixed and application is up and running.





>ENV

maven

eclipse

java17


Monday, April 25, 2022

maven: Execution default-testResources of goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:testResources failed: Unable to load the mojo 'testResources'


 >PROBLEM


The pom.xml shows the following error message:


Execution default-testResources of goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:testResources failed: Unable to load the mojo 'testResources' 

(or one of its required components) from the plugin 'org.apache.maven.plugins:maven-resources-plugin:3.2.0' 

(org.apache.maven.plugins:maven-resources-plugin:3.2.0:testResources:default-testResources:process-test-resources)



>SOLUTION


If the pom.xml was previously working fine, just update the maven project.

If using Eclipse, do:

Point to the project root on "Project Explorer" or equivalent, choose on the context menu mave, Updata Project (or ALT+F5)


>ENV

spring

java


maven: Caused by: java.lang.ClassNotFoundException: com.drillback.sgsup.Application

>PROBLEM


Running the executable jar with dependencies generated by maven plugin fails throwing the error message:

  java -jar myApp-0.0.1-SNAPSHOT-jar-with-dependencies.jar


- Returns:

  ..Caused by: java.lang.ClassNotFoundException..



>SOLUTION


1. Check the main class path in the pom.xml file.

Note: there are alternative plugins to generate executable jars.

See more at: 
How to Create an Executable JAR with Maven


Below is shown one of the options:


<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.appName.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>



2. If the plugins's configuration is correct, check the dependencies included in the project's pom.
For instance, if it was included lombok as shown below, remove it and test again:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>



>ENV


java
spring
maven

Thursday, April 14, 2022

MongoDB: Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method)


>PROBLEM

Starting a project using Maven, JEE/Spring, and MongoDB driver (mongo-java-driver) throws the following exception:

...[cluster-ClusterId{value='62586e27f362ec2cd1336bda', description='null'}-localhost:27027] # INFO  # org.mongodb.driver.cluster # method: info # Exception in monitor thread while connecting to server localhost:27027

com.mongodb.MongoSocketOpenException: Exception opening socket

...

Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method)


>SOLUTION

Although connection refusal may have many causes, this post will treat one of them that maybe it is your issue.

The "mongo-java-driver" tries to start MongoDB connection automatically as soon as the application starts if it finds MongoDB's configuration in .properties or .yml files.

It happens when we start a project using this default feature but later we decide to customize it, and there is no need for this feature anymore.

So, just remove or comment on the lines in the configuration file. 


For instance, if .yml, do this:

spring:

profiles: prod

jpa:
database: default
#  data:
#    mongodb:
#      host: localhost
#      port: 27027
#      database: mydatabase


If .properties:

#spring.data.mongodb.host=localhost
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=mydatabase


>ENV


SpriingBoot 2.X
Java 17
MongoDB 5

- pom.xml:


<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-mongodb</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-mongodb</artifactId>

</dependency>


<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>3.12.10</version>

</dependency>


Monday, April 11, 2022

Spring Boot: web service returns with 404

 

>PROBLEM


web service returns with 404


>SOLUTION

If you have already checked the protocol, then check if the @ResponseBody annotation is missing.


- BEFORE:


@PostMapping(value = {"/reset/{token}"})

public ResponseDTO resetPassword(HttpServletRequest request, @PathVariable String token) {

ResponseDTO dto = request(request, token);

dto.setData("hello there!");

return dto;

}



- AFTER:


@PostMapping(value = {"/reset/{token}"})

@ResponseBody

public ResponseDTO resetPassword(HttpServletRequest request, @PathVariable String token) {

ResponseDTO dto = request(request, token);

dto.setData("hello there!");

return dto;

}


>ENV

spring boot 2

java 17


eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...