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 184059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:13:32+00:00 2026-05-11T15:13:32+00:00

I’m playing around with Reflection and I thought I’d make something which loads a

  • 0

I’m playing around with Reflection and I thought I’d make something which loads a class and prints the names of all fields in the class. I’ve made a small hello world type of class to have something to inspect:

kent@rat:~/eclipsews/SmallExample/bin$ ls IndependentClass.class kent@rat:~/eclipsews/SmallExample/bin$ java IndependentClass  Hello! Goodbye! kent@rat:~/eclipsews/SmallExample/bin$ pwd /home/kent/eclipsews/SmallExample/bin kent@rat:~/eclipsews/SmallExample/bin$  

Based on the above I draw two conclusions:

  • It exists at /home/kent/eclipsews/SmallExample/bin/IndependentClass.class
  • It works! (So it must be a proper .class-file which can be loaded by a class loader)

Then the code which is to use Reflection: (Line which causes an exception is marked)

import java.lang.reflect.Field; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader;  public class InspectClass {     @SuppressWarnings('unchecked')     public static void main(String[] args) throws ClassNotFoundException, MalformedURLException {         URL classUrl;         classUrl = new URL('file:///home/kent/eclipsews/SmallExample/bin/IndependentClass.class');         URL[] classUrls = { classUrl };         URLClassLoader ucl = new URLClassLoader(classUrls);         Class c = ucl.loadClass('IndependentClass'); // LINE 14         for(Field f: c.getDeclaredFields()) {             System.out.println('Field name' + f.getName());         }     } } 

But when I run it I get:

Exception in thread 'main' java.lang.ClassNotFoundException: IndependentClass     at java.net.URLClassLoader$1.run(URLClassLoader.java:200)     at java.security.AccessController.doPrivileged(Native Method)     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)     at InspectClass.main(InspectClass.java:14) 

My questions:

  1. What am I doing wrong above? How do I fix it?
  2. Is there a way to load several class files and iterate over them?
  • 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. 2026-05-11T15:13:32+00:00Added an answer on May 11, 2026 at 3:13 pm

    From the Javadocs for the URLClassLoader(URL[]) constructor:

    Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a ‘/’ is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

    So you have two options:

    1. Refer to the directory that the .class file is in
    2. Put the .class file into a JAR and refer to that

    (1) is easier in this case, but (2) can be handy if you’re using networked resources.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.