Reading txt file from jar fails but reading image works - java

I have an issue which has been bothering me for days... I checked similar questions but couldn't find a solution.
I use NetBeans IDE. I build the project jar file i.e "Clock.jar" which contains a "clock" named folder in which some images, a text file and all project classes are found.
The following code for creating an image icon works
return new ImageIcon(getClass().getResource("/clock/button_close.png"));
But the following code for reading the text file fails
InputStream name = getClass().getResourceAsStream("/clock/input.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(name));
As you may have guessed the NullPointer Exception is thrown meaning probably it couln't locate the file.
But how come the image icon is constructed successfully (by passing it the URL returned from getResource) but the txt file cannot be found (by passing it an input stream from getResourceAsStream).
Thanks in advance, for any answer ( I mean it :) )
jar -tvf Clock.jar
0 Wed May 15 14:44:36 EEST 2013 META-INF/
202 Wed May 15 14:44:34 EEST 2013 META-INF/MANIFEST.MF
0 Wed May 15 14:44:36 EEST 2013 clock/
649 Wed May 15 14:44:36 EEST 2013 clock/Clock$1$1.class
789 Wed May 15 14:44:36 EEST 2013 clock/Clock$1.class
2026 Wed May 15 14:44:36 EEST 2013 clock/Clock.class
709 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$1.class
830 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$2.class
750 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$3.class
713 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$4.class
741 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$5.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$6.class
1081 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$7.class
981 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$8.class
9640 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog.class
702 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$1.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$2.class
734 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$3.class
743 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$4.class
531 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$5.class
1046 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$6.class
9464 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame.class
782 Wed May 15 14:44:36 EEST 2013 clock/ErrorReporter.class
1826 Wed May 15 14:44:36 EEST 2013 clock/IconButton.class
2693 Wed May 15 14:44:36 EEST 2013 clock/MessagePool.class
2824 Wed May 15 14:44:36 EEST 2013 clock/SystemInfo.class
2212 Wed May 15 14:44:36 EEST 2013 clock/button_close.png
6540 Wed May 15 14:44:36 EEST 2013 clock/button_close_highlighted.png
5668 Wed May 15 14:44:36 EEST 2013 clock/input.txt

Looking at the source again closely, try..
URL url = getClass().getResource("/clock/input.txt");
InputStream name = url.openStream();
I recall that the Class::getResourceAsStream variant deals with paths slightly differently than simply Class::getResource - I am sure it should work for the latter one.

Related

kafka connect to Google BigQuery throws error java.lang.NoClassDefFoundError: org/apache/kafka/common/config/ConfigDef$CaseInsensitiveValidString

