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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:34:21+00:00 2026-05-15T00:34:21+00:00

I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new

  • 0

I have the following snippet of java code:

final Class<?> junitCoreClass = AccessController.doPrivileged(
    new PrivilegedAction<URLClassLoader>() {
      @Override
      public URLClassLoader run() {
        return new URLClassLoader(new URL[] { junitJarUrl });
      }
    }).loadClass("org.junit.runner.JUnitCore");

System.out.println(junitCoreClass.getName());
final JUnitCore junitCore = (JUnitCore) junitCoreClass.newInstance();

This compiles fine. But when I try to run it, something weird happens; a java.lang.NoClassDefFoundError is thrown on that last line, referring to the class just loaded. The weird part is, the println prints the exact class name.

I checked that if I keep the new instance reference as an Object and manipulate it only through reflection, everything’s fine, so the offending piece of code must be the explicit cast.

Can someone explain to me why this happens, and also tell me how I can achieve what I’m trying to do?

PS: For those who wants to see a closer stack trace, there’s not much to show:

java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
  at [last line of example)
  [lines from my app]
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
  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:315)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
  at [last line of example]
  [lines from my app]
  • 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-15T00:34:22+00:00Added an answer on May 15, 2026 at 12:34 am

    The problem is that your main class is loaded by the system class loader (the one containing -classpath) that does not have JUnit on the classpath. Then, you create a separate class loader that has only JUnit on the class path. When your main class attempts to cast to JUnitCore, the system class loader is asked to load JUnitCore, which it does not contain, so NoClassDefFoundError occurs.

    There is no convenient way to do what you’re trying to do without using reflection. You will need to (1) create a separate class that accesses JUnitCore directly, (2) include a path to that class on URLClassLoader (directory or JAR), (3) use reflection to load that class, and (4) use reflection to invoke a method on that class.

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Add this location = / { rewrite ^(.*)$ /index.htm last;… May 16, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer Tooltips display automatically. That's a bit of a problem, the… May 16, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer If you're asking how to change a BLOB column to… May 16, 2026 at 6:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following snippet of java code: File directoryToMoveTo = new File(file.getParent()+_TEMP); boolean
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I have the following method being called in a Java EE web application. public
i have the following code snippet, in an appliation working with Zend Framework. I
Let's consider the following code: SimpleDateFormat sdf = new SimpleDateFormat(HH:mm:ss dd/MM/yyyy, Locale.US); long start
I have the following snippet of JS var Customer : function() { this.ShipProduct :
I have a weird issue. I receive the following error that causes a force-close:
The following snippet is not working for me. Basically I want to check if
I'm using the following snippet to create a select/deselect all checkboxes: http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/ This is

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.