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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:26:03+00:00 2026-05-15T03:26:03+00:00

I have the class name stored in a property file. I know that the

  • 0

I have the class name stored in a property file. I know that the classes store will implement IDynamicLoad. How do I instantiate the class dynamically?

Right now I have

     Properties foo = new Properties();
    foo.load(new FileInputStream(new File("ClassName.properties")));
    String class_name = foo.getProperty("class","DefaultClass");
    //IDynamicLoad newClass = Class.forName(class_name).newInstance();

Does the newInstance only load compiled .class files? How do I load a Java Class that is not compiled?

  • 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-05-15T03:26:04+00:00Added an answer on May 15, 2026 at 3:26 am

    How do I load a Java Class that is not compiled?

    You need to compile it first. This can be done programmatically with the javax.tools API. This only requires the JDK being installed at the local machine on top of JRE.

    Here’s a basic kickoff example (leaving obvious exception handling aside):

    // Prepare source somehow.
    String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\"); } }";
    
    // Save source in .java file.
    File root = Files.createTempDirectory("java").toFile();
    File sourceFile = new File(root, "test/Test.java");
    sourceFile.getParentFile().mkdirs();
    Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));
    
    // Compile source file.
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null, sourceFile.getPath());
    
    // Load and instantiate compiled class.
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
    Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello".
    Object instance = cls.getDeclaredConstructor().newInstance(); // Should print "world".
    System.out.println(instance); // Should print "test.Test@hashcode".
    

    Which yields like

    hello
    world
    test.Test@cafebabe
    

    Further use would be more easy if those classes implements a certain interface which is already in the classpath.

    SomeInterface instance = (SomeInterface) cls.getDeclaredConstructor().newInstance();
    

    Otherwise you need to involve the Reflection API to access and invoke the (unknown) methods/fields.


    That said and unrelated to the actual problem:

    properties.load(new FileInputStream(new File("ClassName.properties")));
    

    Letting java.io.File rely on current working directory is recipe for portability trouble. Don’t do that. Put that file in classpath and use ClassLoader#getResourceAsStream() with a classpath-relative path.

    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName.properties"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a class that will store data in a file and I need
In this scenario, I have 2 or more models: class Store(models.Model): name = models.CharField(max_length
I have a property file which is inside a default package and the class
I want to have a class that defines my parameter data for a stored
I have a class Person and a class Name. Name contains two Strings firstName
So i have a class name repository which is just a simple array.Here is
I have a class like this, public class person { name Name {set; get;}
I have style sheet with a class name changebackgroundcolor i want make change in
I have many DIV with the same class name, let save wmplayer. I want
What I have in my project is the binary file that contains list of

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.