AWS Lambda: ClassNotFoundException - java

I currently get a ClassNotFoundException whenever I try to test my Lambda function on AWS Lambda. The exception is shown here:
I've searched online, including this link here: AWS Lambda: class java.lang.ClassNotFoundException, to no avail.
I am working in Android Studio and created a JAR file (using this link: How to make a .jar out from an Android Studio project) to use to upload the class to the AWS Lambda console.
Below is the structure of my project:
When I upload my JAR file to the AWS Lambda console, the Configuration tab looks like this:[
I was previously told that it could have been because my JAR file was not an executable JAR file with a MANIFEST.MF file, but I definitely have that.
Any other reason as to why this error consistently pops up and how to fix it?

Your handler needs to include the full Java package. In your example, you need to have the handler be:
edu.csulb.android.riseandshine.Dynamodb::handleRequest
This is configured on the Lambda screen where you currently have Dynamodb::handleRequest
EDIT
My "hello world" Lambda in looks like the following. Note that this is a maven project so the code has to live where maven expects it. At the "root" of the directory where you're developing is the pom.xml file (below) and the Java file needs to live in src/main/java/com/hotjoe/aws/lambda/hello/handler.
Once you have that and maven installed, run mvn clean package. The deployable jar will be target/hello-world-lambda-1.0-SNAPSHOT.jar. I deployed this to Lambda just now and can run it with the test:
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}
which is the default for the Lambda tests. This is all taken from the AWS docs on creating a deployment. In my example, the Lambda handler is com.hotjoe.aws.lambda.hello.handler.HelloWorldLambdaHandler::handleRequest.
The code I've used is below.
HelloWorldLambdaHandler.java
package com.hotjoe.aws.lambda.hello.handler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
#SuppressWarnings("unused")
public class HelloWorldLambdaHandler implements RequestHandler<HelloWorldLambdaHandler.InputObject, String> {
public String handleRequest(InputObject inputObject, Context context) {
System.out.println( "got \"" + inputObject + "\" from call" );
return "{\"result\": \"hello lambda java\"}";
}
public static class InputObject {
private String key1;
private String key2;
private String key3;
public String getKey1() {
return key1;
}
public String getKey2() {
return key2;
}
public String getKey3() {
return key3;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public void setKey2(String key2) {
this.key2 = key2;
}
public void setKey3(String key3) {
this.key3 = key3;
}
#Override
public String toString() {
return "InputObject{" +
"key1='" + key1 + '\'' +
", key2='" + key2 + '\'' +
", key3='" + key3 + '\'' +
'}';
}
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hotjoe.aws.lambda.hello</groupId>
<artifactId>hello-world-lambda</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The stack trace indicates that the Java runtime cannot find a class named "Dynamodb". There is no such class in the AWS SDK for Java .. the correct class name is "DynamoDB". Notice the difference in case between your class from the exception stack trace and the correct name.

I fixed my issue by following below link. Basically need to run mvn "package shade:shade" command to include all depending jars. https://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-and-eclipse.html (Later experiments showed that just do mvn package would be enough as long as the shade plugin defined in pom.xml file.)
The next challenge I faced is the jar too big. I followed the below link to include dynomaDB, S3, ec2 components instead of the entire sdk.
https://aws.amazon.com/blogs/developer/managing-dependencies-with-aws-sdk-for-java-bill-of-materials-module-bom/.
Then I need to use EnvironmentVariableCredentialsProvider to deploy to lambda function.

It's not an answer to the original question. But I was facing the same issue of class not found and solved it by placing pom.xml at the correct location in the directory structure.
In Android Studio, "src/main/java/.." lives in the Application folder inside the root directory. I was wrongly placing the xml file in the root directly.
When I created a separate directory structure outside of Android Project and placed the xml file as described here, the problem was resolved.

I know it's late to share the solution,
but the same problem I was facing in Apr, 2021 and none of these answers worked in my case. so I'm sharing how I fixed it, it may help someone.
I got it resolved by adding following maven plugin with spring-boot-thin-layout to create a thin jar.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.26.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
and then you can run
./mvnw clean package
or if you're using STS/Eclipse, then run Maven build.. with goals clean package
This will generate a jar file in KB in your target folder and you can use this JAR to deploy on AWS lambda code.
PS:
remove maven-shade-plugin
I've refereed Official documentation of spring.cloud.io

Related

How do I include src/test/java files to run TestNG tests?

I'm just learning Java and could use your help. I'm using Eclipse, and created a Maven project using the org.openjfx archetype. Everything seems to work fine except when I try to write tests in src/test/java, which causes an error.
An error occurred while instantiating class
starcraft.warcraft.test.TestClass: Unable to make public
starcraft.warcraft.test.TestClass() accessible: module
starcraft.warcraft does not "exports starcraft.warcraft.test" to
module org.testng
This is how I created the project with default settings in Eclipse:
Project Setup with Maven Archetype Selection
Now, when Eclipse creates the project, it doesn't have a src/test/java folder, so I create that manually. Then I create a class called "TestClass" inside a package "starcraft.warcraft.test" inside src/test/java, and I add a simple method to test inside the App class called "adder". You can see the project structure
Project Structure
package starcraft.warcraft;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
#Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
// WILL TEST THIS METHOD
public static int adder(int digit1, int digit2) {
return digit1 + digit2;
}
public static void main(String[] args) {
launch();
}
}
Now I want to use TestNG for the tests, and so I include it in my POM which is
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>starcraft</groupId>
<artifactId>warcraft</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>starcraft.warcraft.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is the default POM created by the Maven archetype except for the TestNG dependency I added. When I try to use TestNG, Eclipse makes me add it to the module path like so:
Maven saying I need to add the TestNG library
And here is my module-info:
module starcraft.warcraft {
requires javafx.controls;
requires org.testng;
exports starcraft.warcraft;
}
OK, all good so far, but now when I try to run my test inside TestClass:
package starcraft.warcraft.test;
import org.testng.annotations.Test;
import starcraft.warcraft.App;
public class TestClass {
#Test
public void testAdder() {
int sum = App.adder(1, 2);
System.out.println(sum);
}
}
I get the error, which again is
An error occurred while instantiating class
starcraft.warcraft.test.TestClass: Unable to make public
starcraft.warcraft.test.TestClass() accessible: module
starcraft.warcraft does not "exports starcraft.warcraft.test" to
module org.testng
I can't figure out how to do the export. When I try making the entry in module-info, it doesn't give me the option of adding the package in src/test/java, the only packages it allows me to choose from are in src/main/java.
I don't understand modules well. How can I get the program to let me run tests from src/test/java?
Thanks everyone who looked at this. I solved it by following these steps:
Delete module-info.java. This turns it into a nonmodular project which is fine for me. I hesitated to do this because the Maven JavaFX archetype included it, but as it says here somewhere
https://openjfx.io/openjfx-docs/#IDE-Eclipse
you can just delete it after its created.
The problem then if you try to run the project is it will give you a warning that JavaFX isn't included as a module. It will still run, but its best to get rid of this incase of problems down the road. So you need to download the JavaFX libraries, place them in your hard drive, and then include them in your project via VM arguments in Eclipse:
right click project -> Run configuration -> Arguments tab -> add in the VM arguments area something like:
--module-path [fully qualified path to lib folder containing downloaded JavaFX] --add-modules javafx.controls
path would be like "C:\javafx\lib" or wherever you placed the downloaded JavaFX.
Then it should run, and project will still build using Maven, but I'm not sure if its using the JavaFX which is still in the Maven POM or the one I specified on the C drive. But it works. Any help on whats happening there would be appreciated. Thanks

Is it possible to configure maven to compile generated sources wouthout the use of a plugin?

I know this question is not new. But it seems that there is no definite answer. This answer from 2012 states that if generated sources are placed under target/generated-sources/<tool> they will be compiled. ANTLR 4 maven plugin follows this paradigm. Per documentation, the default value of outputDirectory is: ${project.build.directory}/generated-sources/antlr4.
Now in my case I have a custom tool that generates sources. I've set its output directory to be at ${project.build.directory}/generated-sources/whatever and it didn't work. Regarding the whateverpart, I've tried to use the id of the goal that generates the sources and even tried to hijack antlr4 name. No result though.
When I try this solution that suggests using mojo build-helper-maven-plugin it compiles as expected. But according to maven guide to generating sources it should be working without any helper plugin, shouldn't it? Am I missing something?
Here is the POM (fragment) configuration that I use to generate the sources.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>generate-code</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>com.company.product</groupId>
<artifactId>CodeGenerator</artifactId>
</executableDependency>
<arguments>
<argument>${basedir}/</argument>
<argument>${project.build.directory}/generated-sources/generate-code/</argument>
</arguments>
<mainClass>com.company.codegeneration.CodeGenerator</mainClass>
</configuration>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>CodeGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
Your understanding is just a bit incorrect.
Nothing automatic, plugins generating source code typically handle that by adding their output directory (something like target/generated-sources/ by convention) as source directory to the POM so that it will be included later during the compile phase.
Some less well implemented plugins don't do that for you and you have
to add the directory yourself, for example using the Build Helper
Maven Plugin.
As the other answer noted, most plugins typically add the generated code as new source path.
Ex: See antlr4's Antlr4Mojo.java class. Here, the plugin is adding the generated classes to project source by calling addSourceRoot method in execute method.
// Omitted some code
void addSourceRoot(File outputDir) {
if (generateTestSources) {
project.addTestCompileSourceRoot(outputDir.getPath());
}
else {
project.addCompileSourceRoot(outputDir.getPath());
}
}
// Omitted some code
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
// Omitted code
if(project!=null)
{
// Tell Maven that there are some new source files underneath the output
// directory.
addSourceRoot(this.getOutputDirectory());
}
}
// Omitted some code
So, you can either do this in your custom plugin or use the build-helper-maven-plugin.

Unable to run "Hello World" Java client of Elastic-Search

I am unable to run a very basic program of creating a "Hello World" java client of Elastic-Search.
The documentation is extremely terse about what to do in such cases.
Here is my code:
find . -type f
./pom.xml
./src/main/java/examples/EsRoutingNodeClient.java
Both files are shown below.
Java file containing the code:
package examples;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.NodeBuilder;
public class EsRoutingNodeClient
{
private static final String ZEN_DISCOVERY_UNICAST_HOSTS = "[\"10.10.10.10:9200\"]"; // Used an actual ES master node's IP here
private static final String ES_PATH_HOME = "/Users/appuser/work/software/elasticsearch/dummy-path-home/";
private static final String ES_CLUSTER_NAME = "my-cluster";
private Client client;
private void createEsClient ()
{
Settings settings = Settings.settingsBuilder()
.put("http.enabled", false)
.put("discovery.zen.ping.multicast.enabled", false)
.put("discovery.zen.ping.unicast.hosts", ZEN_DISCOVERY_UNICAST_HOSTS)
.put("discovery.zen.minimum_master_nodes", 1)
.put("path.home", ES_PATH_HOME)
.build();
client =
NodeBuilder.nodeBuilder()
.settings(settings)
.clusterName(ES_CLUSTER_NAME)
.data(false)
.client(true)
.node().client();
}
public EsRoutingNodeClient ()
{
createEsClient();
}
public static void main (String args[])
{
new EsRoutingNodeClient();
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>examples</groupId>
<artifactId>es-node-client</artifactId>
<version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>es-node-client</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration />
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.slf4j:*</exclude>
<exlcude>com.esotericsoftware.kryo:kryo:*</exlcude>
</excludes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>examples.EsRoutingNodeClient</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Finally I run it as follows:
mvn clean package
java -jar target/es-node-client-0.0.3-SNAPSHOT.jar
And the exception I get is:
194) Error injecting constructor, java.lang.IllegalStateException: This is a proxy used to support circular references involving constructors. The object we're proxying is not constructed yet. Please wait until after injection has completed to use this object.
at org.elasticsearch.node.service.NodeService.<init>(Unknown Source)
while locating org.elasticsearch.node.service.NodeService
for parameter 5 at org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction.<init>(Unknown Source)
while locating org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction
for parameter 2 at org.elasticsearch.cluster.InternalClusterInfoService.<init>(Unknown Source)
while locating org.elasticsearch.cluster.InternalClusterInfoService
while locating org.elasticsearch.cluster.ClusterInfoService
for parameter 3 at org.elasticsearch.cluster.routing.allocation.AllocationService.<init>(Unknown Source)
while locating org.elasticsearch.cluster.routing.allocation.AllocationService
for parameter 3 at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.<init>(Unknown Source)
while locating org.elasticsearch.cluster.metadata.MetaDataCreateIndexService
for parameter 5 at org.elasticsearch.snapshots.RestoreService.<init>(Unknown Source)
while locating org.elasticsearch.snapshots.RestoreService
Caused by: java.lang.IllegalStateException: This is a proxy used to support circular references involving constructors. The object we're proxying is not constructed yet. Please wait until after injection has completed to use this object.
at org.elasticsearch.common.inject.internal.ConstructionContext$DelegatingInvocationHandler.invoke(ConstructionContext.java:103)
at com.sun.proxy.$Proxy11.setNodeService(Unknown Source)
at org.elasticsearch.node.service.NodeService.<init>(NodeService.java:77)
at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.elasticsearch.common.inject.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:50)
at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:86)
at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:104)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:47)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:887)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:43)
at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:59)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:46)
at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
... more such lines
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:46)
at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:201)
at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:193)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:880)
at org.elasticsearch.common.inject.InjectorBuilder.loadEagerSingletons(InjectorBuilder.java:193)
at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:175)
at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:46)
at org.elasticsearch.node.Node.<init>(Node.java:200)
at org.elasticsearch.node.Node.<init>(Node.java:128)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145)
at org.elasticsearch.node.NodeBuilder.node(NodeBuilder.java:152)
at examples.EsRoutingNodeClient.createEsClient(EsRoutingNodeClient.java:30)
at examples.EsRoutingNodeClient.<init>(EsRoutingNodeClient.java:46)
at examples.EsRoutingNodeClient.main(EsRoutingNodeClient.java:51)
194 errors
at org.elasticsearch.common.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:360)
at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:178)
at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:46)
at org.elasticsearch.node.Node.<init>(Node.java:200)
at org.elasticsearch.node.Node.<init>(Node.java:128)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145)
at org.elasticsearch.node.NodeBuilder.node(NodeBuilder.java:152)
at examples.EsRoutingNodeClient.createEsClient(EsRoutingNodeClient.java:30)
at examples.EsRoutingNodeClient.<init>(EsRoutingNodeClient.java:46)
at examples.EsRoutingNodeClient.main(EsRoutingNodeClient.java:51)
My ultimate aim is to use a routing node client in storm.
Any help would be very much appreciated.
Thanks!
While upgrading from 1.4 to 2.4 I ran into this same issue. I'm not 100% why, but for me it was something to due with specifying the unicast host. I'm running ES via the zip download with the defaults.
At first I had set the ports for Client Node (ie, the app i'm working on) to be outside the default range thinking I need to do this to avoid conflicts running 2 nodes on the same box. When I did this my client could never find the master. Turns out that the zen discovery only pings 5 local ports in its own range. Before I figured this out (by reading https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/discovery/zen/ping/unicast/UnicastZenPing.java) I added in a unicast.host to get the same issue you are having. After removing both settings my app is now connecting to my ES server.
here's my config
cluster.name=elasticsearch
node.name=local-dev
node.master=false
node.data=false
path.home=${project.build.directory}/es
path.data=${project.build.directory}/es/data
path.logs=${project.build.directory}/es/logs
network.host=127.0.0.1
discovery.zen.minimum_master_nodes=1
code
#Bean
public Node elasticSearchNode() throws Exception {
Settings settings = Settings.settingsBuilder().put(getProps()).build();
return nodeBuilder()
.settings(settings)
.client(true)
.node();
}
#Bean
public Client elasticSearchClient(Node node) throws Exception {
return node.client();
}
private Properties getProps() {
try {
String profile = getProfile();
log.info("Loading ES properties for env: {}", profile);
ClassPathResource resource = new ClassPathResource("es/es."+profile+".properties");
return PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
log.error("Can not read property file");
throw new RuntimeException(e);
}
}
private String getProfile() {
return System.getProperty("env", "local");
}

Very simple step by step JBehave setup tutorial?

Though I have read many, but many articles on how to use JBehave, I can't get it to work. Here are the steps I went through so far:
Created new Java Project
Downloaded JBehave JAR file version 3.6.8 and added it to my build path libraries
Created a package called com.wmi.tutorials.bdd.stack.specs under the test source folder in my workspace
Added the JBehave JAR file to my Build path Library configuration
Created a JBehave story in the above-mentioned package (StackBehaviourStories.story)
Created a Java class in the above-mentioned package (StackBehaviourStory.java)
Created a Java class in the above-mentioned package (StackBehaviourSteps.java)
Imported the Given, Named, Then, When annotations in my Java class
Written two different scenarios in my JBehave story file
And still, I can't get it to work/run! =(
The story file:
Narrative:
In order to learn to with JBehave using Eclipse
As a junior Java developer though senior in .Net and in BDD
I want to define the behaviour of a custom stack
Scenario: I push an item onto the stack
Given I have an empty stack
When I push an item 'orange'
Then I should count 1
Scenario: I pop from the stack
Given I have an empty stack
When I push an item 'apple'
And I pop the stack
Then I should count 0
The story class
package com.wmi.tutorials.bdd.stack.specs
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.junit.JUnitStory;
public class StackBehaviourStory extends JUnitStory {
#Override
public Configuration configuration() { return new MostUsefulConfiguration(); }
#Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration()
, new StackBehaviourSteps());
}
}
The steps class
package com.wmi.tutorials.bdd.stack.specs
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.junit.Assert;
public class StackBehaviourSteps {
#Given("I have an empty stack")
public void givenIHaveAnEmptyStack() { stack = new CustomStack(); }
#When("I push an item $item")
public void whenIPushAnItem(#Named("item") String item) { stack.push(item); }
#Then("I should count $expected")
public void thenIShouldCount(#Named("expected") int expected) {
int actual = stack.count();
if (actual != expected)
throw new RuntimeException("expected:"+expected+";actual:"+actual);
}
}
I'm currently using Eclipse Kepler (4.3) JEE with everything I need to use JUnit, Google App Engine, and yes, JBehave is installed correctly following the Eclipse JBehave installation tutorial.
I can't get it to work. So how can I make it work correctly using Eclipse, JBehave and JUnit?
I know I'm late to the party here but I'm posting because this is the info I wish I had a week ago as it would've saved me a lot of pain. I'm very much into the idea of BDD, but am unfortunately finding JBehave's docs to be a bit of a nightmare, especially when it comes to Maven integration. Moreover a lot of the code I found both on their website and elsewhere didn't work. Through trial and error, and lots of tutorials, I was able to piece together the following. It runs both in Maven and Eclipse, has a single binding class that maps stories to step files, and is able to find story files located in src/test/resources.
here is a working pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.projectvalis.st1</groupId>
<artifactId>st1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>st1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.and.surefire.version}</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>4.0.2</version>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<ignoreFailureInStories>false</ignoreFailureInStories>
<ignoreFailureInView>false</ignoreFailureInView>
<systemProperties>
<property>
<name>java.awt.headless</name>
<value>true</value>
</property>
</systemProperties>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>
</project>
here is a sample story file
Narrative:
In order to work with files to compress
As a guy who wants to win a bet with cameron
I want to ensure files are ingested and processed in the manner in which the
methods in the ingest class purport to process them.
Scenario: Simple test to give JBehave a test drive
Given a file, a.log
When the caller loads the file as a byte array
Then the byte array that is returned contains the correct number of bytes.
here is a sample step file
package com.projectvalis.compUtils.tests.ingest;
import java.io.File;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.steps.Steps;
import org.junit.Assert;
import com.projectvalis.compUtils.util.fileIO.Ingest;
/**
* BDD tests for the ingest class
* #author funktapuss
*
*/
public class LoadByteSteps extends Steps {
private String fNameS;
private byte[] byteARR;
#Given("a file, $filename")
public void setFileName(#Named("filename") String filename) {
File file = new File(getClass().getResource("/" + filename).getFile());
fNameS = file.getPath();
}
#When("the caller loads the file as a byte array")
public void loadFile() {
byteARR = Ingest.loadFile(fNameS);
}
#Then("the byte array that is returned contains the "
+ "correct number of bytes.")
public void checkArrSize() {
File file = new File(fNameS);
Assert.assertTrue(
"loading error - "
+ "the file and the resultant byte array are different sizes!",
(long)byteARR.length == file.length());
}
}
and here is the generic runner
package com.projectvalis.compUtils.tests.runner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.Steps;
import com.projectvalis.compUtils.tests.ingest.LoadByteSteps;
/**
* generic binder for all JBehave tests. Binds all the story files to the
* step files. works for both Eclipse and Maven command line build.
* #author funktapuss
*
*/
public class JBehaveRunner_Test extends JUnitStories {
#Override
public Configuration configuration() {
return new MostUsefulConfiguration()
.useStoryLoader(
new LoadFromClasspath(this.getClass().getClassLoader()))
.useStoryReporterBuilder(
new StoryReporterBuilder()
.withDefaultFormats()
.withFormats(Format.HTML, Format.CONSOLE)
.withRelativeDirectory("jbehave-report")
);
}
#Override
public InjectableStepsFactory stepsFactory() {
ArrayList<Steps> stepFileList = new ArrayList<Steps>();
stepFileList.add(new LoadByteSteps());
return new InstanceStepsFactory(configuration(), stepFileList);
}
#Override
protected List<String> storyPaths() {
return new StoryFinder().
findPaths(CodeLocations.codeLocationFromClass(
this.getClass()),
Arrays.asList("**/*.story"),
Arrays.asList(""));
}
}
the runner lives in src/test/java//tests.runner.
the ingest test lives in src/test/java//tests.ingest.
the story files live in src/test/resources/stories.
As far as I can tell, JBehave has LOTS of options, so this certainly isn't the only way of doing things. Treat this like a template that will get you up and running quickly.
full source is on github.
Following step by step closely the jbehave Getting Started tutorial, the Run story section says: [...] the ICanToggleACell.java class will allow itself to run as a JUnit test.
This means that the JUnit library is required in your Build path.
Using Eclipse:
Select your current project and right-click it, Build path, Configure Build Path...
Properties for [current project], Java Build Path, Libraries, click [Add Library...]
Add Library, select JUnit, click [Next]
JUnit Library, JUnit library version, select the version you wish to use, click [Finish]
Java Build Path, click [OK]
Project Explorer, select your ICanToggleACell.java class, right-click it, then Run As, and click on JUnit Test
So this is the same here as for the above-example code. The StackBehaviourStory.java class should let itself run as a JUnit test after you add the proper library to the Java build path.
In my case, I have extended my Steps class from Steps (from jbehave core)
i had updated the JunitStory to JunitStories and it worked
public class StackBehaviourStory extends JUnitStory ---> JunitStories