I am trying to stream from a Kafka topic to Google BigQuery. My connect-standalone.properties file is as follows:
bootstrap.servers=rhes75:9092,rhes75:9093,rhes75:9094,rhes564:9092,rhes564:9093,rhes564:9094,rhes76:9092,rhes76:9093,rhes76:9094
key.converter=org.apache.kafka.connect.storage.StringConverter
##value.converter=org.apache.kafka.connect.storage.StringConverter
value.converter=org.apache.kafka.connect.json.JsonConverter
key.converter.schemas.enable=false
value.converter.schemas.enable=false
##internal.key.converter=org.apache.kafka.connect.json.JsonConverter
internal.value.converter=org.apache.kafka.connect.json.JsonConverter
internal.key.converter=org.apache.kafka.connect.storage.StringConverter
##internal.value.converter=org.apache.kafka.connect.storage.StringConverter
internal.key.converter.schemas.enable=false
internal.value.converter.schemas.enable=false
offset.storage.file.filename=/tmp/connect_bq.offsets
offset.flush.interval.ms=10000
#plugin.path=/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins
plugin.path=/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins/wepay-kafka-connect-bigquery-2.1.0
and my bigquery-sink.properties is as below
name=bigquery-sink
connector.type=bigquery-connector
connector.class=com.wepay.kafka.connect.bigquery.BigQuerySinkConnector
defaultDataset=test
project=axial-glow-224522
topics=md
autoCreateTables=false
gcsBucketName=tmp_storage_bucket
queueSize=-1
bigQueryRetry=0
bigQueryRetryWait=1000
bigQueryMessageTimePartitioning=false
bigQueryPartitionDecorator=true
timePartitioningType=DAY
keySource=FILE
keyfile=/home/hduser/xyz.json
sanitizeTopics=false
schemaRetriever=com.wepay.kafka.connect.bigquery.retrieve.IdentitySchemaRetriever
threadPoolSize=10
allBQFieldsNullable=false
avroDataCacheSize=100
batchLoadIntervalSec=120
convertDoubleSpecialValues=false
enableBatchLoad=false
upsertEnabled=false
deleteEnabled=false
mergeIntervalMs=60_000L
mergeRecordsThreshold=-1
autoCreateBucket=true
allowNewBigQueryFields=false
allowBigQueryRequiredFieldRelaxation=false
allowSchemaUnionization=false
kafkaDataFieldName=null
kafkaKeyFieldName=null
I run the code as below
#!/bin/ksh
unset CLASSPATH
export CLASSPATH=/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins/wepay-kafka-connect-bigquery-2.1.0/lib:$KAFKA_HOME/libs
export KAFKA_DEBUG=y
$KAFKA_HOME/bin/connect-standalone.sh \
/d4T/hduser/bigquery-kafka-connect-sink/etc/connect-standalone.properties \
/d4T/hduser/bigquery-kafka-connect-sink/etc/bigquery-sink.properties
It comes back with this error
[2021-03-13 18:17:38,499] INFO Started o.e.j.s.ServletContextHandler#2ecf5915{/,null,AVAILABLE} (org.eclipse.jetty.server.handler.ContextHandler:744)
[2021-03-13 18:17:38,503] INFO Started http_8083#6badba10{HTTP/1.1}{0.0.0.0:8083} (org.eclipse.jetty.server.ServerConnector:266)
[2021-03-13 18:17:38,503] INFO Started #1981ms (org.eclipse.jetty.server.Server:379)
[2021-03-13 18:17:38,503] INFO Advertised URI: http://50.140.197.220:8083/ (org.apache.kafka.connect.runtime.rest.RestServer:248)
[2021-03-13 18:17:38,503] INFO REST server listening at http://50.140.197.220:8083/, advertising URL http://50.140.197.220:8083/ (org.apache.kafka.connect.runtime.rest.RestServer:207)
[2021-03-13 18:17:38,503] INFO Kafka Connect started (org.apache.kafka.connect.runtime.Connect:55)
[2021-03-13 18:17:38,518] ERROR Stopping after connector error (org.apache.kafka.connect.cli.ConnectStandalone:113)
java.lang.NoClassDefFoundError: org/apache/kafka/common/config/ConfigDef$CaseInsensitiveValidString
at com.wepay.kafka.connect.bigquery.config.BigQuerySinkConfig.getConfig(BigQuerySinkConfig.java:505)
at com.wepay.kafka.connect.bigquery.BigQuerySinkConnector.config(BigQuerySinkConnector.java:79)
at org.apache.kafka.connect.connector.Connector.validate(Connector.java:132)
at org.apache.kafka.connect.runtime.AbstractHerder.validateConnectorConfig(AbstractHerder.java:277)
at org.apache.kafka.connect.runtime.standalone.StandaloneHerder.putConnectorConfig(StandaloneHerder.java:164)
at org.apache.kafka.connect.cli.ConnectStandalone.main(ConnectStandalone.java:107)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.config.ConfigDef$CaseInsensitiveValidString
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.apache.kafka.connect.runtime.isolation.PluginClassLoader.loadClass(PluginClassLoader.java:104)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
It complains about not finding this class
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.config.ConfigDef$CaseInsensitiveValidString
However, under directory
/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins/wepay-kafka-connect-bigquery-2.1.0/lib
it is in kafka-clients-1.1.0.jar
jar tvf kafka-clients-1.1.0.jar|grep org.apache.kafka.common.config.ConfigDef$CaseInsensitiveValidString
2051 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$1.class
1175 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$2.class
2876 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$3.class
1132 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$4.class
1801 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$CompositeValidator.class
3274 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$ConfigKey.class
1255 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Importance.class
1104 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$NonEmptyString.class
2144 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$NonEmptyStringWithoutControlChars.class
922 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$NonNullValidator.class
2072 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Range.class
583 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Recommender.class
1513 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Type.class
293 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Validator.class
1943 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$ValidList.class
2196 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$ValidString.class
1269 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef$Width.class
34225 Fri Mar 23 22:54:26 GMT 2018 org/apache/kafka/common/config/ConfigDef.class
So I have no idea what is missing in here?
The jars loaded are shown below
jvm.classpath = /d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins/wepay-kafka-connect-bigquery-2.1.0/lib:/data6/hduser/kafka_2.12-1.1.0/libs:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/aopalliance-repackaged-2.5.0-b32.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/argparse4j-0.7.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/commons-lang3-3.5.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/connect-api-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/connect-file-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/connect-json-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/connect-runtime-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/connect-transforms-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/guava-20.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/hk2-api-2.5.0-b32.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/hk2-locator-2.5.0-b32.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/hk2-utils-2.5.0-b32.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-annotations-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-core-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-databind-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-jaxrs-base-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-jaxrs-json-provider-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jackson-module-jaxb-annotations-2.9.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javassist-3.20.0-GA.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javassist-3.21.0-GA.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javax.annotation-api-1.2.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javax.inject-1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javax.inject-2.5.0-b32.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javax.servlet-api-3.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/javax.ws.rs-api-2.0.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-client-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-common-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-container-servlet-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-container-servlet-core-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-guava-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-media-jaxb-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jersey-server-2.25.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-client-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-continuation-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-http-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-io-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-security-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-server-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-servlet-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-servlets-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jetty-util-9.2.24.v20180105.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/jopt-simple-5.0.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-clients-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-log4j-appender-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-streams-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-streams-examples-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-streams-test-utils-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka-tools-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka_2.12-1.1.0-sources.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka_2.12-1.1.0-test-sources.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/kafka_2.12-1.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/log4j-1.2.17.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/lz4-java-1.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/maven-artifact-3.5.2.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/metrics-core-2.2.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/osgi-resource-locator-1.0.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/plexus-utils-3.1.0.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/reflections-0.9.11.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/rocksdbjni-5.7.3.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/scala-library-2.12.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/scala-logging_2.12-3.7.2.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/scala-reflect-2.12.4.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/slf4j-api-1.7.25.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/slf4j-log4j12-1.7.25.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/snappy-java-1.1.7.1.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/validation-api-1.1.0.Final.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/zkclient-0.10.jar:/data6/hduser/kafka_2.12-1.1.0/bin/../libs/zookeeper-3.4.10.jar
os.spec = Linux, amd64, 3.10.0-1062.9.1.el7.x86_64
os.vcpus = 6
Thanks all.
I was using an older Kafka version.
I upgraded Kafka in the cluster from kafka_2.12-1.1.0 to the latest stable version kafka_2.12-2.7.0. I also upgraded zookeeper from zookeeper-3.4.6 to apache-zookeeper-3.6.2-bin version.
In addition in the run file I added the following:
#!/bin/ksh
unset CLASSPATH
export CLASSPATH=/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins:$KAFKA_HOME/libs
$KAFKA_HOME/bin/connect-standalone.sh \
/d4T/hduser/bigquery-kafka-connect-sink/etc/connect-standalone.properties \
/d4T/hduser/bigquery-kafka-connect-sink/etc/bigquery-sink.properties
where the first part of CLASSPATH comes from plugins
plugin.path=/d4T/hduser/bigquery-kafka-connect-sink/share/kafka/plugins
This solved the issue

The finer points of runnable jar creation (Jersey & Genson)

I can create a runnable jar every time with Eclipse's Runnable Jar wizard. If I check the box for Eclipse to "Save as ANT script", then incorporate that code into build.xml and then have our GitLab CI process do a build & create a runnable jar, it runs, but it can't seem to find a class in that jar even though the .class file is in there.
I'm not sure where to start. The resulting jar files from the Eclipse build vs the standalone java/ant build are similar, but not exactly the same. A few things are out of order, but that's all I can put my finger on at the moment.
How can I start to hone in on this? My program is a Jersey web client. I'm using Genson and the error message I'm getting with the standalone jar build is
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json
But I can list the jar files and both have these entries
47 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/javax.ws.rs.ext.MessageBodyWriter
1220 Thu Aug 07 14:08:08 CDT 2014 javax/ws/rs/ext/MessageBodyWriter.class
Right now I can get around it by doing a build in Eclipse and manually deploying the file, but of course this isn't acceptable.
EDIT: Thanks to a helpful friend, I was pointed to the contents of META-INF/services. I don't know what it means, but here are the contents of that from both jars.
GitLab jar (get MessageBodyWriter error message)
0 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/
47 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/javax.json.spi.JsonProvider
47 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/javax.ws.rs.ext.MessageBodyReader
47 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/javax.ws.rs.ext.MessageBodyWriter
47 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/javax.ws.rs.ext.Providers
50 Sun Mar 27 22:23:48 CDT 2016 META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
61 Fri May 27 19:08:18 CDT 2016 META-INF/services/org.glassfish.hk2.extension.ServiceLocatorGenerator
60 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
67 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/javax.servlet.ServletContainerInitializer
67 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider
63 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.servlet.spi.FilterUrlMappingsProvider
56 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
56 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/javax.ws.rs.ext.RuntimeDelegate
73 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
137 Thu Oct 27 14:32:12 CDT 2016 META-INF/services/org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
855 Sun Oct 02 11:14:44 CDT 2016 META-INF/services/javax.annotation.processing.Processor
Eclipse jar (works)
0 Tue Dec 13 08:55:42 CST 2016 META-INF/services/
47 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.json.spi.JsonProvider
47 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.ws.rs.ext.MessageBodyReader
47 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.ws.rs.ext.MessageBodyWriter
47 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.ws.rs.ext.Providers
50 Tue Dec 13 08:55:42 CST 2016 META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
61 Tue Dec 13 08:55:42 CST 2016 META-INF/services/org.glassfish.hk2.extension.ServiceLocatorGenerator
67 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.servlet.ServletContainerInitializer
67 Tue Dec 13 08:55:42 CST 2016 META-INF/services/org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider
63 Tue Dec 13 08:55:42 CST 2016 META-INF/services/org.glassfish.jersey.servlet.spi.FilterUrlMappingsProvider
56 Tue Dec 13 08:55:42 CST 2016 META-INF/services/org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
56 Tue Dec 13 08:55:42 CST 2016 META-INF/services/javax.ws.rs.ext.RuntimeDelegate
855 Tue Dec 13 08:55:44 CST 2016 META-INF/services/javax.annotation.processing.Processor
EDIT3: The contents of each duplicate
org.glassfish.jersey.internal.spi.AutoDiscoverable (3)
com.owlike.genson.ext.jaxrs.JerseyAutoDiscoverable
org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
org.glassfish.jersey.server.filter.internal.ServerFiltersAutoDiscoverable
org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable (2)
org.glassfish.jersey.jaxb.internal.JaxbAutoDiscoverable
org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
Versus the "good" jar file, built by Eclipse, which has no duplicates:
in org.glassfish.jersey.internal.spi.AutoDiscoverable
com.owlike.genson.ext.jaxrs.JerseyAutoDiscoverable
I guess I'll plow through other META-INF things, so see what can be seen.
in org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
org.glassfish.jersey.jaxb.internal.JaxbAutoDiscoverable

