Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:58:51+00:00 2026-06-10T08:58:51+00:00

I have a Java project called MyProject. I have a few different packages (keeping

  • 0

I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:

src/PackageA
src/PackageA/PackageAa
src/PackageA/PackageAa/PackageAaa
src/PackageB
src/PackageB/PackageBa
src/PackageB/PackageBa/PackageBaa

I have a class

src/PackageA/PackageAa/PackageAaa/MyJavaFile.java

And I have an image

src/PackageB/PackageBa/PackageBaa/MyImage.png

Inside of MyJavaFile.java, I would like to declare an Image oject of MyImage.png

Image img = new Image(....what goes here?...)

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:58:53+00:00Added an answer on June 10, 2026 at 8:58 am

    You could either call Class.getResource and specify a path starting with /, or ClassLoader.getResource and not bother with the /:

    URL resource = MyJavaFile.class
          .getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
    

    or:

    URL resource = MyJavaFile.class.getClassLoader()
          .getResource("PackageB/PackageBa/PackageBaa/MyImage.png");
    

    Basically Class.getResource will allow you to specify a resource relative to the class, but I don’t think it allows you to use “..” etc for directory navigation.

    Of course, if you know of a class in the right package, you can just use:

    URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png");
    

    (I’m assuming you can pass a URL to the Image constructor in question. There’s also getResourceAsStream on both Class and ClassLoader.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java project in eclipse with different packages. How can I compile
I have a Java project in Eclipse with ~10 packages and ~10 class files
I have one java project in netBeans. There are some packages, and classes in
I have a Java project called ood_100 to which I am trying to add
I've managed to install StatET into eclipse, and I have a Java project called
I have a java application build upon Spring 3. This project has another jar
I'm trying to work this out. In my project, i have a file called
I have this simple java code: package com.androiddesktoptest.testtest; public class AndroidDesktopTestMain { public static
I have a java project that uses JPA 2/Hibernate 3.5.6 for data access and
I have a Java project that needs a addon interface. I was thinking about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.