0

I have a small project in java, its 'tree' goes like this:

File Tree:

file tree

And i can't seem to be able to read image.jpg in my code like this:

private BufferedImage image = null;
try{
    image = ImageIO.read( new File( "resources\image.jpg" ) );
   /* ...  */
}
catch( IOException exc ){ /*...*/ }

What should be the so-called relative directory in this case? I went through a dozen of similar posts but haven't found answer so far.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Show the *package* structure of your code. Is the code and resources within a jar file? – Hovercraft Full Of Eels Apr 30 '20 at 13:56
  • Depends on if you're trying to read it as a resource (e.g., on the classpath) or as a file (depends on execution context). – Dave Newton Apr 30 '20 at 13:56
  • I am fairly new to Java and don't exactly know what the *package* structure of my code is, but the code and resources are not within any jar file as I know. I work is VS Code and simply create & add files and folders. – Bartosz Świtalski Apr 30 '20 at 13:58
  • Execution context is to later draw this read image with a boolean Graphics.drawImage(...) – Bartosz Świtalski Apr 30 '20 at 13:59
  • It also depends on what project you are working on. Is it a general Java project? One simple way to know what is your current relative path is to create a file or an image. See where it goes to. – user3437460 Apr 30 '20 at 13:59
  • In VS Code, as I work with it, there is an option to "copy relative path", but as I c&p it into desired path of new File( desired_path ) my app still cannot find it somehow. – Bartosz Świtalski Apr 30 '20 at 14:02
  • also, so-called absolute path is working. – Bartosz Świtalski Apr 30 '20 at 14:08
  • And you still need to give us a more clear indication of your program's structure, including the packages it is using, where the files are relative to each other. Show some relevant code, including the package declaration as well as an image of your file tree showing where things are. Then be sure to place the images in a file where they can be accessed as resources, not as files. – Hovercraft Full Of Eels Apr 30 '20 at 14:10
  • As soon as I figure out how to add correct package declaration I will get back here. – Bartosz Świtalski Apr 30 '20 at 14:18

1 Answers1

0

Update path like this:

private BufferedImage image = null;
try{
    image = ImageIO.read( new File( "src/java/resources/image.jpg" ) );
   /* ...  */
}
catch( IOException exc ){ /*...*/ }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28