Storing Java Calendar in ArrayList leads to unexpected behaviour [duplicate]

This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 7 years ago.
What I have: A today's Date-Object.
What I need: Storing the dates of the containing week in an ArrayList.
What have I tried:
public static void main(String[] args) {
Date today = new Date();
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(today);
List<Calendar> daysOfWeek = new ArrayList<>();
IntStream
.iterate(2, n -> n+1)
.limit(7)
.forEach(n ->
{
if(n != 8){
cal.set(Calendar.DAY_OF_WEEK, n);
}else{
cal.set(Calendar.DAY_OF_WEEK, 1);
}
System.out.println("CAL_INSIDE: " + cal.getTime());
daysOfWeek.add(cal);
});
System.out.println("");
daysOfWeek.stream().forEach( cal -> System.out.println("CAL_OUTSIDE: " + cal.getTime()));
}
Within the forEach-statement I get exactly what I want. But the ArrayList seems to be containing only the last Date.
Output:
CAL_INSIDE: Mon Aug 03 14:35:37 CEST 2015
CAL_INSIDE: Tue Aug 04 14:35:37 CEST 2015
CAL_INSIDE: Wed Aug 05 14:35:37 CEST 2015
CAL_INSIDE: Thu Aug 06 14:35:37 CEST 2015
CAL_INSIDE: Fri Aug 07 14:35:37 CEST 2015
CAL_INSIDE: Sat Aug 08 14:35:37 CEST 2015
CAL_INSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
Basically I get what I want but...
Question: Why is only sunday stored within the ArrayList ? What am I missing ?
EDIT
Output according to accepted answer:
CAL_INSIDE: Mon Aug 03 14:51:31 CEST 2015
CAL_INSIDE: Tue Aug 04 14:51:31 CEST 2015
CAL_INSIDE: Wed Aug 05 14:51:31 CEST 2015
CAL_INSIDE: Thu Aug 06 14:51:31 CEST 2015
CAL_INSIDE: Fri Aug 07 14:51:31 CEST 2015
CAL_INSIDE: Sat Aug 08 14:51:31 CEST 2015
CAL_INSIDE: Sun Aug 09 14:51:31 CEST 2015
CAL_OUTSIDE: Mon Aug 03 14:51:31 CEST 2015
CAL_OUTSIDE: Tue Aug 04 14:51:31 CEST 2015
CAL_OUTSIDE: Wed Aug 05 14:51:31 CEST 2015
CAL_OUTSIDE: Thu Aug 06 14:51:31 CEST 2015
CAL_OUTSIDE: Fri Aug 07 14:51:31 CEST 2015
CAL_OUTSIDE: Sat Aug 08 14:51:31 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:51:31 CEST 2015
Calendar is a mutable object so you just store references to that object and CAL_OUTSIDE only gives you 7 times the last inner state of the one referenced Calendar object.
You will have to move
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(today);
inside your loop to get 7 different Calendar objects.

