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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:59:35+00:00 2026-06-08T02:59:35+00:00

How can I instantiate an object of the class java.lang.Class from a given .java

  • 0

How can I instantiate an object of the class java.lang.Class from a given .java file?

I want to create an application to automatically generate JUnit tests. For that I would need “Method” objects and for “Method” objects I need to have a “Class” object.

  • 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-08T02:59:36+00:00Added an answer on June 8, 2026 at 2:59 am

    java 6 onwards has an api for the compiler: http://www.javabeat.net/2007/04/the-java-6-0-compiler-api/

    the link above includes an example.

    here’s another example – http://www.java2s.com/Code/Java/JDK-6/JavaCompilertoolshowyoucancompileaJavasourcefrominsideaJavaprogram.htm

    to load the file once compiled you use a classloader. there’s an example at http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html and another at http://www.javaworld.com/jw-10-1996/jw-10-indepth.html

    you’d think there would be a library to simplify all this. i can’t find one, but am still looking.

    meanwhile, here’s a really nice article from ibm that compiles a function and plots it – http://www.ibm.com/developerworks/java/library/j-jcomp/index.html

    found one http://docs.codehaus.org/display/JANINO/Home – this is a library that simplifies the process. i would recommend configuring it to use the javax.tools API (see last sentence in the “what is Janino” paragraph).

    sorry for the google snark earlier.


    it just struck me that maybe you just want a class object.

    if you have a class called MyClass then the associated class is Myclass.class. that’s probably obvious, but perhaps that’s all you need.

    and if you have the class name in a string you can use this method – http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)

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

Sidebar

Related Questions

Given an instantiated JavaScript object/class can I make an Ajax call from that class
In java, can I use a class object to dynamically instantiate classes of that
I'm trying to create a class that can instantiate arrays at runtime by giving
How can I instantiate a class instance in java with generics? I am trying
In Java we can instantiate an object (knowing only the parameters types) dynamically like
I am trying to instantiate a Java abstract class from my Groovy code. Considering
Using the C# object initializer syntax I can instantiate an anonymous object like this:
How can I instantiate an anonymous object while passing the propertynames and values as
Can I avoid instantiate a Db object inside of Names object to access it
Is it possible to assure that only spring can instantiate a class, and not

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.