Unable to start Jetty Server - Error scanning entry META-INF/versions/9/ - maven

When running XACML-PAP-ADMIN and XACML-PAP-REST on Windows 10. Java jdk1.8.0_144. I get next error:
Error scanning entry META-INF/versions/9/module-info.class from jar file:///D:/Projects/XACML/XACML-PAP-ADMIN/target/xacml-pap-admin-2.0.0-SNAPSHOT/WEB-INF/lib/log4j-api-2.11.0.jar

That could be linked to your version of Jetty, considering it fails on log4j 2.11 jar.
See this question:
log4j 2.9 and later are multi-release jars for Java 9.
Make sure to use a Jetty compatible with that, or use slf4j instead.

You can create a custom DevMode JettyLauncher:
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.dev.shell.jetty.JettyLauncher;
import org.eclipse.jetty.webapp.WebAppContext;
import java.io.File;
public class DevModeJettyLauncher extends JettyLauncher {
#Override
protected WebAppContext createWebAppContext(TreeLogger logger, File appRootDir) {
WebAppContext webAppContext = super.createWebAppContext(logger, appRootDir);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern", "none");//this is just a regex that matches nothing
return webAppContext;
}
}
And then configure it when launching.
Dev Mode Parameters: -server <package>.DevModeJettyLauncher

Related

Spring Boot Application - Running jar file gives ResourceFinderException error

Created a jar file for a spring boot multimodule application and ran the jar file using java -jar command. While starting the application, it gives ResourceFinderException. When I analyzed it, the issue is happening because in my ResourceConfig file, i have used the package for my api end points. If I use register(service.class), the application starts fine. Any suggestion how can I provide the package instead of using register? The reason I want to use package is because I have lots of services inside multiple packages and the code looks very ugly if i use register for all the services. The ResourceConfig file looks like below.
public class AppResourceConfig extends ResourceConfig {
public AppResourceConfig {}{
super();
property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
**packages("com.api");**
register(GsonProvider.class);
register(RequestContextFilter.class);
register(NotFoundExceptionMapper.class);
register(DefaultExceptionMapper.class);
}
}
Here the issue is with highlighted line: packages("com.api")
If I comment out this code application will be up. Otherwise it is giving org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: api-01.03.00.04-snapshot.jar (No such file or direcotry)
Note: api-01.03.00.04-snapshot.jar is the jar file for one of the module in a project

org.springframework.boot.SpringApplication import throwing error

I am working on this tutorial using Spring Tool Suites.
I've copied what it says and my code and pom look the same (copied for easy review)
package com.javatpoint.spring_boot_example_sts;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootExampleSts
{
public static void main(String[] args){
SpringApplication.run(SpringBootExampleSts.class, args);
}
}
My issue is that I am getting a red line under ' org.springframework.boot.SpringApplication. To my knowledge, this library is not deprecated and I can't seem to find any instance of it being moved.
Thanks in advance.
May be maven either maven dependency is not downloaded or your IDE has some issues.
You can try to restart your IDE.
If issue still persist, Please build using mvn clean package.

How to run Spring Boot quick start code