Injecting Maven project information into Swing Application Framework resources?

I have a Maven project using the Swing Application Framework and would like to inject project information from the pom.xml into my application's global resources to avoid duplication.
The base application (provided via netbeans) uses Application.title, Application.version, Application.vendor, Application.description resources etc for Window titles and about box configuration but I can't find a way to set these values programatically at run time and I'm not a maven maven so don't have the skills to inject them at build time.
Anyone have any recommendations on how best to achieve the desired result?
You could try using filtered resources. If you create a property file, say src/main/resources/com/myapp/app.properties that looks like this:
version=${project.version}
name=${project.name}
id=${project.artifactId}
Them you need to enable filtering in your pom.xml:
<build>
<resources>
<resource>src/main/resources</resource>
<filtering>true</filtering>
</resources>
</build>
Now when maven builds your project, it'll expand the property file, and place it on the classpath. Then you can just call getResourceAsStream("/com/myapp/app.properties") to read it into your app.
Whist maven does automatically create a file /META-INF/maven/$groupId/$artifactId/pom.properties, this may not have all the information you need.
You can keep those in separte property file and read it from both pom.xml and your application.
Another option is to read pom.xml file from classpath (mvn will put it in META-INF folder) and parse it from there as plain xml file.
I would go with first option.
I would try using the maven-antrun-plugin. Pass the necessary maven properties to ant and create an ant build script which modifies an application properties file or the spring context configuration directly.
Another way would be to generate a separate properties file with the properties-maven-plugin and then add this properties file to the application bundle names:
For the pom.xml to write application.properties file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>${project.build.outputDirectory}/application.properties</outputFile>
</configuration>
</plugin>
...
</plugins>
...
</build>
For including the application.properties into your application:
public class MyApplication extends SingleFrameApplication
public MyApplication() {
super();
addGeneratedApplicationProperties();
}
private void addGeneratedApplicationProperties() {
ResourceManager resourceManager = getContext().getResourceManager();
getContext().setApplicationClass(MyApplication.class);
List<String> bundleNames = new LinkedList<String>(resourceManager.getApplicationBundleNames());
bundleNames.add(0, "application");
resourceManager.setApplicationBundleNames(bundleNames);
}
...
}
However, I find the maven-filter-solution way more elegant.

Categories

Resources