maven test-jar: src/test/resources/file present but could not found by test

I have module structure like
external_services/pom.xml
ifs/pom.xml
src/main/java
src/main/java/resources
src/test/java
src/test/java/resources/sample.json
inventory/pom.xml
ifs/pom.xml
src/main/java
src/main/java/resources
src/test/java/MyTest.java
In this MyTest.java I need to get sample.json since I want that to be shared across, so in business/pom.xml I add the following
<dependency>
<groupId>com.org.my_services.external_services</groupId>
<artifactId>ifs</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
so now when I run the test, I see
file:/Users/harith/.m2/repository/com/org/my_services/external_services/ifs/1.0-SNAPSHOT/ifs-1.0-SNAPSHOT-tests.jar!/sample.json (No such file or directory)
However, when I look at jar ifs-1.0-SNAPSHOT-tests.jar, I see it there
jar -tvf /Users/harith/.m2/repository/com/org/pryme_services/federated_services/ifs/1.0-SNAPSHOT/ifs-1.0-SNAPSHOT-tests.jar
0 Wed May 07 09:47:36 PDT 2014 META-INF/
131 Wed May 07 09:47:34 PDT 2014 META-INF/MANIFEST.MF
0 Wed May 07 09:47:34 PDT 2014 com/
0 Wed May 07 09:47:34 PDT 2014 com/org/
0 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/
0 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/
0 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/
0 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/configuration/
892 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/configuration/DevelopmentContextConfiguration.class
888 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/configuration/ProductionContextConfiguration.class
2381 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/IFSClientTest.class
1422 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/InventoryRESTClientTest.class
1033 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/MockIfsClient$1.class
2350 Wed May 07 09:47:34 PDT 2014 com/org/pryme_services/external_services/ifs/MockIfsClient.class
142315 Wed May 07 09:47:34 PDT 2014 sample.json // <-- here
0 Wed May 07 09:47:36 PDT 2014 META-INF/maven/
0 Wed May 07 09:47:36 PDT 2014 META-INF/maven/com.org.pryme_services.federated_services/
0 Wed May 07 09:47:36 PDT 2014 META-INF/maven/com.org.pryme_services.federated_services/ifs/
2391 Wed May 07 09:45:36 PDT 2014 META-INF/maven/com.org.pryme_services.federated_services/ifs/pom.xml
138 Wed May 07 09:47:34 PDT 2014 META-INF/maven/com.org.pryme_services.federated_services/ifs/pom.properties
The way it is used in class is
inventoryPriceDetails = mapper.readValue(new File(getClass().getResource("/getInventoryAndPrice.json").getPath()), new TypeReference<List<InventoryPriceDetail>>() {
});
I am not sure what I am missing, can some one please help?
there are a couple of things:
1)
external_services/pom.xml
ifs/pom.xml
src/main/java
src/main/java/resources
src/test/java
src/test/java/resources/sample.json
but maven expects
external_services/pom.xml
ifs/pom.xml
src/main/java
src/main/resources
src/test/java
src/test/resources/sample.json
"resource" parallel to "java". In your case "resource" is a package.
2) The file is called "sample.json" but you look for "getInventoryAndPrice.json"
3) new File does not work on a Path. You need getResourceAsStream() to extract data from a jar.
(If you want to work with paths you need to work with the nio classes, not File)

