aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes getResource always returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "getResource always returns null" Watch "getResource always returns null" New topic
Author

getResource always returns null

Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
Hi there. I'm preparing my submission for the SCJD, and have stumbled across what so many of you already have, the whole 'must load the resource from the jar' bit.

I got that working for images in a folder, but I couldn't get that working for the database file at the top level of the jar, located outside any package. I finally broke down and used



and managed to locate it, but it felt wrong. I don't know why I couldn't load that file like I did the images - the only difference was, it wasn't in a package.

That brought to light a different error - now it can locate the file, but when it tries to create a new RandomAccessFile from it like this, it fails.



Which makes sense, since I also read you can't modify files in the JAR. But that does leave me with one pressing question: Will the reviewer be extracting the files from the JAR, before testing it? If not, I don't see how I can possibly modify this file.

Any suggestions?


Mxolisi Veco
Ranch Hand

Joined: Jan 14, 2010
Posts: 59
I suggest you use Use Roel's
Submission Jar: test case + Ant build script to build your jar.
to build your jar, it will save you some time and trouble. I think that build.xml file will put your images somewhere in the runme.jar file.


Will the reviewer be extracting the files from the JAR, before testing it?
I do not know.
If you build your jar using Roels build.xml file, then you do not have to worry about that.
There is also a JUnit test that you can use to test the jar file created by Roel's build.xml.
Just search for Submission Jar: test case + Ant build script to build your jar
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Cindy,

That's an easy one: your database file is not supposed to be in the jar. Before your client application (standalone mode) or server application starts, the user will have to point to the database file! The database file should NOT be packaged inside the runme.jar; it should be packaged into the submission jar on the path specified by your instructions.

Will the reviewer be extracting the files from the JAR, before testing it?

If I execute java -jar path_to_jar/runme.jar alone the application should run in standalone mode, no extractions of files (inside the runme.jar) should be needed!

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Mxolisi Veco wrote:I suggest you use Use Roel's Submission Jar: test case + Ant build script to build your jar.

Seems an excellent idea to me

Mxolisi Veco wrote:Just search for Submission Jar: test case + Ant build script to build your jar

Or have a look at the ScjdFaq (you can find a topic with link to the appropriate thread).
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
But in a message you posted here, you stated:

Roel De Nijs wrote:Of course I am sure! And to prove it, here is a (snippet of the) listing of my submission jar created with the jar tool:[code]
C:\Java> jar tf submission.jar
META-INF/
META-INF/MANIFEST.MF
code/
docs/
db-1x1.db
runme.jar
version.txt


That shows the db-1x1.db file inside the submission jar file. Don't forget, we're talking about two jar files here - the 'runme.jar' file, and the submission jar file containing everything. My question is if they extract the contents of the outer jar file first, which would expose the runme.jar and the database at the same level.
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

The submission jar is just a tote for keeping the disparate parts together. It'll be fully expanded, and the evaluator will then run runme.jar with the appropriate arguments.
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
Thanks David!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Cindy Carney wrote:That shows the db-1x1.db file inside the submission jar file.

Exactly! According to my instructions that was the location I had to put my database file. And of course the submission jar will be extracted, because it is no more than an archive (could be easily a zip) so you can simply submit 1 file instead of hundreds of files.
The runme.jar will not be extracted, should not contain the database file and you should not try to access the database file from a jar at all. The user (evaluator) will point to the file on its filesystem to run your application in different modes.

Kind regards,
Roel
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
OK, thanks for the clarification, Roels. I appreciate it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getResource always returns null