• Post Reply Bookmark Topic Watch Topic
  • New Topic

A few problems with my program  RSS feed

 
Uwe Pfeifer
Greenhorn
Posts: 21
Chrome IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a few problems with my program I'll explain them here now.
You can find my questions in the comments in the code.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't ask your questions in comments. It makes it very hard to find and read them.


I don't really know what problems you have, but it looks like one of them is that you can't find resources. This has to do with how Class#getResource() searches for the resource and gets a URL. From the API (here: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)):
... an absolute resource name is constructed from the given resource name using this algorithm:
* If the name begins with a '/', then the absolute name of the resource is the portion of the name following the '/'.
* Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.'


So when you use java.net.URL coffeeFull = MyCoffee.class.getResource("images/coffeefull.jpg"); it will be creating a URL relative to the MyCoffee.class' package, and you would expect to find the image at: [CLASSPATH or JAR file]/de/freakyonline/MyCoffee/images/coffeefull.jpg. If your images folder is actually top-level relative to your classpath/JAR file, then you should use '/' at the start of the path to indicate absolute path rather than relative: java.net.URL coffeeFull = MyCoffee.class.getResource("/images/coffeefull.jpg"); would look for [CLASSPATH or JAR file]/images/coffeefull.jpg.
 
Uwe Pfeifer
Greenhorn
Posts: 21
Chrome IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be nice if someone could help me I cant get any further. Maybe just showing me the proper way its done. For my IDA so the program runs like it should and for a executeable jar file. Ive set up maven and resources but I don't know how that works together.

I have this in the pom.xml for maven:
<directory>images/</directory>
<includes>
<include>*.jpg</include>
</includes>

This places the .jpg files in the root directory of the jar file, but in my program it looks for it in images/ however this is my next question if i get it to work that the pics are even displayed.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the question is about how to get Maven to package the jar file? Maybe for starters you can package the jar file manually, just to make sure it works fine when it's packaged the way Steve described.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People here would like to help you, but honestly, it's hard to tell what help you need. I see 5-6 issues that you are asking about. We find it generally works best if you post one question per thread. Fix that one issue before working on anything else.
 
Uwe Pfeifer
Greenhorn
Posts: 21
Chrome IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll ask one question then. How can I get this program to work like, put the files as images on the screen inside my applications window? I mean, how to set coffeFull and cofeeEmpty properly to let it display the pictures in the program?
 
Uwe Pfeifer
Greenhorn
Posts: 21
Chrome IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I got it working. the answer from steve helped. thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!