I am new to Spring and Spring Boot, and I am trying it out. I am having trouble running the code sample from https://projects.spring.io/spring-boot/.
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
#Controller
#EnableAutoConfiguration
public class SampleController {
#RequestMapping("/")
#ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
I issued mvn install and everything appears to be fine. But then I issued java -cp target/myArtifId-1.0-SNAPSHOT.jar hello.SampleController and ClassNotFoundException is thrown.
How do I run this code sample?
As per spring boot documentation, you should be able to run your application with this command:
java -jar target/myArtifId-1.0-SNAPSHOT.jar
Spring Boot produces an executable jar, no need to specify a java class with a main method. That's also the reason why you can not include another class with a main method.
I prefer to use run goal of Spring Boot Maven Plugin to compile and run it in a single command:
mvn spring-boot:run

Unresolved requirement: osgi.component

I'm trying to develop onto Karaf an OSGi WAB containing a REST API and a call from a custom-made service. However, for some weird reason, the OSGi framework complains about an unsatisfied capability, osgi.component.
I would like to know:
How can I solve this issue?
What is osgi.component bundle? Why is needed?
Why maven-bundle-plugin (thereby also bnd), declares it inside the entry "Require-Capability"?
If I need to install it on the OSGi framework, where may I find it?
Some additional information:
karaf version: 4.0.7;
maven bundle plugin: 3.2.0;
OS: Windows 10 64bit;
IDE: Eclipse Neon;
Some code to provide additional info:
The whole error:
Error executing command: Error executing command on bundles:
Error starting bundle 96: Unable to resolve com.massimobono.karaf.examples.user-fully-rest [96](R 96.0): missing
requirement [com.massimobono.karaf.examples.user-fully-rest [96](R
96.0)] osgi.extender; (&(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))
Unresolved requirements:
[[com.massimobono.karaf.examples.user-fully-rest [96](R 96.0)]
osgi.extender;
(&(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))]
Manifest file:
Manifest-Version: 1.0
Bundle-SymbolicName: com.massimobono.karaf.examples.user-fully-rest
Archiver-Version: Plexus Archiver
Built-By: massi
Bnd-LastModified: 1479908575162
Bundle-ActivationPolicy: lazy
Bundle-ManifestVersion: 2
Import-Package: com.massimobono.karaf.examples.user;version="[0.0,1)",
com.massimobono.karaf.examples.user.service;version="[0.0,1)",javax.w
s.rs;version="[2.0,3)",javax.ws.rs.core;version="[2.0,3)"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=osgi.compo
nent)(version>=1.3.0)(!(version>=2.0.0)))",osgi.service;filter:="(obj
ectClass=com.massimobono.karaf.examples.user.service.UserService)";ef
fective:=active,osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/com.massimobono.karaf.examples.user.ui.ful
lyrest.UserRest.xml
Tool: Bnd-3.2.0.201605172007
Originally-Created-By: Maven Integration for Eclipse
Export-Package: com.massimobono.karaf.examples.user.ui.fullyrest;uses:
="javax.ws.rs,javax.ws.rs.core";version="0.0.1"
Bundle-Name: user-fully-rest Maven Webapp
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.8.0_91
Rest base class:
package com.massimobono.karaf.examples.user.ui.fullyrest;
import java.time.LocalDateTime;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import com.massimobono.karaf.examples.user.User;
import com.massimobono.karaf.examples.user.service.UserService;
import com.massimobono.karaf.examples.user.service.UserServiceException;
#Path("user")
#Component(immediate=true)
public class UserRest {
#Reference
private volatile UserService userService;
#GET
#Produces(MediaType.TEXT_HTML)
public String getUserNumber() {
try {
return String.format("<h1>Total users: %d</h1>", this.userService.size());
} catch (UserServiceException e) {
return String.format("Couldn't fetch total users because %s", e.getMessage());
}
}
#PUT
#Path("add/{name}/{surname}")
#Produces(MediaType.TEXT_HTML)
public String add(#PathParam("name") String name, #PathParam("surname") String surname) {
try {
User u = new User(name, surname, LocalDateTime.now());
this.userService.addUser(u);
return String.format("<h1>New user with id %d</h1>", u.getId());
} catch (UserServiceException e) {
return String.format("<h1>Couldn't fethc total users because %s</h1>", e.getMessage());
}
}
#DELETE
#Path("remove/{id}")
#Produces(MediaType.TEXT_HTML)
public String remove(#PathParam("id") int id) {
User u;
try {
u = this.userService.getUser(id);
this.userService.removeUser(u);
return String.format("<h1>User name=%s surname=%s removed correctly</h1>", u.getName(), u.getSurname());
} catch (UserServiceException e) {
return String.format("<h1>Couldn't remove user because %s</h1>", e.getMessage());
}
}
}
Thanks for any kind reply
How can I solve this issue?
Most likely you are missing SCR in your Karaf runtime. You can install it with feature:install scr
What is osgi.component bundle? Why is needed?
It's not a bundle but a requirement. Basically it says your bundle needs SCR (or something) that knows how to process and register the components defined in it via Declarative Services.
Why maven-bundle-plugin (thereby also bnd), declares it inside the entry "Require-Capability"?
Because it sees that you are using Declarative Services and knows they will not work unless you have something at runtime that understands how they are declared and knows how to manage their lifecycle. If the requirement was not there (which I believe was the case with earlier versions of bnd) then your bundle would start without issues but services would still be not registered / activated.
If I need to install it on the OSGi framework, where may I find it?
In Karaf it's available as feature (see the answer to your first question). In plain OSGi runtime (Felix, Equinox, ...) you need to install it manually. It's available in Maven central.
I had the same issue and in my case I was using to recent version of maven-bundle-plugin (4.x). After I downgraded the version (3.x) the issue disappeared.
Looks like the 3.x version of the plugin generates osgi.extender=osgi.component Require-Capability that my OSGi container can provide.