JAVA Date Calculation Error .?

Here is my code fragment. I need to subtract days from a certain date but I'm not getting the results I expect:
public class TestingDates {
public static void main(String[] args) {
Date toDate=new Date();
Date fromDate=new Date();
for (int i = 0; i < 30; i++) {
fromDate.setTime(toDate.getTime() - i * 24 * 60 * 60 * 1000);
System.out.println(fromDate);
}
}
}
And i'm confuse with this result
here is the output
Fri Sep 13 12:24:50 IST 2013
Thu Sep 12 12:24:50 IST 2013
Wed Sep 11 12:24:50 IST 2013
Tue Sep 10 12:24:50 IST 2013
Mon Sep 09 12:24:50 IST 2013
Sun Sep 08 12:24:50 IST 2013
Sat Sep 07 12:24:50 IST 2013
Fri Sep 06 12:24:50 IST 2013
Thu Sep 05 12:24:50 IST 2013
Wed Sep 04 12:24:50 IST 2013
Tue Sep 03 12:24:50 IST 2013
Mon Sep 02 12:24:50 IST 2013
Sun Sep 01 12:24:50 IST 2013
Sat Aug 31 12:24:50 IST 2013
Fri Aug 30 12:24:50 IST 2013
Thu Aug 29 12:24:50 IST 2013
Wed Aug 28 12:24:50 IST 2013
Tue Aug 27 12:24:50 IST 2013
Mon Aug 26 12:24:50 IST 2013
Sun Aug 25 12:24:50 IST 2013
Sat Aug 24 12:24:50 IST 2013
Fri Aug 23 12:24:50 IST 2013
Thu Aug 22 12:24:50 IST 2013
Wed Aug 21 12:24:50 IST 2013
Tue Aug 20 12:24:50 IST 2013
Tue Oct 08 05:27:38 IST 2013
Mon Oct 07 05:27:38 IST 2013
Sun Oct 06 05:27:38 IST 2013
Sat Oct 05 05:27:38 IST 2013
Fri Oct 04 05:27:38 IST 2013
please help me to sort out this issue
You seem to be wanting to calculate backwards 30 days...You should try using Calendar instead
Calendar cal = Calendar.getInstance();
cal.setTime(toDate);
for (int i = 0; i < 30; i++) {
cal.add(Calendar.DATE, -1);
System.out.println(cal.getTime());
}
You face the integer overflow. The maximal value for your int variable i is 2^31-1.
Use long type for the i variable to avoid the overflow.
for (long i = 0; i < 30; i++) {
fromDate.setTime(toDate.getTime() - i * 24 * 60 * 60 * 1000);
System.out.println(fromDate);
}
You get an overflow in your calculation
public static void main(String[] args) {
Date toDate=new Date();
Date fromDate=new Date();
for (int i = 0; i < 30; i++) {
int x = i * 24 * 60 * 60 * 1000;
System.out.println(x);
fromDate.setTime(toDate.getTime() - x);
System.out.println(fromDate);
}
}
results in
0
Fri Sep 13 09:32:00 CEST 2013
86400000
Thu Sep 12 09:32:00 CEST 2013
172800000
Wed Sep 11 09:32:00 CEST 2013
259200000
Tue Sep 10 09:32:00 CEST 2013
345600000
Mon Sep 09 09:32:00 CEST 2013
432000000
Sun Sep 08 09:32:00 CEST 2013
518400000
Sat Sep 07 09:32:00 CEST 2013
604800000
Fri Sep 06 09:32:00 CEST 2013
691200000
Thu Sep 05 09:32:00 CEST 2013
777600000
Wed Sep 04 09:32:00 CEST 2013
864000000
Tue Sep 03 09:32:00 CEST 2013
950400000
Mon Sep 02 09:32:00 CEST 2013
1036800000
Sun Sep 01 09:32:00 CEST 2013
1123200000
Sat Aug 31 09:32:00 CEST 2013
1209600000
Fri Aug 30 09:32:00 CEST 2013
1296000000
Thu Aug 29 09:32:00 CEST 2013
1382400000
Wed Aug 28 09:32:00 CEST 2013
1468800000
Tue Aug 27 09:32:00 CEST 2013
1555200000
Mon Aug 26 09:32:00 CEST 2013
1641600000
Sun Aug 25 09:32:00 CEST 2013
1728000000
Sat Aug 24 09:32:00 CEST 2013
1814400000
Fri Aug 23 09:32:00 CEST 2013
1900800000
Thu Aug 22 09:32:00 CEST 2013
1987200000
Wed Aug 21 09:32:00 CEST 2013
2073600000
Tue Aug 20 09:32:00 CEST 2013
-2134967296
Tue Oct 08 02:34:48 CEST 2013
-2048567296
Mon Oct 07 02:34:48 CEST 2013
-1962167296
Sun Oct 06 02:34:48 CEST 2013
-1875767296
Sat Oct 05 02:34:48 CEST 2013
-1789367296
Fri Oct 04 02:34:48 CEST 2013
see the x value before your date jumps.
Try to print the actual int you get after this line:
i * 24 * 60 * 60 * 1000
Here is the output:
86400000
172800000
259200000
345600000
432000000
518400000
604800000
691200000
777600000
864000000
950400000
1036800000
1123200000
1209600000
1296000000
1382400000
1468800000
1555200000
1641600000
1728000000
1814400000
1900800000
1987200000
2073600000
-2134967296
-2048567296
-1962167296
-1875767296
-1789367296
As you can see you create a number higher than the one an int can store and thus get overflow. The solution is using a long, that is a 64 bit number.

Categories

Resources