adding Apache Commons Lang3 to my Spring Boot project - spring

I am doing a Spring Boot exercise from Baeldung.com. The project has a test class that uses the method randomAlphabetic, which I have traced back to RandomStringUtils, which in turn belongs to Lang3 from Apache Commons. I can't seem to add the import of RandomStringUtils or even Lang3 itself to my project.
I have looked at the import on the GitHub repository for the project. The project is done as a Maven project, but I have done mine as a Gradle project, because Gradle is what I use at work (I don't know Maven). I have added the following to my build.gradle file:
plugins {
id 'org.apache.commons'
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
}
I have attempted the following import statements:
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.*;
When I am writing my import statement and I put the period after commons, the autocomplete only comes up with two choices: logging and logging.impl . I do not even have an option to add lang3. I have also checked in the Spring Initializer to see if I could add lang3 there, but I cannot.
I have downloaded lang3 onto my computer. It contains 5 JAR files plus a lot of other material that I don't know how to use. I thought I could move those JAR files into my Project and External Dependencies 'folder,' but I can't find any way to do so. I'm using Spring Tools 4 Suite for Eclipse, btw.
Any help is appreciated.

Related

How can I tell Spring Boot to place some of the classes at the root of the jar instead of BOOT-INF?

I'm trying to set a custom log Handler in my Spring Boot (version 2.6.3) application. The result is a ClassNotFound as described in this other question
Can't override java.util.logging.LogManager in a Spring Boot web application: Getting java.lang.ClassNotFoundException on already loaded class
Based on the answer to that question, it seems I need my Handler and all its dependencies to be placed into the root of the executable jar.
Is there a direct way to accomplish this during the Maven build, i.e. not by extracting and repackaging the jar myself post-build?
This issue is a result of BOOT-INF fat jar structure introduced by Spring Boot 1.4.
There is currently no straightforward solution, and it appears some of the Spring Boot maintainers do not agree there is a problem, so it could be a long time before the situation changes:
Issue #6626: Make it easier to package certain content in the root of a fat jar
Issue #12659: Starting executable war with -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager produces a ClassNotFoundException
WORKAROUND #1
I had to do two things to get my application working again with a custom log handler. 1) use Maven Shade to package up the log handler with all its dependencies, and 2) launch the app with using the PropertiesLauncher class in the command line instead of using java -jar:
java -cp executable.jar:logger-shaded.jar -Dloader.main=mypackage.myapp org.springframework.boot.loader.PropertiesLauncher
The executable.jar, logger-shaded.jar, and mypackage.myapp are placeholders specific to my project, so adjust accordingly.
WORKAROUND #2
If the handler is loaded from code in a config class or from main() instead of being specified in the file loaded via java.util.logging.config.file, as discussed in the comments to the answer in this other question, then everything works as expected. I actually prefer this over Workaround #1 as it results in a smaller deployment, but it does require writing a few more lines of code.

Why I cannot import JsonFormat from package com.google.protobuf.util?

Currently, I'm working on google cloud (google pub/sub). I've used java client. But I cannot import JsonFormat class from package com.google.protobuf.util. I'm using intelliJ idea, I have tried invalidate cache and restart the idea. What am I missing?
My build.gradle
// google cloud
implementation platform('com.google.cloud:libraries-bom:22.0.0')
implementation 'com.google.cloud:google-cloud-pubsub'
In latest versions of google cloud bom the package com.google.cloud.protobuf:protobuf-util is used with runtime scope, so it's not available during compilation. Not sure why they changed it...
Adding manually the dependency with compile scope (maven) or implementation (gradle) should help.
Source:
https://mvnrepository.com/artifact/com.google.cloud/google-cloud-pubsub/1.114.7

Jar file not getting added in to a external Libraries in Intellij

I am trying to work on a spring-security project in which i have added the spring security dependency via pom.xml file.But as my maven completed its build successfully,its not getting added in the external library.
Please find my the screenshots below:
pom.xml file:
External Library
Dependencies added after successful maven build :
Tried to create a new project to check whether the above issues persist there as well,but there its working as expected as i am able to get the required java files.So the issue is only relevant to the above project.
I even tried to do the steps mentioned from the below links apart from the maven life cycle steps,but that also did not work out
link

Spring Boot modifying example project - Error: Could not find or load main class hello.Application

I imported on of the Spring Boot getting started projects,
gs-rest-service-complete and looked over it, ran it and got the
general idea how I could start things.  So needless to say, I started
to modify that project in order to get my own set of web services
started.  Originally there was an Application class in the hello
package annotated with the #SpringBootApplication and had a main
method which did a SpringApplication.run(Application.class, args) to
get it running.  Obviously I was going to use the 'hello' package, so
I deleted all that and made my own package hierarchy something on the
order of com.mycompany.product.  In that package I made an
Application.class with the same guts (but corresponding to this
package) and a controller in a subpackage of that.
When trying to start the spring boot app, i get this error in the console now:
    Error: Could not find or load main class hello.Application
I have no idea what's telling it to look for that specific class in
that specific package.  There's no reference to that stuff in my
project anymore.  (running in STS).
What controls this?  What do I do?  I had assumed having only class in
the project at the top of a package layout with the
#SpringBootApplication would get everything moving 'just right'.
How about that... The Boot Dashboard is actually a bunch of launch configurations for run or debug.
I selected my app there and realized when right clicking on it there was an option called "Open Config" which brought me to the "Debug Configurations" dialog. And there was my answer... in the "Main type" field, it said "hello.Application". That's where you set or change it.
I changed that to my "com.mycompany.product.Application" and the app started up as hoped!
The recommended way to get started with a spring boot application is to use the initializr
Click on the 'Switch to full version', fill in the details of your application and select the spring-boot starter packages you want to include. You can look in the pom.xml of the demo project you were using as a tutorial to figure out which ones you need. Then you just click the generate project button and import it into eclipse. Inside eclipse go file -> import -> Maven -> Existing maven projects. Then point to where you unzipped the file you downloaded from spring initializr and it will import your project into Eclipse.

org.springframework package cannot be imported

As the topic says I cannot import this package in my web dynamic project using SpringSource Tool Suite.The command Spring Tools --> Add Spring Project Nature has been already executed.
Thank you.
What kind of project did you create? Is it a Maven project? If so, you need to make sure that you are importing 'at least' the spring-context dependency like so
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.0.RELEASE</version>
    <scope>runtime</scope>
</dependency>
(or whatever version you want to use)
[EDIT]
Since as you say, you are using a Dynamic Web Project through this example -> http://www.vaannila.com/spring/spring-mvc-tutorial-1.html, you need to physically add the following jar files to your WEB-INF/lib folder
antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.context.support-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
org.springframework.web-3.0.0.M3
org.springframework.web.servlet-3.0.0.M3
You should have the following:
Adding the 'Spring Project Nature' does not add the dependencies for you, but only instructs the IDE that this project is using Spring.
Use maven.it automatically adds the necessary jar files.otherwise u need to manually do it.you can have a look at this http://www.mkyong.com/maven/how-to-convert-java-web-project-to-maven-project/
to migrate an existing project into maven
OR
You can manually add the necessary jar files.
Right click on the spring project>>
properties>>
java build path>>
libraries>>Add jars
:)

Resources