New Relic for Spring Boot

Recently, we convert a tomcat/spring app to spring boot. Everything is working fine apart from new relic. Is there a way I can easily config new relic with spring boot project. I don't want to hard code the location of new relic agent jar path, then run the spring boot project with the path.
edit: Spring boot project is with maven
You can include NewRelic Maven dependency and use maven-dependency-plugin to unpack in into your target/classes directory, which allows Maven to include it into final Jar file. Then you have to add Premain-Class attribute into manifest file and you can use your application jar as -javaagent source. You can find details on my blog post
Step by step instructions
Extract the files from the newrelic java agent archive.
Create a directory named newrelic in the root of your application.
Place the newrelic.jar from the archive in the above created newrelic folder
Place the newrelic.yml YAML config file in the above created newrelic folder.
Update the values in newrelic.yml as below.
license_key: 'your license key'
app_name: ‘Your application name’
Run you application by using the option javaagent
java -javaagent:newrelic\newrelic.jar -jar yourapplication.jar
-javaagent option needs to be before the -jar so the agent can start
I was stuck with the same issue, here is what I figured out. I implemented the 2nd way for my applications.
There are 3 ways to integrate New Relic with a Spring Boot Application-
Using the Java Agent provided by New Relic
Using New Relic's Micrometer Dependency
Micormeter's New Relic Dependency
1. Configuration using Java Agent Provided By New Relic
Download the Java Agent from this URL- https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/
Extract it.
Modify the newrelic.yml file inside the extracted folder to include your
license_key:
app_name:
Create a SpringBoot application with some REST endpoints.
Build the application.
Navigate to the root path where you have extracted the newrelic java agent.
Enter this command
java -javagent:<path to your new relic jar>\newrelic.jar -jar <path to your application jar>\<you rapplication jar name>.jar
To view the application metrics-
Log in to your New Relic account.
Go to Explorer Tab.
Click on Services-APM
You can see the name of your application(which you had mentioned in the newrelic.yml file) listed there.
Click on the application name.
The dashboard should look something like this.
Using New Relic's Micrometer Dependency is the preferred way to do it.
2. Configuration using New Relic's Micrometer Dependency
Add this dependency
<dependency>
<groupId>com.newrelic.telemetry</groupId>
<artifactId>micrometer-registry-new-relic</artifactId>
<version>0.7.0</version>
</dependency>
Modify the MicrometerConfig.java class to add your API Key and Application name.
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.micrometer.NewRelicRegistry;
import com.newrelic.telemetry.micrometer.NewRelicRegistryConfig;
import java.time.Duration;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.util.NamedThreadFactory;
#Configuration
#AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
#AutoConfigureAfter(MetricsAutoConfiguration.class)
#ConditionalOnClass(NewRelicRegistry.class)
public class MicrometerConfig {
#Bean
public NewRelicRegistryConfig newRelicConfig() {
return new NewRelicRegistryConfig() {
#Override
public String get(String key) {
return null;
}
#Override
public String apiKey() {
return "your_api_key"; // for production purposes take it from config file
}
#Override
public Duration step() {
return Duration.ofSeconds(5);
}
#Override
public String serviceName() {
return "your_service_name"; // take it from config file
}
};
}
#Bean
public NewRelicRegistry newRelicMeterRegistry(NewRelicRegistryConfig config) throws UnknownHostException {
NewRelicRegistry newRelicRegistry = NewRelicRegistry.builder(config)
.commonAttributes(new Attributes().put("host", InetAddress.getLocalHost().getHostName())).build();
newRelicRegistry.config().meterFilter(MeterFilter.ignoreTags("plz_ignore_me"));
newRelicRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("jvm.threads"));
newRelicRegistry.start(new NamedThreadFactory("newrelic.micrometer.registry"));
return newRelicRegistry;
}
}
Run the application.
To view the Application metrics-
Log in to your New Relic account.
Go to Explorer Tab.
Click on Services-OpenTelemetry
You can see the name of your application(which you had mentioned in the MicrometerConfig file) listed there.
Click on the application name.
The dashboard should look something like this.
Here is the link to my original question.

Resources