I just got up and running with Kafka 0.8 beta 1. I have a really simple example up and running, the problem is, I can only get one message consumer to work, not several. That is, the runSingleWorker() method WORKS. The run() method DOES NOT WORK:
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.consumer.ConsumerConfig;
import kafka.javaapi.consumer.ConsumerConnector;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.truecar.inventory.worker.core.application.config.AppConfig;
public class ConsumerThreadPool {
private final ConsumerConnector consumer;
private final String topic;
private ExecutorService executor;
private static ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
public ConsumerThreadPool(String topic) {
consumer = kafka.consumer.Consumer.createJavaConsumerConnector((ConsumerConfig)context.getBean("consumerConfig"));
this.topic = topic;
}
public void shutdown() {
if (consumer != null) consumer.shutdown();
if (executor != null) executor.shutdown();
}
public void run(Integer numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, numThreads);
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> topicListeners = consumerMap.get(topic);
executor = Executors.newFixedThreadPool(numThreads);
for(Integer i = 0; i < numThreads; i++ ){
KafkaStream<byte[], byte[]> stream = topicListeners.get(i);
executor.submit(new Consumer(stream, i));
}
}
public void runSingleWorker(Integer numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(it.hasNext()){
System.out.println(new String(it.next().message()));
}
}
}
}
And inside my toy consumer:
import kafka.consumer.KafkaStream;
import kafka.consumer.ConsumerIterator;
public class Consumer implements Runnable {
private KafkaStream kafkaStream;
private Integer threadNumber;
public Consumer(KafkaStream kafkaStream, Integer threadNumber) {
this.threadNumber = threadNumber;
this.kafkaStream = kafkaStream;
}
public void run() {
ConsumerIterator<byte[], byte[]> it = kafkaStream.iterator();
System.out.println("Created iterator " + it.toString() + " thread number " + threadNumber);
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
while(it.hasNext()) {
System.out.println("Thread " + threadNumber + ": " + new String(it.next().message()));
}
}
System.out.println("Shutting down Thread: " + threadNumber);
}
}
The problem is, the pool of workers does not pick up messages:
Created iterator empty iterator thread number 3
Created iterator empty iterator thread number 6
Created iterator empty iterator thread number 9
Created iterator empty iterator thread number 7
Created iterator empty iterator thread number 0
Created iterator empty iterator thread number 0
Created iterator empty iterator thread number 8
Created iterator empty iterator thread number 3
etc...
When I add messages via the produce command line, the messages are printed under the single threaded worker version, but messages are not printed under the multi-stream situation. Whats going on here? How can I fix this?
Btw, the pom.xml for kafka 0.8 is not a valid pom and will not acquire dependencies, so here is a pom with complete dependencies.
<?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>group1</groupId>
<artifactId>artifact1</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.0-beta1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>com.yammer.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
<finalName>inventory-core</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.truecar.inventory.worker.core.application.Starter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<onejarVersion>0.97</onejarVersion>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
</project>
Maybe too late for questioner but could be useful to other developers.
Seems you have used only one Partition for couple of Consumers – that's wrong.
Quote from Documentation:
Since there are many partitions this still balances the load over many consumer instances. Note however that there cannot be more consumer instances than partitions.
So when you think about Consumers you should think how to divide messages by partitions.
In most cases you should use some high-level grouping to it or even let it be random by default.
Related
I follow tutorial at https://www.youtube.com/watch?v=AjpSdbdvIHw
My code
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.anil.hz</groupId>
<artifactId>H</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
File Server.java
package com.anil.server;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class Server {
public static void main(String[] args) {
HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
IMap<Long, String> map = hzInstance.getMap("testMap");
map.put(1L, "one");
map.put(2L, "two");
}
}
File Student.java
package com.anil.server;
public class Student {
private Long id;
private String sName;
private String city;
public Student(Long id, String sName, String city) {
this.id = id;
this.sName = sName;
this.city = city;
}
}
File Server.java
package com.anil.server;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class Server {
public static void main(String[] args) {
HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
IMap<Long, String> map = hzInstance.getMap("testMap");
map.put(1L, "one");
map.put(2L, "two");
}
}
File HZConfig.java
package com.anil.server;
import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
public class HZConfig {
public static Config getConfig(){
Config config = new Config();
NetworkConfig network = config.getNetworkConfig();
network.setPort(5710).setPortCount(20);
network.setPortAutoIncrement(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().addMember("localhost").setEnabled(true);
config.getManagementCenterConfig().setEnabled(true);
config.getManagementCenterConfig().setUrl("http://localhost:8081/mancenter");
return config;
}
}
File Client.java
package com.anil.server;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class Client {
public static void main(String[] args) {
HazelcastInstance hzClient = null;
try {
hzClient = HazelcastClient.newHazelcastClient();
IMap<Long, String> testMap = hzClient.getMap("testMap");
System.out.println(testMap.get(1L));
} finally {
hzClient.shutdown();
}
}
}
I catch error at
What are new API for old thing HazelcastInstance hzClient = HazelcastClient.newHazelcastClient(); ?
You are using the following dependency
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.11</version>
</dependency>
This does not contain the client. The tutorial changes that dependency to hazelcast-all at 3:45, which has the client included. You can also include the client separately using hazelcast-client artifactId.
However 3.11 is a really old version, not supported anymore, you should use the latest release - 5.1 currently. In this release, there is no hazelcast-all jar anymore and the client is included in the main jar. Note that there are some breaking changes between 3.x and 4.x/5.x, but it's mostly just package names.
Base on answer from František Hartman, I add more detail
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.anil.hz</groupId>
<artifactId>H</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
File Client.java
package com.anil.server;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
public class Client {
public static void main(String[] args) {
HazelcastInstance hzClient = null;
try {
hzClient = HazelcastClient.newHazelcastClient();
IMap<Long, String> testMap = hzClient.getMap("testMap");
System.out.println(testMap.get(1L));
} finally {
hzClient.shutdown();
}
}
}
File Server.java
package com.anil.server;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
public class Server {
public static void main(String[] args) {
HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
IMap<Long, String> map = hzInstance.getMap("testMap");
map.put(1L, "one");
map.put(2L, "two");
}
}
File HZConfig.java
package com.anil.server;
import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
public class HZConfig {
public static Config getConfig(){
Config config = new Config();
NetworkConfig network = config.getNetworkConfig();
network.setPort(5710).setPortCount(20);
network.setPortAutoIncrement(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().addMember("localhost").setEnabled(true);
config.getManagementCenterConfig().setConsoleEnabled(true);
config.getManagementCenterConfig().setDataAccessEnabled(true);
config.getManagementCenterConfig().setScriptingEnabled(true);
// config.getManagementCenterConfig().setUrl("http://localhost:8081/mancenter"); // ?
return config;
}
}
I want to use picocli with jline3. So I create a project with the following pom.xml:
<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.flaxel</groupId>
<artifactId>picocli_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>picocli</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<picocli.version>3.9.3</picocli.version>
<jline.version>3.9.0</jline.version>
</properties>
<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli-shell-jline3</artifactId>
<version>${picocli.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.flaxel.picocli.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compile-plugin</artifactId>
<version>3.7.0</version>
</plugin>
</plugins>
</build>
Now I have copied a class from the picocli github page:
package com.flaxel.picocli;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.MaskingCallback;
import org.jline.reader.ParsedLine;
import org.jline.reader.UserInterruptException;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.shell.jline3.PicocliJLineCompleter;
/**
* Example that demonstrates how to build an interactive shell with JLine3 and
* picocli.
*
* #since 3.9
*/
public class App {
/**
* Top-level command that just prints help.
*/
#Command(name = "", description = "Example interactive shell with completion", footer = { "",
"Press Ctl-D to exit." }, subcommands = { MyCommand.class, ClearScreen.class })
static class CliCommands implements Runnable {
LineReaderImpl reader;
PrintWriter out;
CliCommands() {
}
public void setReader(LineReader reader) {
this.reader = (LineReaderImpl) reader;
out = reader.getTerminal().writer();
}
#Override
public void run() {
out.println(new CommandLine(this).getUsageMessage());
}
}
/**
* A command with some options to demonstrate completion.
*/
#Command(name = "cmd", mixinStandardHelpOptions = true, version = "1.0", description = "Command with some options to demonstrate TAB-completion"
+ " (note that enum values also get completed)")
static class MyCommand implements Runnable {
#Option(names = { "-v", "--verbose" })
private boolean[] verbosity = {};
#Option(names = { "-d", "--duration" })
private int amount;
#Option(names = { "-u", "--timeUnit" })
private TimeUnit unit;
#ParentCommand
CliCommands parent;
#Override
public void run() {
if (verbosity.length > 0) {
parent.out.printf("Hi there. You asked for %d %s.%n", amount, unit);
} else {
parent.out.println("hi!");
}
}
}
/**
* Command that clears the screen.
*/
#Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true, description = "Clears the screen", version = "1.0")
static class ClearScreen implements Callable<Void> {
#ParentCommand
CliCommands parent;
#Override
public Void call() throws IOException {
parent.reader.clearScreen();
return null;
}
}
public static void main(String[] args) {
try {
// set up the completion
CliCommands commands = new CliCommands();
CommandLine cmd = new CommandLine(commands);
Terminal terminal = TerminalBuilder.builder().build();
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new PicocliJLineCompleter(cmd.getCommandSpec()))
.parser(new DefaultParser())
.build();
commands.setReader(reader);
String prompt = "prompt> ";
String rightPrompt = null;
// start the shell and process input until the user quits with Ctrl-D
String line;
while (true) {
try {
line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
ParsedLine pl = reader.getParser().parse(line, 0);
String[] arguments = pl.words().toArray(new String[0]);
CommandLine.run(commands, arguments);
} catch (UserInterruptException e) {
// Ignore
} catch (EndOfFileException e) {
return;
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
If I run the code in the Eclipse IDE, I can write a command in the console and I get an answer. But if I create a jar file with maven, it does not work. I get the following error:
Error: Unable to initialize main class com.flaxel.picocli.App
Caused by: java.lang.NoClassDefFoundError: org/jline/reader/UserInterruptException
I work with Eclipse 2018-09 and Java 11.
The NoClassDefFoundError occurs because you are missing the JLine classes when running your jar. Try using the shade Maven plugin to create an Uber-jar that contains all the dependencies.
With JLine 3 on Windows, you probably want to add the org.jline:jline-terminal-jansi:3.9.0 dependency in addition to info.picoli:picocli-shell-jline3:3.9.3. Without this (or the jline-terminal-jna dependency) you will get a “dumb” terminal without ANSI colors or autocompletion.
(info.picoli:picocli-shell-jline3:3.9.3 will bring in info.picocli:picocli and org.jline:jline as transitive dependencies, so there is no need to include these explicitly.)
Perfect that works for me. I use the maven shade plugin and add some properties, so I get this pom.xml:
<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.flaxel</groupId>
<artifactId>picocli_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>picocli</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<picocli.version>3.9.3</picocli.version>
<jline.version>3.9.0</jline.version>
</properties>
<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli-shell-jline3</artifactId>
<version>${picocli.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.flaxel.picocli.App</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compile-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
EDIT
I have at least one question. If I start the application and want to use the tab completion, it does not work. So I get no completion, only four spaces are set:
Feb. 07, 2019 6:50:29 NACHM. org.jline.utils.Log logr
WARNING: Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
prompt> cmd
Or did I something wrong?
I am trying to execute a simple Java example that uses the AWS Polly service. I am using the code provided by AWS on their documentation. I created a simple Maven Project using the following -**
1. group id - com.amazonaws.polly
2. artifact id - java-demo
3. version - 0.0.1-SNAPSHOT
Following is my project structure -
Following is my pom.xml -
<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.amazonaws.polly</groupId>
<artifactId>java-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-polly -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-polly</artifactId>
<version>1.11.77</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.soundlibs/jlayer -->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.amazonaws.demos.polly.PollyDemo</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Following is my java class -
package com.amazonaws.demos.polly;
import java.io.IOException;
import java.io.InputStream;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.polly.AmazonPollyClient;
import com.amazonaws.services.polly.model.DescribeVoicesRequest;
import com.amazonaws.services.polly.model.DescribeVoicesResult;
import com.amazonaws.services.polly.model.OutputFormat;
import com.amazonaws.services.polly.model.SynthesizeSpeechRequest;
import com.amazonaws.services.polly.model.SynthesizeSpeechResult;
import com.amazonaws.services.polly.model.Voice;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
public class PollyDemo {
private final AmazonPollyClient polly;
private final Voice voice;
private static final String SAMPLE = "Congratulations. You have successfully built this working demo "+
"of Amazon Polly in Java. Have fun building voice enabled apps with Amazon Polly (that's me!), and always"+
"look at the AWS website for tips and tricks on using Amazon Polly and other great services from AWS";
public PollyDemo(Region region) {
//Didn't work
//AWSCredentials credentials = new BasicAWSCredentials("someAccessKey","someSecretKey");
//polly = new AmazonPollyClient(credentials);
//Didn't work
// create an Amazon Polly client in a specific region
polly = new AmazonPollyClient(new DefaultAWSCredentialsProviderChain(),
new ClientConfiguration());
polly.setRegion(region);
// Create describe voices request.
DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest();
// Synchronously ask Amazon Polly to describe available TTS voices.
DescribeVoicesResult describeVoicesResult = polly.describeVoices(describeVoicesRequest);
voice = describeVoicesResult.getVoices().get(0);
}
public InputStream synthesize(String text, OutputFormat format) throws IOException {
SynthesizeSpeechRequest synthReq =
new SynthesizeSpeechRequest().withText(text).withVoiceId(voice.getId())
.withOutputFormat(format);
SynthesizeSpeechResult synthRes = polly.synthesizeSpeech(synthReq);
return synthRes.getAudioStream();
}
public static void main(String args[]) throws Exception {
//create the test class
PollyDemo helloWorld = new PollyDemo(Region.getRegion(Regions.US_EAST_1));
//get the audio stream
InputStream speechStream = helloWorld.synthesize(SAMPLE, OutputFormat.Mp3);
//create an MP3 player
AdvancedPlayer player = new AdvancedPlayer(speechStream,
javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice());
player.setPlayBackListener(new PlaybackListener() {
#Override
public void playbackStarted(PlaybackEvent evt) {
System.out.println("Playback started");
System.out.println(SAMPLE);
}
#Override
public void playbackFinished(PlaybackEvent evt) {
System.out.println("Playback finished");
}
});
// play it!
player.play();
}
}
I am running the code locally, therefore I have my AWS IAM credentials configured in my system,
My IAM user also has access to AWS Polly service.
I am getting the following error when I run the code -
Exception in thread "main" com.amazonaws.services.polly.model.AmazonPollyException: The security token included in the request is invalid. (Service: AmazonPolly; Status Code: 403; Error Code: UnrecognizedClientException; Request ID: 4d4b01fb-8015-11e8-8e18-4548f95fba92)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1586)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1254)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
at com.amazonaws.services.polly.AmazonPollyClient.doInvoke(AmazonPollyClient.java:668)
at com.amazonaws.services.polly.AmazonPollyClient.invoke(AmazonPollyClient.java:644)
at com.amazonaws.services.polly.AmazonPollyClient.describeVoices(AmazonPollyClient.java:383)
at com.amazonaws.demos.polly.PollyDemo.<init>(PollyDemo.java:39)
at com.amazonaws.demos.polly.PollyDemo.main(PollyDemo.java:54)
I am referring the following AWS Doc for the Polly java example-
https://docs.aws.amazon.com/polly/latest/dg/examples-java.html
Can someone help fix my code? What do I change in my code?
It's a 403 error. Where are you passing the AWS access and secret key? You can try this
* Constructs a new client to invoke service methods on AmazonPolly. A
* credentials provider chain will be used that searches for credentials in
* this order:
* <ul>
* <li>Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY</li>
* <li>Java System Properties - aws.accessKeyId and aws.secretKey</li>
* <li>Instance profile credentials delivered through the Amazon EC2
* metadata service</li>
* </ul>
* <p>
* All service calls made using this new client object are blocking, and
* will not return until the service call completes.
*
* #see DefaultAWSCredentialsProviderChain
*/
public AmazonPollyClient() {
this(new DefaultAWSCredentialsProviderChain(), new ClientConfiguration());
}
https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-polly/src/main/java/com/amazonaws/services/polly/AmazonPollyClient.java
Replace
new DefaultAWSCredentialsProviderChain()
with
AWSStaticCredentialsProvider(new BasicAWSCredentials("AccessKey", "Secret Key"))
I noticed there is no POM file in the Github repository for this service. We will fix that. Here is a POM file that works.
<?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>pollyJ1Project</groupId>
<artifactId>pollyJ1Project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-polly -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-polly</artifactId>
<version>1.11.774</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.awaitility/awaitility -->
<!-- https://mvnrepository.com/artifact/org.awaitility/awaitility -->
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Also - here is an example you can try:
package com.amazonaws.polly.samples;
import com.amazonaws.services.polly.AmazonPolly;
import com.amazonaws.services.polly.AmazonPollyClientBuilder;
import com.amazonaws.services.polly.model.DescribeVoicesRequest;
import com.amazonaws.services.polly.model.DescribeVoicesResult;
public class DescribeVoicesSample {
public static void main(String[] args) {
AmazonPolly client = AmazonPollyClientBuilder.defaultClient();
describeVoices(client);
}
public static void describeVoices(AmazonPolly client ) {
DescribeVoicesRequest allVoicesRequest = new DescribeVoicesRequest();
DescribeVoicesRequest enUsVoicesRequest = new DescribeVoicesRequest()
.withLanguageCode("en-US");
try {
String nextToken;
do {
DescribeVoicesResult allVoicesResult =
client.describeVoices(allVoicesRequest);
nextToken = allVoicesResult.getNextToken();
allVoicesRequest.setNextToken(nextToken);
System.out.println("All voices: " + allVoicesResult.getVoices());
} while (nextToken != null);
do {
DescribeVoicesResult enUsVoicesResult = client.describeVoices(enUsVoicesRequest);
nextToken = enUsVoicesResult.getNextToken();
enUsVoicesRequest.setNextToken(nextToken);
System.out.println("en-US voices: " + enUsVoicesResult.getVoices());
} while (nextToken != null);
} catch (Exception e) {
System.err.println("Exception caught: " + e);
}
}
}
The above code is V1. You can find V2 code examples here:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/polly
I found a nice working demo here https://youtu.be/WMMSQAn_vHI. It is working java example for AWS Polly service using new DefaultAWSCredentialsProviderChain() only.
So, I'm trying to launch a jar application that I've built, but I'm encountering a very weird issue. When I tried to launch the application on the command line using java -jar MyCookie.jar, I received the following error: Error: Could not find or load main class
Now I know that this is a common error. It is basically telling me that the JVM can't find the main class (or that there is an issue with the main class). Naturally, the first things that I checked were:
A) Did I use the right FQCN, and spell it properly? YES.
B) Did I use MyCookie.jar instead of simply MyCookie in CMD, and main.java.MyCookie.mainClass in the manifest instead of main.java.MyCookie.mainClass.class? YES.
C) Is the desired class actually located in the JAR, or in the Class-Path? YES, The class is located in the main.java.MyCookie package, located within the MyCookie.jar.
After checking these things, I suspected something might be off with Java, especially as I have 4 different java installations on my machine (jre7, jre1.8.0_101, jdk1.7.0_80, jdk1.8.0_101), so I attempted to run the executable jar application on the command line with each different version.
Interestingly enough, I discovered that both the 1.8.0 jre and JDK produced the previously mentioned error, but that the application executed properly using both jre7 and jdk1.7.0_80. Not surprisingly java -jar MyCookie.jar wasn't working because my "java" was set to jdk1.8.0.
It may be a long-shot, but does anyone have any idea why my application may work in JSE7, but trigger a failure to load main class in 1.8.0?
I'm using Maven as a build tool, my main class is mainView, which looks like this:
package main.java.myCookie;
import main.java.gls.entities.Emp;
import main.java.gls.entities.JobStatus;
import main.java.gls.event.StringEventListener;
import main.java.gls.util.JustOneLock;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.swing.JComponent;
import main.java.myCookie.background.fetchEmpList;
import main.java.myCookie.background.fetchStatusList;
import main.java.myCookie.interfaces.Main;
public class mainView
extends javax.swing.JFrame
implements Main
{
#PersistenceUnit
final private EntityManagerFactory emFactory = javax.persistence.Persistence.createEntityManagerFactory("JobEntitiesPU");
#PersistenceContext
final private EntityManager em = emFactory.createEntityManager();
Properties config = new Properties();
ArrayList<EventListener> listenerList = new ArrayList<EventListener>();
//set from property file in working directory
String defaultPrinter;
ArrayList<Integer> statusExceptionList = new ArrayList<Integer>();
ArrayList<Integer> statusLockedList = new ArrayList<Integer>();
ArrayList<JobStatus> adjustedStatusList = new ArrayList<JobStatus>();
ArrayList<JobStatus> statusList = new ArrayList<JobStatus>();
boolean statusListFetched = false;
int defaultEmpId;
Emp defaultEmp;
Emp noEmp;
ArrayList<Emp> empList = new ArrayList<Emp>();
boolean empListFetched = false;
PanelManager panelManager;
public mainView()
{
lockTest();
init();
}
private void lockTest()
{
JustOneLock jol = new JustOneLock("TimeClock");
if ( jol.isAppActive() )
{
System.out.println("Instance of TimeClock is already Running");
System.exit(1);
}
}
private void init()
{
MainSingleton.getInstance().setMain(this);
this.openPropertiesFromFile();
this.fetchEmpList();
this.fetchStatusList();
initComponents();
panelManager = new PanelManager();
this.setVisible(true);
}
private void openPropertiesFromFile()
{
System.out.println( "Current Working Directory: "+System.getProperty("user.dir"));
//get properties file
try
{
this.config.load(new FileInputStream( System.getProperty("user.dir")+"\\main.java.myCookie.properties"));
} catch (IOException ex)
{
Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
}
//get default user if it exists
if ( config.containsKey("DEFAULT_USER") )
{
System.out.println( "DEFAULT_USER: "+config.getProperty("DEFAULT_USER") );
this.defaultEmpId = Integer.parseInt( config.getProperty("DEFAULT_USER") );
}
else
{
System.out.println( "DEFAULT_USER: NOT FOUND (set to 1000)" );
this.defaultEmpId = 1000;
}
//get default printer if it exists
if ( config.containsKey("DEFAULT_PRINTER") )
{
System.out.println( "DEFAULT_PRINTER: "+config.getProperty("DEFAULT_PRINTER") );
this.defaultPrinter = config.getProperty("DEFAULT_PRINTER");
}
else
{
System.out.println( "DEFAULT_PRINTER: NOT FOUND" );
this.defaultPrinter = "myPrinter";
}
//get status exceptions if it exists ( comma delineated )
if ( config.containsKey("STATUS_EXCEPTIONS") )
{
System.out.println( "STATUS_EXCEPTIONS list found" );
String[] rawSE = config.getProperty("STATUS_EXCEPTIONS").split(",");
for ( String s : rawSE )
{
this.statusExceptionList.add(Integer.parseInt(s));
}
}
else
{
System.out.println( "STATUS_EXCEPTIONS list NOT found" );
}
//get locked status list if it exists ( coma delineated )
if ( config.containsKey("STATUS_LOCKED") )
{
System.out.println( "STATUS_LOCKED list found" );
String[] rawSL = config.getProperty("STATUS_LOCKED").split(",");
for ( String s : rawSL )
{
this.statusLockedList.add(Integer.parseInt(s));
}
}
else
{
System.out.println( "STATUS_LOCKED list NOT found" );
}
}
private void fetchEmpList()
{
final fetchEmpList task = new fetchEmpList();
task.addPropertyChangeListener
(
new PropertyChangeListener()
{
#Override
public void propertyChange( PropertyChangeEvent evt )
{
if ( "DONE".equals( evt.getNewValue().toString() ) )
{
try {
fetchEmpListDone(task.get());
} catch (InterruptedException ex) {
Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
);
task.execute();
}
private void fetchStatusList()
{
final fetchStatusList task = new fetchStatusList();
task.addPropertyChangeListener
(
new PropertyChangeListener()
{
#Override
public void propertyChange( PropertyChangeEvent evt )
{
if ( "DONE".equals( evt.getNewValue().toString() ) )
{
try{
fetchStatusListDone( task.get() );
} catch (InterruptedException ex)
{
Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex)
{
Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
);
task.execute();
}
private void fetchEmpListDone( List<Emp> empList )
{
for( Emp e : empList )
{
//removes non-current employees
if ( e.getEmpQuit() == null )
{
this.empList.add(e);
}
//employee 1000: No Owner
if ( e.getEmpNum() == 1000 )
{
this.noEmp = e;
}
//sets default emp
if ( e.getEmpNum() == this.defaultEmpId )
{
this.defaultEmp = e;
}
}
this.empListFetched = true;
}
private void fetchStatusListDone( List<JobStatus> statusList )
{
//only add status types that are not in the exception list
for( JobStatus js : statusList )
{
this.statusList.add(js);
if ( !this.statusExceptionList.contains(js.getId()) )
{
this.adjustedStatusList.add(js);
}
}
this.statusListFetched = true;
}
#Override
public ArrayList<Integer> getStatusExceptionList()
{
return this.statusExceptionList;
}
#Override
public ArrayList<JobStatus> getAdjustedStatusList()
{
return this.adjustedStatusList;
}
#Override
public ArrayList<Integer> getStatusLockedList()
{
return this.statusLockedList;
}
#Override
public String getDefaultPrinter()
{
return this.defaultPrinter;
}
#Override
public Emp getDefaultEmp()
{
return this.defaultEmp;
}
#Override
public Emp getNoEmp()
{
return this.noEmp;
}
#Override
public ArrayList<Emp> getEmpList()
{
return this.empList;
}
#Override
public boolean empListFetched()
{
return this.empListFetched;
}
#Override
public List<JobStatus> getStatusList()
{
return this.statusList;
}
#Override
public boolean statusListFetched()
{
return this.statusListFetched;
}
#Override
public EntityManager getEM()
{
return this.em;
}
#Override
public JComponent getParentContainer()
{
return this.mainPanel;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
private void initComponents() {
myCookieLabel = new javax.swing.JLabel();
mainPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("myCookie");
setMinimumSize(new java.awt.Dimension(640, 480));
setResizable(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
myCookieLabel.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.disabledShadow"));
myCookieLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
myCookieLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
myCookieLabel.setText("Steve Pro");
getContentPane().add(myCookieLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 640, 40));
mainPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(mainPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 40, 640, 440));
pack();
}
/**
* #param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(
new Runnable()
{
#Override
public void run()
{
new mainView();
}
}
);
}
// Variables declaration - do not modify
private javax.swing.JPanel mainPanel;
private javax.swing.JLabel myCookieLabel;
// End of variables declaration
#Override
public void addStringEventListener ( StringEventListener stringEventListener )
{
listenerList.add( stringEventListener );
}
#Override
public void removeStringEventListener ( StringEventListener stringEventListener )
{
listenerList.remove( stringEventListener );
}
private void fireStringEvent( String string )
{
for ( EventListener l : listenerList )
{
if ( l instanceof StringEventListener )
{
((StringEventListener) l).stringEventFired(string);
}
}
}
}
My POM:
<?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.glsarchives</groupId>
<artifactId>myCookie</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.netbeans.external</groupId>
<artifactId>AbsoluteLayout</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>cecore</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>celib</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>ceplugins</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>cereports</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>cesession</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>ceutils</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.lucee</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.dspace.xmlui.concurrent</groupId>
<artifactId>Concurrent</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>corbaidl</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33-bin</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.businessobjects.sdk</groupId>
<artifactId>rascore</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.lucee</groupId>
<artifactId>xml-apis</artifactId>
<version>0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.glsarchives</groupId>
<artifactId>GLSLib</artifactId>
<version>1.1.2-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<!--Build Settings-->
<build>
<sourceDirectory>./src</sourceDirectory>
<finalName>myCookie-${project.version}</finalName>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- Maven-Dependency-Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!-- Maven-Jar-Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>main.java.myCookie.mainView</mainClass>
</manifest>
<manifestEntries>
<Class-Path>lib/</Class-Path>
<Class-Path>myCookie-1.0</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- Maven Resources Plugin -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<!-- Copies resource files that can be accessed by the user (unpackaged resources), such as configuration and properties files to the installation directory -->
<execution>
<id>copy-internal-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
</includes>
<directory>${basedir}/src/main/resources/external</directory>
<targetPath>${project.build.directory}/</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/java/gls_desktop/resources</directory>
<targetPath>main/java/gls_desktop/resources</targetPath>
</resource>
<resource>
<directory>${project.basedir}/src/main/java/gls_desktop/xml</directory>
<targetPath>main/java/gls_desktop/xml</targetPath>
</resource>
<resource>
<directory>${project.basedir}/src/main/java/gls_desktop/jCalendar</directory>
<targetPath>main/java/gls_desktop/jCalendar</targetPath>
<includes>
<include>Bundle.properties</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/java/gls_desktop/jCalendar/images</directory>
<targetPath>main/java/gls_desktop/jCalendar/images</targetPath>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
MANIFEST.MF:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: bsnider
Class-Path: myCookie-1.0 lib/AbsoluteLayout-0.0.jar lib/cecore-0.0.jar
lib/celib-0.0.jar lib/ceplugins-0.0.jar lib/cereports-0.0.jar lib/ce
session-0.0.jar lib/ceutils-0.0.jar lib/commons-logging-1.1.1.jar lib
/Concurrent-0.0.jar lib/corbaidl-0.0.jar lib/eclipselink-2.5.2.jar li
b/javax.persistence-0.0.jar lib/log4j-1.2.17.jar lib/mysql-connector-
java-5.1.33-bin.jar lib/rascore-0.0.jar lib/xercesImpl-2.9.1.jar lib/
xml-apis-1.3.04.jar lib/xml-apis-0.0.jar lib/GLSLib-1.1.2-20170210.20
0207-1.jar lib/commons-lang-2.1.jar lib/serialization.jar-0.0.jar lib
/ebus405-0.0.jar lib/MetafileRenderer-0.0.jar lib/rasapp-0.0.jar lib/
javax.mail-1.5.0.jar lib/activation-1.1.jar lib/org.eclipse.persisten
ce.jpa-2.6.4.jar lib/org.eclipse.persistence.asm-2.6.4.jar lib/org.ec
lipse.persistence.antlr-2.6.4.jar lib/javax.json-1.0.4.jar lib/org.ec
lipse.persistence.jpa.jpql-2.6.4.jar lib/org.eclipse.persistence.core
-2.6.4.jar lib/imgscalr-lib-4.2.jar lib/awtextra-0.0.jar lib/ReportPr
inter-0.0.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_101
Main-Class: main.java.myCookie.mainView
This question already has answers here:
Including all the jars in a directory within the Java classpath
(25 answers)
Closed 6 years ago.
I get a
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Hex
when i execute my code.
I have looked through a number of examples on this exception on stack overflow but nothing seems to help. My pom.xml looks like this
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Code to reproduce it looks like this
package com.mypackage;
import java.util.*;
import java.io.*;
import java.security.*;
import org.apache.commons.codec.binary.Hex;
public class Test{
private byte[] m_key;
private static String resourceFolder = System.getenv("SEABED_HOME").toString() + "/testing" + "/resources";
public static void main(String[] args)
{
Test t = new Test();
t.readKey();
}
public byte[] generateKey()
{
SecureRandom scr = new SecureRandom();
byte[] key = new byte[32];
scr.nextBytes(key);
return key;
}
private void readKey()
{
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(resourceFolder + "/.seabedkey"));
m_key = Hex.decodeHex(br.readLine().toCharArray());
}
catch(Exception e)
{
if(e instanceof FileNotFoundException)
{
System.out.println("Key not found, generating now");
writeKey(generateKey());
readKey();
return;
}
e.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
private void writeKey(byte[] key)
{
try{
FileOutputStream fos = new FileOutputStream(resourceFolder + "/.seabedkey");
fos.write(key);
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
I execute it like this
java -cp target/mypackage-1.0-SNAPSHOT.jar:~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar com.mypackage.Test
It compiles but then at runtime it looks like the class cannot be found. I tried changing the scope of the dependency to provided but apparently that is not the right way to use it so I switched back to the default which is compile. I can find the jar in my .m2 folder so I am sure it is there.
You're misspelling repository in your manual classpath specification.
Probably the separator, ; for windows : for Unix
if you are running in windows
java -cp "target/mypackage-1.0-SNAPSHOT.jar;~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar" com.mypackage.Test
Linux
java -cp "target/mypackage-1.0-SNAPSHOT.jar:~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar" com.mypackage.Test