I've got a problem which I understand but I have no idea how to fix it.
I have a class in my project that contains static fields with images or sounds so that I can use them everywhere without recreating variables every time.
One of the fields for reference (they are all the same):
public static ImageIcon mainMenuBg = new ImageIcon(Resources.class.getResource("img/menubg.png"));
Now my problem is that it works fine in Eclipse when I run it, but when I export the project to a runnable JAR, nothing happens. I then went in the command window and ran it from there, and I have this ExceptionInInitializerError due to my images. Basically, none of the images are recognized at runtime, so if I remove every image call, it works.
Also, it is worth noting that when exporting, the list of Main classes doesn't really update. If I change the project's main's name, it isn't added to the list. Might it be a problem with my main class specifically? If so, how can I fix that? I tried to recreate it already but nothing changed.
Could someone enlighten me about this problem, please?
Here is the printstacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at Client.<init>(Client.java:53)
at Client$1.run(Client.java:37)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Ressources.<clinit>(Ressources.java:41)
... 16 more
Where the lines pointed are lines in which I'm trying to load an image from my resources class.
Also, all of my classes are in the same package, the default one.
Finally, here is the 'jar tvf ...' command's result:
153 Tue Jan 13 21:43:36 CET 2015 META-INF/MANIFEST.MF
0 Tue Jan 13 21:43:36 CET 2015 org/
0 Tue Jan 13 21:43:36 CET 2015 org/eclipse/
0 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/
0 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/
0 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/
978 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/JIJC
onstants.class
714 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/JarR
srcLoader$ManifestInfo.class
4735 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/JarR
srcLoader.class
1505 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/Rsrc
URLConnection.class
1841 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/Rsrc
URLStreamHandler.class
1149 Tue Jan 13 21:43:36 CET 2015 org/eclipse/jdt/internal/jarinjarloader/Rsrc
URLStreamHandlerFactory.class
1495 Tue Jan 13 21:43:06 CET 2015 ChatPrive$1.class
1792 Tue Jan 13 21:43:06 CET 2015 ChatPrive$2.class
1596 Tue Jan 13 21:43:06 CET 2015 ChatPrive$3.class
1897 Tue Jan 13 21:43:06 CET 2015 ChatPrive$4.class
1553 Tue Jan 13 21:43:06 CET 2015 ChatPrive$5.class
7565 Tue Jan 13 21:43:06 CET 2015 ChatPrive.class
2680 Tue Jan 13 21:43:06 CET 2015 Tour.class
9003 Tue Jan 13 21:43:06 CET 2015 Move.class
2622 Tue Jan 13 21:43:06 CET 2015 InfosPartie.class
1448 Tue Jan 13 21:43:06 CET 2015 Message.class
3262 Tue Jan 13 21:43:06 CET 2015 Roi.class
1464 Tue Jan 13 21:43:06 CET 2015 Case.class
987 Tue Jan 13 21:43:06 CET 2015 JoueurIA.class
750 Tue Jan 13 21:43:06 CET 2015 MenuCopie$1.class
1307 Tue Jan 13 21:43:06 CET 2015 MenuCopie$2.class
2992 Tue Jan 13 21:43:06 CET 2015 MenuCopie.class
7882 Tue Jan 13 21:43:06 CET 2015 ReceptionClient.class
1138 Tue Jan 13 21:43:06 CET 2015 Chat$1.class
851 Tue Jan 13 21:43:06 CET 2015 Chat$2.class
3835 Tue Jan 13 21:43:06 CET 2015 Chat.class
2263 Tue Jan 13 21:43:06 CET 2015 Fou.class
832 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$1.class
2081 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$2.class
1401 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$3.class
1216 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$4.class
1216 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$5.class
1216 Tue Jan 13 21:43:06 CET 2015 MenuPromotion$6.class
4231 Tue Jan 13 21:43:06 CET 2015 MenuPromotion.class
1501 Tue Jan 13 21:43:06 CET 2015 Jeu$1.class
825 Tue Jan 13 21:43:06 CET 2015 Jeu$2.class
10677 Tue Jan 13 21:43:06 CET 2015 Jeu.class
1457 Tue Jan 13 21:43:06 CET 2015 ChatAll$1.class
1570 Tue Jan 13 21:43:06 CET 2015 ChatAll$2.class
1344 Tue Jan 13 21:43:06 CET 2015 ChatAll$3.class
1914 Tue Jan 13 21:43:06 CET 2015 ChatAll$4.class
1527 Tue Jan 13 21:43:06 CET 2015 ChatAll$5.class
9326 Tue Jan 13 21:43:06 CET 2015 ChatAll.class
1135 Tue Jan 13 21:43:06 CET 2015 PanelInfo$1.class
1337 Tue Jan 13 21:43:06 CET 2015 PanelInfo$10.class
1164 Tue Jan 13 21:43:06 CET 2015 PanelInfo$11.class
963 Tue Jan 13 21:43:06 CET 2015 PanelInfo$12.class
1335 Tue Jan 13 21:43:06 CET 2015 PanelInfo$13.class
963 Tue Jan 13 21:43:06 CET 2015 PanelInfo$14.class
830 Tue Jan 13 21:43:06 CET 2015 PanelInfo$15.class
829 Tue Jan 13 21:43:06 CET 2015 PanelInfo$16.class
1435 Tue Jan 13 21:43:06 CET 2015 PanelInfo$2.class
576 Tue Jan 13 21:43:06 CET 2015 PanelInfo$3.class
1748 Tue Jan 13 21:43:06 CET 2015 PanelInfo$4.class
1356 Tue Jan 13 21:43:06 CET 2015 PanelInfo$5.class
1552 Tue Jan 13 21:43:06 CET 2015 PanelInfo$6.class
1556 Tue Jan 13 21:43:06 CET 2015 PanelInfo$7.class
945 Tue Jan 13 21:43:06 CET 2015 PanelInfo$8.class
1437 Tue Jan 13 21:43:06 CET 2015 PanelInfo$9.class
16765 Tue Jan 13 21:43:06 CET 2015 PanelInfo.class
1176 Tue Jan 13 21:43:06 CET 2015 MenuUtilisateurs$1.class
1173 Tue Jan 13 21:43:06 CET 2015 MenuUtilisateurs$2.class
1753 Tue Jan 13 21:43:06 CET 2015 MenuUtilisateurs$3.class
2228 Tue Jan 13 21:43:06 CET 2015 MenuUtilisateurs$4.class
2071 Tue Jan 13 21:43:06 CET 2015 MenuUtilisateurs.class
3996 Tue Jan 13 21:43:06 CET 2015 Piece.class
3830 Tue Jan 13 21:43:06 CET 2015 PartieSolo.class
3300 Tue Jan 13 21:43:06 CET 2015 ConnexionClient.class
1160 Tue Jan 13 21:43:06 CET 2015 EchecGUI$1.class
8154 Tue Jan 13 21:43:06 CET 2015 EchecGUI.class
6844 Tue Jan 13 21:43:06 CET 2015 Joueur.class
677 Tue Jan 13 21:43:06 CET 2015 Client$1.class
834 Tue Jan 13 21:43:06 CET 2015 Client$2.class
2718 Tue Jan 13 21:43:06 CET 2015 Client$3.class
1041 Tue Jan 13 21:43:06 CET 2015 Client$4.class
1008 Tue Jan 13 21:43:06 CET 2015 Client$5.class
983 Tue Jan 13 21:43:06 CET 2015 Client$6.class
1096 Tue Jan 13 21:43:06 CET 2015 Client$7.class
4611 Tue Jan 13 21:43:06 CET 2015 Client.class
2783 Tue Jan 13 21:43:06 CET 2015 Pion.class
663 Tue Jan 13 21:43:06 CET 2015 JoueurReel.class
1620 Tue Jan 13 21:43:06 CET 2015 MessagePrive.class
6302 Tue Jan 13 21:43:06 CET 2015 PartieEnLigne.class
1911 Tue Jan 13 21:43:06 CET 2015 Cheval.class
6817 Tue Jan 13 21:43:06 CET 2015 Ressources.class
1235 Tue Jan 13 21:43:06 CET 2015 NotificationMessage$1.class
1348 Tue Jan 13 21:43:06 CET 2015 NotificationMessage.class
3835 Tue Jan 13 21:43:06 CET 2015 PartieAmi.class
1355 Tue Jan 13 21:43:06 CET 2015 EmissionClient.class
2999 Tue Jan 13 21:43:06 CET 2015 Reine.class
0 Tue Jan 13 20:55:48 CET 2015 img/
945 Sun Jan 11 19:40:24 CET 2015 img/attaque.png
196863 Fri Jan 09 22:59:04 CET 2015 img/board.png
595046 Sun Jan 11 22:04:40 CET 2015 img/board1.png
631303 Sat Jan 10 00:14:04 CET 2015 img/board2.png
440 Sun Jan 11 22:32:28 CET 2015 img/boutonchat.png
413 Sun Jan 11 22:32:46 CET 2015 img/boutonchatsel.png
735 Sun Jan 11 21:53:46 CET 2015 img/boutonrecommencer.png
793 Sun Jan 11 21:54:52 CET 2015 img/boutonrecommencersel.png
258 Wed Dec 24 17:28:54 CET 2014 img/caseCible.png
224 Sat Jan 10 18:23:04 CET 2015 img/casePleinePoss.png
1100 Sat Jan 10 18:14:06 CET 2015 img/caseSelectionnee.png
224 Sat Jan 10 18:14:30 CET 2015 img/caseVidePoss.png
5962 Wed Dec 24 17:28:54 CET 2014 img/chevalblanc.png
1203 Mon Jan 12 21:37:44 CET 2015 img/chevalblancmort.png
1106 Sun Jan 11 00:40:14 CET 2015 img/chevalblancpromo.png
5720 Wed Dec 24 17:28:54 CET 2014 img/chevalnoir.png
1214 Mon Jan 12 21:37:44 CET 2015 img/chevalnoirmort.png
1119 Sun Jan 11 00:40:50 CET 2015 img/chevalnoirpromo.png
8080 Mon Jan 12 20:44:20 CET 2015 img/echec.png
29956 Mon Jan 12 00:08:06 CET 2015 img/ecranpause.png
3736 Thu Jan 08 00:14:42 CET 2015 img/fondlabel.png
5783 Sun Jan 11 19:04:14 CET 2015 img/fondlabelblanc.png
6968 Sun Jan 11 19:04:18 CET 2015 img/fondlabelnoir.png
5327 Sun Jan 11 22:22:52 CET 2015 img/footer.png
4087 Wed Dec 24 17:28:54 CET 2014 img/foublanc.png
1024 Mon Jan 12 21:37:46 CET 2015 img/foublancmort.png
887 Sun Jan 11 00:43:04 CET 2015 img/foublancpromo.png
3987 Wed Dec 24 17:28:54 CET 2014 img/founoir.png
1010 Mon Jan 12 21:37:48 CET 2015 img/founoirmort.png
864 Sun Jan 11 00:43:24 CET 2015 img/founoirpromo.png
10904 Sun Jan 11 01:19:48 CET 2015 img/historique.png
260917 Tue Jan 13 20:55:48 CET 2015 img/menubg.png
127477 Tue Jan 13 19:30:36 CET 2015 img/panelinfobg.png
270 Sun Jan 11 22:13:34 CET 2015 img/pause.png
256 Sun Jan 11 22:13:48 CET 2015 img/pausesel.png
3817 Mon Jan 12 01:41:14 CET 2015 img/pionblanc.png
979 Mon Jan 12 21:37:50 CET 2015 img/pionblancmort.png
3813 Thu Jan 08 00:39:10 CET 2015 img/pionnoir.png
992 Mon Jan 12 21:37:42 CET 2015 img/pionnoirmort.png
379 Sun Jan 11 23:00:42 CET 2015 img/play.png
377 Sun Jan 11 23:00:28 CET 2015 img/playsel.png
37986 Mon Jan 12 01:17:54 CET 2015 img/promobg.png
449 Sun Jan 11 22:58:14 CET 2015 img/redo.png
439 Sun Jan 11 22:58:32 CET 2015 img/redosel.png
5292 Wed Dec 24 17:28:54 CET 2014 img/reineblanc.png
1245 Mon Jan 12 21:37:54 CET 2015 img/reineblancmort.png
1140 Sun Jan 11 00:41:18 CET 2015 img/reineblancpromo.png
5185 Wed Dec 24 17:28:54 CET 2014 img/reinenoir.png
1222 Mon Jan 12 22:24:28 CET 2015 img/reinenoirmort.png
1115 Sun Jan 11 00:41:40 CET 2015 img/reinenoirpromo.png
5809 Mon Jan 12 00:11:32 CET 2015 img/reprendreBtn.png
4946 Mon Jan 12 00:12:56 CET 2015 img/reprendreBtnsel.png
5889 Wed Dec 24 17:28:54 CET 2014 img/roiblanc.png
5717 Wed Dec 24 17:28:54 CET 2014 img/roinoir.png
2152 Sun Jan 11 00:57:20 CET 2015 img/roque.png
2144 Sun Jan 11 19:26:50 CET 2015 img/roqueblanc.png
2099 Sun Jan 11 19:26:16 CET 2015 img/roquenoir.png
529 Mon Jan 12 21:58:58 CET 2015 img/sonoff.png
503 Mon Jan 12 21:59:24 CET 2015 img/sonoffsel.png
686 Mon Jan 12 21:59:36 CET 2015 img/sonon.png
623 Mon Jan 12 21:59:52 CET 2015 img/sononsel.png
395 Sun Jan 11 22:43:30 CET 2015 img/surrender.png
357 Sun Jan 11 22:43:46 CET 2015 img/surrendersel.png
849 Mon Jan 12 19:40:22 CET 2015 img/tooltip.png
4564 Wed Dec 24 17:28:54 CET 2014 img/tourblanc.png
1146 Mon Jan 12 21:38:00 CET 2015 img/tourblancmort.png
1008 Sun Jan 11 00:42:16 CET 2015 img/tourblancpromo.png
4528 Wed Dec 24 17:28:54 CET 2014 img/tournoir.png
1135 Mon Jan 12 21:38:04 CET 2015 img/tournoirmort.png
986 Sun Jan 11 00:42:42 CET 2015 img/tournoirpromo.png
453 Sun Jan 11 22:57:06 CET 2015 img/undo.png
441 Sun Jan 11 22:56:48 CET 2015 img/undosel.png
1326 Mon Jan 12 01:13:52 CET 2015 img/valider.png
1393 Mon Jan 12 01:19:40 CET 2015 img/validersel.png
0 Tue Jan 13 18:50:36 CET 2015 snd/
2248748 Mon Jan 12 23:35:38 CET 2015 snd/ambiancefeu.wav
77868 Mon Jan 12 23:32:06 CET 2015 snd/attaque.wav
25988 Tue Jan 13 00:08:06 CET 2015 snd/boutonclic.wav
397356 Mon Jan 12 21:02:50 CET 2015 snd/echec.wav
43342 Sun Jan 11 23:59:36 CET 2015 snd/hover.wav
319532 Wed Jan 07 00:43:04 CET 2015 snd/message.wav
108464 Mon Jan 12 23:45:02 CET 2015 snd/played.wav
606552 Tue Jan 13 18:50:36 CET 2015 snd/promotion.wav
112156 Sat Jan 10 19:06:50 CET 2015 snd/select.wav
After digging in documentation around I think that is possible that the problem is that you are using a relative path instead of an absolute path as explained here:
[http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading]
So please try:
ImageIcon mainMenuBg = new ImageIcon(Resources.class.getResource("/img/menubg.png"));
Use somthing like this for your images.
ClassLoader cldr = this.getClass().getClassLoader();
URL picture = cldr.getResource("files/gcf.jpg");
ImageIcon mainMenuBg = new ImageIcon(picture);
And for the sounds.
ClassLoader cldr = this.getClass().getClassLoader();
URL musicurl = cldr.getResource("files/gcf.wav");
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(musicurl);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
catch (Exception ex){
System.out.println("loading ??");
}
(import java.net.*; for the URL class)
Put your /files folder into the /src folder and create your JAR.
Related
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
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.
I've built a jar file:
[mdwilkie#cobalt ~/codesigning]$ jar tvf beforesign.jar
0 Tue Mar 04 14:47:10 PST 2014 META-INF/
89 Tue Mar 04 14:47:10 PST 2014 META-INF/MANIFEST.MF
560 Tue Sep 08 08:35:48 PDT 2009 ControlPoint.class
1190 Tue Sep 08 08:35:48 PDT 2009 Curve.class
2367 Tue Sep 08 08:35:48 PDT 2009 CurveControls.class
2417 Tue Sep 08 08:35:48 PDT 2009 CurveControls2.class
4815 Tue Sep 08 08:35:48 PDT 2009 CurvePanel.class
1186 Tue Sep 08 08:35:48 PDT 2009 Demo.class
4108 Tue Sep 08 08:35:48 PDT 2009 DemoPanel.class
5155 Tue Sep 08 08:35:48 PDT 2009 DisplayPanel.class
1311 Tue Sep 08 08:35:48 PDT 2009 karst.class
5411 Tue Sep 08 08:35:48 PDT 2009 Matrix.class
The jar file was constructed as follows:
C:\Users\projects>dir curvefitting_demo
Volume in drive C has no label.
Volume Serial Number is 0AED-DBEF
Directory of C:\Users\projects\curvefitting_demo
08/04/2014 01:19 PM <DIR> .
08/04/2014 01:19 PM <DIR> ..
08/09/2009 08:35 AM 560 ControlPoint.class
08/09/2009 08:35 AM 1,190 Curve.class
08/09/2009 08:35 AM 2,367 CurveControls.class
08/09/2009 08:35 AM 2,417 CurveControls2.class
08/09/2009 08:35 AM 4,815 CurvePanel.class
08/09/2009 08:35 AM 1,186 Demo.class
08/09/2009 08:35 AM 4,108 DemoPanel.class
08/09/2009 08:35 AM 5,155 DisplayPanel.class
08/09/2009 08:35 AM 1,311 karst.class
08/09/2009 08:35 AM 5,411 Matrix.class
10 File(s) 28,520 bytes
2 Dir(s) 160,337,125,376 bytes free
C:\Users\projects>jar cvf beforesign.jar -C curvefitting_demo/ .
added manifest
adding: ControlPoint.class(in = 560) (out= 396)(deflated 29%)
adding: Curve.class(in = 1190) (out= 732)(deflated 38%)
adding: CurveControls.class(in = 2367) (out= 1353)(deflated 42%)
adding: CurveControls2.class(in = 2417) (out= 1408)(deflated 41%)
adding: CurvePanel.class(in = 4815) (out= 2725)(deflated 43%)
adding: Demo.class(in = 1186) (out= 736)(deflated 37%)
adding: DemoPanel.class(in = 4108) (out= 2391)(deflated 41%)
adding: DisplayPanel.class(in = 5155) (out= 3170)(deflated 38%)
adding: karst.class(in = 1311) (out= 981)(deflated 25%)
adding: Matrix.class(in = 5411) (out= 3129)(deflated 42%)
C:\Users\projects>
I can execute the jar file without error
java -jar beforesign.jar
I then sign the jar file as follows, using a certificate bought from GoDaddy, using the instructions at http://support.godaddy.com/help/article/4780/signing-java-code ("approach 1").
jarsigner -verbose -keystore codesignstore -storepass password -keypass password -tsa http://tsa.starfieldtech.com/ -signedjar aftersign.jar beforesign.jar codesigncert
Now, when I try to verify the signed jar file I get:
$ jarsigner -verify -verbose -certs aftersign.jar
jarsigner: java.lang.NullPointerException
$
and
$ java -jar aftersign.jar
[mdwilkie#cobalt ~/codesigning]$ java -jar aftersign.jar
Exception in thread "main" java.lang.NullPointerException
at sun.security.util.SignatureFileVerifier.getTimestamp(SignatureFileVerifier.java:538)
at sun.security.util.SignatureFileVerifier.getSigners(SignatureFileVerifier.java:481)
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:225)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:193)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:294)
at java.util.jar.JarVerifier.update(JarVerifier.java:205)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:338)
at java.util.jar.JarFile.getInputStream(JarFile.java:403)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:706)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:266)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Demo. Program will exit.
But Demo.class is there (in both jar files):
$ jar tvf beforesign.jar
0 Tue Mar 04 14:47:10 PST 2014 META-INF/
89 Tue Mar 04 14:47:10 PST 2014 META-INF/MANIFEST.MF
560 Tue Sep 08 08:35:48 PDT 2009 ControlPoint.class
1190 Tue Sep 08 08:35:48 PDT 2009 Curve.class
2367 Tue Sep 08 08:35:48 PDT 2009 CurveControls.class
2417 Tue Sep 08 08:35:48 PDT 2009 CurveControls2.class
4815 Tue Sep 08 08:35:48 PDT 2009 CurvePanel.class
1186 Tue Sep 08 08:35:48 PDT 2009 Demo.class
4108 Tue Sep 08 08:35:48 PDT 2009 DemoPanel.class
5155 Tue Sep 08 08:35:48 PDT 2009 DisplayPanel.class
1311 Tue Sep 08 08:35:48 PDT 2009 karst.class
5411 Tue Sep 08 08:35:48 PDT 2009 Matrix.class
$ jar tfv aftersign.jar
769 Tue Apr 08 13:31:46 PDT 2014 META-INF/MANIFEST.MF
872 Tue Apr 08 13:32:02 PDT 2014 META-INF/CODESIGN.SF
7304 Tue Apr 08 13:32:02 PDT 2014 META-INF/CODESIGN.RSA
0 Tue Mar 04 14:47:10 PST 2014 META-INF/
560 Tue Sep 08 08:35:48 PDT 2009 ControlPoint.class
1190 Tue Sep 08 08:35:48 PDT 2009 Curve.class
2367 Tue Sep 08 08:35:48 PDT 2009 CurveControls.class
2417 Tue Sep 08 08:35:48 PDT 2009 CurveControls2.class
4815 Tue Sep 08 08:35:48 PDT 2009 CurvePanel.class
1186 Tue Sep 08 08:35:48 PDT 2009 Demo.class
4108 Tue Sep 08 08:35:48 PDT 2009 DemoPanel.class
5155 Tue Sep 08 08:35:48 PDT 2009 DisplayPanel.class
1311 Tue Sep 08 08:35:48 PDT 2009 karst.class
5411 Tue Sep 08 08:35:48 PDT 2009 Matrix.class
I'm using java version 1.6
I have the same problem since yesterday. It looks like the problem is with the timestamping. Try to use another timestampserver instead of tsa.starfieldtech.com
You can try https://timestamp.geotrust.com/tsa/ or http://adobe-timestamp.geotrust.com/tsa/
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.
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.