• Post Reply Bookmark Topic Watch Topic
  • New Topic

Maven Created JAR: Can't Locate .CSS File  RSS feed

 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I've used Maven to build a fat JAR of a GUI application that uses JavaFX.  I've used a .CSS file as a stylesheet for the GUI.  It works fine in my IDE (Eclipse) and my folder structure is as follows

src/main/java/*all my app's packages*
src/main/resources/database/Spring-Datasource.xml
src/main/resources/database/Spring-Login.xml
src/main/resources/images/background.jpg  -- This is the jpeg that my stylesheet uses as a background for the GUI
src/main/resources/stylesheets/GUI.css -- This is my stylesheet, code for which is below
src/main/resources/Spring-Module.xml




The JAR is created in a directory named "target" by Maven and the directory also contains directories named

classes
generated-sources
generated-test-sources
maven-archiver
maven-status
surefire-reports
test-classes

When I double-click on the JAR it launches but the GUI has not used the jpeg as it appears it can't locate it.  What should the directory structure be for the JAR to be able to use this?
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't show the code used to open the image, but I suspect that if you want it to work in Eclipse and from a jar file you will need java.lang.Class.getResource(String name) or java.lang.ClassLoader.getResourceAsStream(String name).  "name" is the filename, which should start with a slash, as is "/images/background.jpg".
 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Knute.  This is the line of code I'm using to access the stylesheet.



If I change that to "/images/background.jpg" I'd lose formatting specified in the stylesheet, which I'd rather avoid doing.
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha.  Well, what you've done seems correct, assuming that -fx-background-image acts like -background-image.  Is the image in the jar file?
 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the image is in a subdirectory of the directory that contains the JAR.  I've not included the image in the JAR itself (I didn't know that was possible?).
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Ritchie wrote:No, the image is in a subdirectory of the directory that contains the JAR.  I've not included the image in the JAR itself (I didn't know that was possible?).


Hi
A suggestion on this.Please place the image file in the resources (just include) of src and start using the commands as usual .
This is just to easen up  the procedure of manuallly including the imagae file (ie presume  as jar -uvf  jarfile imagefile  ) in nutshell to make
you understand that  the maven will do it for you while creating the so created jar file or so that you were referring.No need of gotcha.
Please reply
Thanks
As
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Ritchie wrote:No, the image is in a subdirectory of the directory that contains the JAR.  I've not included the image in the JAR itself (I didn't know that was possible?).

I believe that the image needs to be in the jar file.
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just created a project that did a getResourceAsStream() with a JPEG in /src/main/resources/images, then I created a jar file and executed it with the image in the jar, and it worked fine.  Maven took care of everything.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the correct method.on the same lines I i tried a java application program that uses a random accessfile with xx.bin file to provide data for the  same.Will you please tell how i should make the method to enable execution of the jar (after creation by maven)by a double click.(Here i replaced with xx.bin file  for image file as suggested only to err.pleas help
ie) how should be ny POM,src etc
O atleast where to include the xx.bin so as to it is included in the created jar gfile
Thanks
As
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, if you ask a new question you should start a new thread.  But this question may be closely related to the original so I'll let it stand.
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you please tell how i should make the method to enable execution of the jar (after creation by maven)by a double click.

I believe you're asking how to make the jar file you create executable.  There are many way to do this, for instance, your IDE will most likely have a way to create executable jar files.  In Eclipse, it is File > Export > Java > Runnable Jar File.

Making the jar file execute on a double-click is a function of Windows (I'm assuming Windows -- does Mac have double click to execute too?)  Basically, you associate the jar file with Java.

But since this is a Maven forum, let's assume you want Maven to create your executable jar.  You can do this with a plugin: Shade.  You just tell Maven the name of the main method to execute first.  In this snippet, I'm launching net.snortum.scrabblewords.view.ScrabbleWords:
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Here i replaced with xx.bin file  for image file as suggested only to err.pleas help 

You will need to post the error and stacktrace in order for me to help you.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
(Here i replaced with xx.bin file  for image file as suggested only to err.pleas help 

You will need to post the error and stacktrace in order for me to help you.

Hi
this is just to inform that the data file viz XX.bin that i was able to include it inside the executable jar file created by mave is not able to execute  itself on double click click also does not produce any error message.(Random access file that uses data from xx.bin is not compatible with the jar archive perhaps.But the same executable file works in the normal command
ie)

java -jar target/jupiter.jar  provided the xx.bin  file is available in the same target folder.

Where as my so many of my  other java programs that were made executable file file by maven work fine on double click but not this referred one that uses randomaccess file via .xx.bin

With Seasons Best Wishes,
and thanks
As
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure you need to have the random-access file in the jar to be able to double click it.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
while appreciating & acknowledging your authenticity ,i still need my data file viz xx.bin in order to make the maven created executable jar file  work although this same data file has already been included in the jar by maven.(out of curiosity)Also i included  java.io.RandomAccessFile,java.util.Random  in the jar but in vain.As suggested by you.everything is ok but double-clicking is only non functional.
Also i would inform that jar for created in the traditional way viz., jar -cvmf jar-name .class file xx.bin  -->works with doubleclick .Thanks
As
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you could post your pom.xml file here I might be able to see if something is wrong. 
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The pom is as below.
thanks
As
CRMK


 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May not make a difference, this version should be 3.0.0:

You have the real class name here in your pom file?

When you open the jar file made by Maven, do you see your bin file in there?
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Everything is as usual,making hardly any differece,thus maven is not kind enough to render us any solution viz doubleclicking is tough.The files are (Both JavaExam.java and its datafile viz ramanavami.bin are very well parked inside the jar by maven being not usable.Please go through the whole lot and initialize to enable for double clicking feature.
Thaks even in  the mids of this error
AS
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one more thing before we put this thread to bed: try using the Maven Shade plugin instead of Assembly.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

  Last but not the least Why cant Maven be more than Robo as for example enabling all kinds of flexibility in the formation of project structure design especially so for java apps with data file as above.Me i am still trying to figure out a good design with src/main/resource /data along a pom to dump at the  target a good executable jar that quickly does  the mouse operations thus helping the the whole lot of Java Users
Hope this suffices time being-cheers
As
CRMK 
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The design of Maven was to work with a default format.  Some people like the default and others don't.  You can change any default you want, it's just not easy sometimes.  So Maven has flexibility, it's just easier not to fight the defaults.
 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Simon Ritchie wrote:No, the image is in a subdirectory of the directory that contains the JAR.  I've not included the image in the JAR itself (I didn't know that was possible?).

I believe that the image needs to be in the jar file.


Forgive me for asking but how can I include the image in my JAR?

This is my POM.xml

 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files in the resource directory will automatically be copied to the jar file when it's created.  The default resource directory is

src/main/resources

You can change this default with:

 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  I added those entries to my POM but trying to run the JAR from the command line I now get a message saying "Could not find or load main class <my JAR name>".  I've specified the main class in my build so I don't understand why that's happening?
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the exact error message?  It will probably point you to a line in your POM file.  Could you copy that portion of the POM file here (or even the whole POM file if it's easier)?
 
Simon Ritchie
Ranch Hand
Posts: 104
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid the message doesn't point to any line in the file.  But here is the full POM.xml

 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The design of Maven was to work with a default format.  Some people like the default and others don't.  You can change any default you want, it's just not easy sometimes.  So Maven has flexibility, it's just easier not to fight the defaults.


Hi
Been trying all the while to inculde my xx.bin file so as to get it executed as i earlier referred.I sincerely now agree to your
point that it is not easy sometimse., though this is rightly said i have yet another maven's help who says in order to make the plugin or any jar file the non default "classifier" can be attached as supplemental  for the artifactId instead of completely replace it.If this helplfu to us, would thy self be able to give us the method and ways to include it in the pom Once this non -default procedure is ok then  i think we have a fair chances for total success where in i would be able to make the jar (randomaccessfile used one)
workable on the double click mouse operation.
In the event of our success
Cheers
As
CRMK
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You shouldn't need these lines, unless your resource folder really is in the root folder.  The default resource folder is src/main/resource in the project folder, not in root (no initial slash).  Everything from the resource folder is copied into the jar file automatically.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
You shouldn't need these lines, unless your resource folder really is in the root folder.  The default resource folder is src/main/resource in the project folder, not in root (no initial slash).  Everything from the resource folder is copied into the jar file automatically.

Hi
   
        Indeed your suggestion motivated me to further add the base dir etc
        as under to make the jar file work on double click but only inside the target
        without itself independently executable.It requires the copied file .bin for its functioning that was assisted
        by the #{base dir} src /main only within the target dir so the problem is still there
                                             
                                             
                                           <resources>
                        <resource>
    <directory>${basedir}/src/main/resources</directory>
         <includes>
          <include>sitemanager</include>
             </includes>
                     </resource>
                       <resource>
                      <directory>${basedir}/src/main/resources</directory>
                      <includes>
                          <include>ramanavami.bin</include>
                      </includes>
                      </resource>
                          </resources>
                     <directory>C:\Program Files\java\bin\apache-maven-3.3.9-bin\bin\target</directory>
   
                   The same is now open for further modification at thy self.if it is success well and good
                  thanks and good luck
                  As
                  CRMK






 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!