• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Finding absolute path to resource files.

 
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey folks,

Using this method:


I'm trying to find the absolute path to resources sitting in the same package as the class that's using them.
when I do this...


it works fine, but when I try to use it here...


I get a file not found exception. I'm not sure what I'm missing here.

Switch that, it's working for loadTemplate(), but not getStylesheets().
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the file in a jar file? What is the URL it is returning?
 
Greg Zobel
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:Is the file in a jar file? What is the URL it is returning?



I've changed findPath() attempting to figure out what's happening:



So, when I run from NetBeans this is my output:


file:/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/timeclock/animatedClock.css
SQL Server Connection Ready
/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/sql/templates/getEmployeeData.sql
/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/sql/templates/ifClockStatusExists.sql
Clock Status Exists = true
/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/sql/templates/getClockStatus.sql
Clock status = false
Punching In
/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/sql/templates/setClockStatusIn.sql
/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/build/classes/mwsmfg/sql/templates/addPunchIn.sql
Result Set Closed
Statement Closed
Connection Closed



Everything works as expected. But when I run from the command line I get this:


C:\Users\Greg\NetBeansProjects-MWSMFG\MWSMFG-timeclock\dist>java -jar mwsmfg-timeclock.jar
jar:file:/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/dist/MWSMFG-timeclock.jar!/mwsmfg/timeclock/animatedClock.css
SQL Server Connection Ready
file:/C:/Users/Greg/NetBeansProjects-MWSMFG/MWSMFG-timeclock/dist/MWSMFG-timeclock.jar!/mwsmfg/sql/templates/getEmployeeData.sql
Script template was not found.
Statement Closed
Connection Closed



The .css file loads just fine, looking at this, I see there is no "jar:" on the path to the .sql file. Is there another method I should be using to acquire that path? If I use the same return as for the .css file it stops working in Netbeans as well.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't shown the code that is attempting to use those files, but when you're running from the commandline the files appear to be in jar files. If the code was expecting the URL's to be to a file on disk then it probably doesn't know what to do with a url to jar contents.

Can you show the code that is throwing the exception?
 
Greg Zobel
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:You haven't shown the code that is attempting to use those files, but when you're running from the commandline the files appear to be in jar files. If the code was expecting the URL's to be to a file on disk then it probably doesn't know what to do with a url to jar contents.

Can you show the code that is throwing the exception?



Sure can. I was thinking it had something to do with them being in the jar, but not understanding why resourceURL.toExternalForm() works for the .css files and not the .sql files.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Zobel wrote:

Sure can. I was thinking it had something to do with them being in the jar, but not understanding why resourceURL.toExternalForm() works for the .css files and not the .sql files.



If you were getting a FileNotFoundException it was not coming from URL.toExternalForm(). That does not throw any checked Exceptions. I imagine your exception will have been coming from the FileInputStream, because the URL you gave it is not to a file on the file system. If you post the stack trace we can confirm that.

You should take a look at Class.getResourceAsStream(). That will give you a stream you can use to load the bytes from the files, even if they are inside Jar files.
 
Greg Zobel
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll definitely take a look at getResourceAsStream(). Here's the stack trace.


java.io.FileNotFoundException: file:\C:\Users\Greg\NetBeansProjects-MWSMFG\MWSMF
G-timeclock-gui\dist\MWSMFG-timeclock-gui.jar!\mwsmfg\sql\templates\getEmployeeD
ata.sql (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at mwsmfg.sql.templates.SQLTemplateLoader.loadTemplate(SQLTemplateLoader
.java:35)
at mwsmfg.dbconnect.SQLScriptManager.getEmployeeData(SQLScriptManager.ja
va:25)
at mwsmfg.dbconnect.PunchClock.<init>(PunchClock.java:32)
at mwsmfg.timeclock.MWSTimeClockController.processPunch(MWSTimeClockCont
roller.java:75)

etc...

 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

file:\C:\Users\Greg\NetBeansProjects-MWSMFG\MWSMFG-timeclock-gui\dist\MWSMFG-timeclock-gui.jar!\mwsmfg\sql\templates\getEmployeeData.sql


That bold part isn't a file - it's a file inside a JAR file. You can't use FileInputStream for that.

But why not refactor your method to take an InputStream instead? You can also overload the method so your existing code calls the new one that takes an InputStream. Just remember to not close any InputStream that is given as an argument of your method. That method didn't create it, isn't the owner, so should close it. That should be the responsibility of the calling method.
 
Greg Zobel
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:
But why not refactor your method to take an InputStream instead? You can also overload the method so your existing code calls the new one that takes an InputStream. Just remember to not close any InputStream that is given as an argument of your method. That method didn't create it, isn't the owner, so should close it. That should be the responsibility of the calling method.



Just to be sure I understand, are you saying have findPath() return an InputStream rather than a URL? If I use try with resources to get the reference to an input stream it will auto close once that reference is no longer used, correct?
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
findPath can return a URL; you can then use openStream() to get an InputStream to that URL.

And yes, with try-with-resources the stream will be closed when the block ends.
 
Greg Zobel
Greenhorn
Posts: 23
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've changed findFile to:


load my css files like this:


and my sql files like this:


I'm sure it could be done more elegantly, but at least now it runs both from within NetBeans and the command line.

Thanks for the help guys!
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic