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

  • SEARCH
  • Home
  • 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 8177569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:32:37+00:00 2026-06-06T23:32:37+00:00

Possible Duplicate: Loading resource bundles using a custom class loader I’ve made a custom

  • 0

Possible Duplicate:
Loading resource bundles using a custom class loader

I’ve made a custom class loader which downloads a jar from my website, and then runs it.
The problem is: my (downloaded) jar needs some resources such as png images, etc…

If I run my (downloaded) jar directly at a command prompt, it works. The resources are loaded like that:

getClass().getResource("/images/logo.png")

Now, if I run it with my custom class loader, using:

Class<?> c = classLoader.loadClass("MainClass");
c.getMethod("main", new Class[] { java.lang.String[].class}).invoke(null, new Object[]{new String[] {"arg1_goes_here"}});

I get a null exception because it can’t load the resource.

When I load my jar file into memory, I do something like that:

String name = entry.getName();
if(name.endsWith(".class")) {
    name = name.substring(0, name.length() - 6).replace('/', '.');
    classbytes = getResourceData(input);
    classes.put(name, classbytes);
} else {
    classbytes = getResourceData(input);
    if (name.charAt(0) != '/') {
        name = "/" + name;
    }
    resources.put(name, classbytes);
}

I’m loading my class using:

defineClass(classToLoad, buffer, 0, buffer.length, null);

where buffer is the byte array containing the code.

So my question is the following: Is there a kind of defineResource() in Java?

Thanks.

  • 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-06T23:32:38+00:00Added an answer on June 6, 2026 at 11:32 pm

    Problem solved,

    I’ve written my own method public InputStream getResourceAsStream(String name).

    Here is the code:

    public InputStream getResourceAsStream(String name)
    {
        byte[] buffer = resources.get(name);
        return new ByteArrayInputStream(buffer);
    }
    

    resourcesis a Map that contains my resource data.

    Then, in my code, I can use the inputstream to get back my resource. Easy and safe.

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

Sidebar

Related Questions

Possible Duplicate: Dynamic Resource Loading Android In Android, I can load a string from
Possible Duplicate: Error loading Active Record gem with sinatra app using RVM I have
Possible Duplicate: Change the URL in the browser without loading the new page using
Possible Duplicate: Css and javascript Not Loading in rewrite rule After using only rewrite
Possible Duplicate: Dynamically loading a class in Java hi i am trying to create
Possible Duplicate: Loading multiple versions of the same class What is the best way
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Possible Duplicate: C/C++ Image Loading I need a C\C++ cross-platform library for loading/displaying images(jpg,gif,png,tga).
Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following

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.