Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Book -> petrol pump example -> reading resources #100

Closed
kostadinnm opened this issue Jun 2, 2016 · 2 comments
Closed

Book -> petrol pump example -> reading resources #100

kostadinnm opened this issue Jun 2, 2016 · 2 comments

Comments

@kostadinnm
Copy link

Hello,

I am trying to follow the book and I got up to chapter 4 - the petrol pump example. However, I have difficulty running the example. The thing is, although I focus on the logic as you advice, I am writing all of the code(except the lib itself, of course) by hand. It seems we're using the same IDE(Intellij Idea), but I hadn't even opened the project you provide(I followed the advice to install sodium as a maven package). I've written the first three versions of the pump - LifeCyclePump, AccumulatePulsesPump and ShowDollarsPump. When I try to run it, I get an exception when it tries to read the images and sounds - "File not found, etc". I need to continue and I need a working example, so I decided not to go too deep into 'Java & resources'. And, because we have different project structure, I think I need to move the image and sound files a couple of folders.

So I tried copying the two resource folders("images" and "sounds") one folder up and down, but it still gives me the same exception. The path(a call to .getPath() on the URL object) is a String similar to this:
<mymachine>/<projects>/FRP_FuelPump/../images/petrol-pump-front.png
It looks like the two dots("..") are disturbing the execution. Without them(I fix the value through debugging)the problematic row(PetrolPump.java -> line 95):
background = ImageIO.read(new URL(rootURL, "../images/petrol-pump-front.png"));
passes like charm and no exception is thrown. I couldn't find anywhere on the Internet, how in Java relative paths are constructed with the double dot(s) syntax/notion?! Also, this is very rare(PetrolPump.java -> line 459):
URL rootURL = new URL("file:.");
At first, I thought it could be separator issue, but replacing the '/' with File.separator runs exactly the same.
Could you help me out with this?! As you might have already guessed, I am new to Java as well and this got annoyed as I am unable to continue with the important stuff Sodium provides :)

With regards,
Kostadin

@kostadinnm
Copy link
Author

Sorry, guys - my mistake. I misplaced the two folders. What tricked me were the double dots("..") shown in the debugger. I think I should've been using getCanonicalPath() or sth to get better understanding of what's happening.

For those who are writing it all by themselves(and are using the generic maven template/archetype, where you have src/main/java), the "images" and "sounds" folders should be placed within their projects folder(i.e. along with your other projects, and NOT in the Fuel Pump project dir).

Still, I might make up a better solution and if it ok, I may present it :)

Cheers,
Kostadin

@the-real-blackh
Copy link

I'm glad you figured it out! I put those directories in odd places in order to share them with other languages. I'm happy to change it if you have a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants