I have a SwingWorker which runs a dependency's functions
public class JarRunnerWorker extends SwingWorker<Void, Void> {
private JnlpApp jnlpApp;
public JarRunnerWorker(){}
#Override
protected Void doInBackground() {
try {
System.out.println("*** running jar ***");
jnlpApp = com.asd.bsign.App.startForJnlp();
jnlpApp.run();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void setThreadPillFalse(){
jnlpApp.setThreadPillFalse();
}
Maven dependency and build from pom.xml:
<dependency>
<groupId>com.asd.bsign</groupId>
<artifactId>bsignclient</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>com.bermuda.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This function works well in IDE and in my jar(jar and libs are in the same folder).
Here is my jnlp file(all jars in libs are added):
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://localhost:8080/bsign/" href="bsign.jnlp">
<information>
<title>Jnlp Testing</title>
<vendor>bermuda</vendor>
<homepage href="http://localhost:8080/bsign/" />
<description>jnlp Testing</description>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.6+" />
<jar href="bsignclient.jar" />
<jar href="libs/commons-discovery-0.2.jar" />
<jar href="libs/commons-io-2.8.0.jar" />
<jar href="libs/commons-logging-1.1.1.jar" />
.
.
.
</resources>
<application-desc main-class="com.bermuda.App" />
</jnlp>
When I run jnlp, jnlp executes System.out.println("*** running jar ***"); but it doesn't work, when I run setThreadPillFalse I get NullPointerException
We can think JnlpApp class like this:
public class JnlpApp implements Runnable{
private boolean pill;
public JnlpApp(){
pill = true;
}
#Override
public void run() {
while(pill){
System.out.println("Doing some job");
Thread.sleep(5000);
}
}
public void setThreadPillTrue(){
connectionMonitor.setPill(true);
}
public void setThreadPillFalse(){
connectionMonitor.setPill(false);
}
}
And startForJnlp creates a JnlpApp and returns it.
How can I run this function from jnlp?
Related
following this example and this guide I created a program that works in Eclipse development environment, however if I try to package it via maven (mvn clean package) and run it standalone I get this error:
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.SecurityException:
Invalid signature file digest for Manifest main attributes
I tried several ways (including using other maven plugin, like maven-assembly-plugin with jar-with-dependencies descriptorRef), without success getting different error.
this is my main (and only) class:
package it.factory.pub;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
public class PublishWithErrorHandler {
//
private static final Logger log = LogManager.getLogger(PublishWithErrorHandler.class);
//
public static void main(String... args) throws Exception {
String projectId = args[0];
String topicId = args[1];
String[] messages = Arrays.copyOfRange(args, 2, args.length);
log.info("projectId: " + projectId);
log.info("projectId: " + topicId);
log.info("messages: " + Arrays.toString(messages));
publishWithErrorHandlerExample(projectId, topicId, messages);
}
private static void publishWithErrorHandlerExample(String projectId, String topicId, String[] messages) throws IOException, InterruptedException {
TopicName topicName = TopicName.of(projectId, topicId);
Publisher publisher = null;
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.newBuilder(topicName).build();
// List<String> messages = Arrays.asList("first message", "second message");
for (final String message : messages) {
log.info("Publishing message: '" + message + "'");
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// Once published, returns a server-assigned message id (unique within the topic)
ApiFuture<String> future = publisher.publish(pubsubMessage);
// Add an asynchronous callback to handle success / failure
ApiFutures.addCallback(future, new ApiFutureCallback<String>(){
#Override
public void onFailure(Throwable throwable) {
if (throwable instanceof ApiException) {
ApiException apiException = ((ApiException) throwable);
// details on the API exception
log.info(apiException.getStatusCode().getCode());
log.info(apiException.isRetryable());
}
log.error("Error publishing message : " + message);
}
#Override
public void onSuccess(String messageId) {
// Once published, returns server-assigned message ids (unique within the topic)
log.info("Published message ID: " + messageId);
}
}, MoreExecutors.directExecutor());
}
} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
publisher.awaitTermination(1, TimeUnit.MINUTES);
}
}
}
}
and this is my pom.xml 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>it.factory</groupId>
<artifactId>poc-pubsub</artifactId>
<version>0.0.2-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<customer>a2a</customer>
<log4j2.version>2.12.1</log4j2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>it.factory.pub.PublishWithErrorHandler</Main-Class>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- pub-sub google -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>19.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I am guessing you already figured this out, but there is a typo in pom.xml, it should be mainClass instead of Main-Class.
Aside from that, what resolved the issue for me was to exclude additional types:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>it.factory.pub.PublishWithErrorHandler</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Since there were some additional SF and RSA files which were triggering the error
I'm trying to use jaxb2-maven-plugin to generate xsd from java beans.
But I constantly get the following error:
java.time.LocalDate is a non-static inner class, and JAXB can't handle those.
this problem is related to the following location:
at java.time.LocalDate (Unknown Source)
The bean is like:
public class MyBean {
private LocalDate date;
}
I therefore created a jaxb mapping file and an adapter converting between java.time.LocalDate and xs:date. But the error just remains the same.
public class LocalDateAdapter {
private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
public static LocalDate unmarshal(String v) throws Exception {
return fmt.parseLocalDate(v);
}
public static String marshal(LocalDate v) throws Exception {
return v.toString("yyyyMMdd");
}
}
src/main/resources/xsdbindings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<globalBindings>
<javaType name="java.time.LocalDate" xmlType="xs:date"
parseMethod="my.path.LocalDateAdapter.unmarshal"
printMethod="my.path.LocalDateAdapter.marshal"
/>
</globalBindings>
</bindings>
maven pom.xml
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<bindingFiles>xsdbindings.jaxb</bindingFiles>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>
I've got an embeded OSGi framework (Felix).
I'm installing the basic bundles without problem, I can access the Felix webconsole.
But starting libra-commons-osgi-core-wsbundle-1.4.0.war gives me this error :
"org.osgi.framework.BundleException: Unable to resolve hu.libra.commnos.osgi.core.wsbundle [9](R 9.0): missing requirement [hu.libra.commnos.osgi.core.wsbundle [9](R 9.0)] osgi.wiring.package; (osgi.wiring.package=hu.libra.commnos.osgi.core.service) Unresolved requirements: [[hu.libra.commnos.osgi.core.wsbundle [9](R 9.0)] osgi.wiring.package; (osgi.wiring.package=hu.libra.commnos.osgi.core.service)]"
hu.libra.commnos.osgi.core.service bundle is installed and started (Felix webconsole shows bundle as Active and shows the registered service)
hu.libra.commnos.osgi.core.service\META-INF\MANIFEST.MF contains
Export-Package: hu.libra.commons.osgi.core.service;version="1.4.0";uses:="org.osgi.framework"
hu.libra.commnos.osgi.core.wsbundle\META-INF\MANIFEST.MF contains
Import-Package: org.osgi.framework;version="[1.8,2)",javax.servlet,jav
ax.servlet.http,hu.libra.commnos.osgi.core.service
What is the problem? Thank You!
My embeded Framework :
.java
package hu.libra.commons.osgi.core;
import static org.osgi.framework.Constants.FRAGMENT_HOST;
import static org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN;
import static org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
public class LibraOSGiCore {
//LOGGING
private static final Logger log = Logger.getLogger(LibraOSGiCore.class);
//FIELDS
private static final Framework framework;
private static final List<Bundle> bundleList = new LinkedList<>();
//CONSTRUCTORS
static {
try {
//log4j
PropertyConfigurator.configure("log4j.properties");
//config
Map<String, String> config = new HashMap<>();
config.put(FRAMEWORK_STORAGE_CLEAN, FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
setConfig(config);
//framework
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
framework = frameworkFactory.newFramework(config);
framework.start();
}
catch (RuntimeException | BundleException e) {
log.fatal(e, e);
throw new RuntimeException(e);
}
}
//MAIN
public static void main(
String[] args) {
try {
try {
installBundles();
startBundles();
}
catch (IOException | BundleException e) {
log.fatal(e, e);
throw new RuntimeException(e);
}
try {
framework.waitForStop(0);
}
catch (InterruptedException e) {
return;
}
}
finally {
System.exit(0);
}
}
//METHODS - private
private static void setConfig(
Map<String, String> config) {
//TODO use config.xml
config.put("org.osgi.service.http.port", "8181");
//config.put("felix.webconsole.username", "...");
//config.put("felix.webconsole.password", "...");
}
private static Bundle installBundle(
String name,
String postfix,
String ext)
throws IOException,
BundleException {
if (postfix != null) {
postfix = "-" + postfix;
}
else {
postfix = "";
}
String resourceName = name + postfix + "." + ext;
InputStream is;
File file = new File(resourceName);
if (file.exists()) {
is = new FileInputStream(file);
}
else {
is = LibraOSGiCore.class.getResourceAsStream(resourceName);
}
try(InputStream xis = is) {
Bundle bundle = framework.getBundleContext().installBundle("file:/" + name + "." + ext, is);
return bundle;
}
}
private static void installBundles()
throws IOException,
BundleException {
//Felix basic bundles
bundleList.add(installBundle("org.apache.felix.log", "1.0.1", "jar"));
bundleList.add(installBundle("org.apache.felix.configadmin", "1.8.12", "jar"));
bundleList.add(installBundle("org.apache.felix.eventadmin", "1.4.8", "jar"));
bundleList.add(installBundle("org.apache.felix.http.servlet-api", "1.1.2", "jar"));
bundleList.add(installBundle("org.apache.felix.http.api", "3.0.0", "jar"));
//Felix Jetty bundle
bundleList.add(installBundle("org.apache.felix.http.jetty", "3.4.0", "jar"));
//Felix Webconsole bundle
bundleList.add(installBundle("org.apache.felix.webconsole", "4.2.16-all", "jar"));
//Core bundles
bundleList.add(installBundle("libra-commons-osgi-core-service", "1.4.0", "jar"));
bundleList.add(installBundle("libra-commons-osgi-core-wsbundle", "1.4.0", "war"));
}
private static void startBundles()
throws BundleException {
for (Bundle bundle : bundleList) {
if (bundle.getHeaders().get(FRAGMENT_HOST) != null)
continue;
bundle.start();
}
}
}
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">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>hu.libra.commons</groupId>
<artifactId>libra-commons-osgi-core</artifactId>
<version>1.4.0</version>
<name>Libra Common OSGi Core</name>
<packaging>bundle</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<instructions>
<Bundle-SymbolicName>hu.libra.commnos.osgi.core</Bundle-SymbolicName>
</instructions>
</configuration>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
</supportedProjectTypes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<dependencies>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.6.1</version>
</dependency>
</dependencies>
</project>
hu.libra.commnos.osgi.core.service 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">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>hu.libra.commons</groupId>
<artifactId>libra-commons-osgi-core-service</artifactId>
<version>1.4.0</version>
<name>Libra Common OSGi Core Service Bundle</name>
<packaging>bundle</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-SymbolicName>hu.libra.commons.osgi.core.service</Bundle-SymbolicName>
<Bundle-Activator>hu.libra.commons.osgi.core.service.Activator</Bundle-Activator>
<Export-Package>hu.libra.commons.osgi.core.service</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
hu.libra.commnos.osgi.core.wsbundle 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">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>hu.libra.commons</groupId>
<artifactId>libra-commons-osgi-core-wsbundle</artifactId>
<version>1.4.0</version>
<name>Libra Common OSGi Core Webservice Bundle</name>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-SymbolicName>hu.libra.commnos.osgi.core.wsbundle</Bundle-SymbolicName>
<Bundle-Activator>hu.libra.commons.osgi.core.wsbundle.Activator</Bundle-Activator>
<Private-Package>hu.libra.commons.osgi.core.wsbundle</Private-Package>
<Import-Package>
org.osgi.framework,
javax.servlet,
javax.servlet.http,
javax.servlet.*,
javax.servlet.jsp.*,
javax.servlet.jsp.jstl.*,
hu.libra.commnos.osgi.core.service
</Import-Package>
<DynamicImport-Package>
javax.*,
org.xml.sax,
org.xml.sax.*,
org.w3c.*
</DynamicImport-Package>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Web-ContextPath>/libraosgicore</Web-ContextPath>
<Webapp-Context>/libraosgicore</Webapp-Context>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Libra OSGi Core Service -->
<dependency>
<groupId>hu.libra.commons</groupId>
<artifactId>libra-commons-osgi-core-service</artifactId>
<version>1.4.0</version>
<scope>provided</scope>
</dependency>
<!-- WS -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.10</version>
</dependency>
</dependencies>
</project>
Anyone know how to filter text outside of quotes in a java file via the resource plugin?
I have a resource filter set with a delimiter of # but I only see the version.designator replaced. This makes me think there's a trick to filtering text outside of quotes in a java file.
Thanks for the help
Peter
Pom
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>update Version file from pom</id>
<phase>generate-resources</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<overwrite>true</overwrite>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<outputDirectory>${project.build.sourceDirectory}/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Source
public final class Version {
public static final int MAJOR_VERSION = #version.major#;
public static final String DESIGNATOR_VERSION = "#version.designator#";
Result
public final class Version {
public static final int MAJOR_VERSION = #version.major#;
public static final String DESIGNATOR_VERSION = "BETA";
Trying to create my first ever executable jar and am using Maven to do so.
The Java runs fine by itself but when I try and run the jar I get a FileNotFoundException for src\main\resources\sound.wav.
I think the problem is obviously in the pom.xml file and in the resources declaration, but my lack of experience with maven and jars and lack of idea means that I can't seem to fix it no matter how much fiddling I do.
My pom.xml file looks like this:
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupID</groupId>
<artifactId>Main</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<finalName>MailCheck</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>sound.wav</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
and my Java file looks like this:
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import javax.mail.*;
import javax.mail.search.FlagTerm;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Main {
public static void main(String[] args)
{
String password = "mypassword";
String emailAddress = "myemail";
String soundFile = "src\\main\\resources\\sound.wav";
AudioStream as = null;
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try
{
InputStream in = new FileInputStream(soundFile);
as = new AudioStream(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
Session session = Session.getInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", emailAddress, password);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message[] messages = inbox.search(ft);
int unreadMail = 0;
for (Message message : messages)
{
unreadMail++;
if (unreadMail > 0)
{
AudioPlayer.player.start(as);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
After package, this file should exist in the generated JAR. Please check this. And besides, you can not get InputStream of a file in a JAR like normal file in file system. You should try to get the InputStream from resource.
InputStream in = Main.class.getClassLoader()
.getResourceAsStream("src/main/resources/sound.wav");