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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:55:20+00:00 2026-05-25T09:55:20+00:00

Situation: I’m making a simulator for plotting the time complexity of algorithms. The students

  • 0

Situation:

I’m making a simulator for plotting the time complexity of algorithms. The students can add load their own .java file to run it.
My program compiles (with ‘JavaCompiler’) the .java file. Then it tries to load the .class file:

loadedClass = loadClass("customAlgorithms."+this.getClassName()+"");

When running my program in Eclipse, everything works fine, and students can use the program flawlessly.

Problem:

But then I export my project to an executable jar file. The compiling part still works but loading the class fails because it searches it inside the jar file.

I was wonderding why I couldn’t just do this: (change . with /)

loadedClass = loadClass("customAlgorithms/"+this.getClassName()+"");

What options are possible? Here what I can think of:

  • adding the compiled .class file to the currently running .jar file. Can this even work?

    I know it is possible to edit a jar archive. But can this work while running and without having to restart the program?

  • other way to use ‘loadClass()’ ? So that I can load a class file which is not included in my jar file

Are there other ideas?

  • 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-25T09:55:20+00:00Added an answer on May 25, 2026 at 9:55 am

    Java loads classes via ClassLoaders. When you start a JVM, you have to tell the JVM its “classpath”, that is a list of folders or jar files where classes are. The JVM will create a ClassLoader (usually a subclass of URLClassLoader) and give this ClassLoader the list of folders and jar files, so that the classloader will be able to load the class.

    In your application, when you compile the java source file, it generates a class file, that is saved somewhere. Probably in eclipse, the folder where the class file is saved is a folder included in the classpath, so that the JVM will be able to find the class.

    When you export the project to a running jar file, probably the JVM will be started having only that JAR file in its classpath, so that it does not search anywhere else for your compiled class.

    You have several ways to fix this. In order of ascending complexity :

    1. Start your jar not as an executable jar, but using a .bat/.sh or any other java runner that makes it possible to configure the classpath, add a specific folder to the classpath, compile the java source files to that folder.
    2. When you compile the java file, create a folder and create a new instance of URLClassLoader pointing to that folder. Compile the java file to that folder. Then use that URLCLassLoader to load the compiled class.
    3. Compile the java source to RAM, to a byte array for example, and implement a custom classloader that will return that byte array instead of loading from the disk.

    Solution 1 is very simple, but has its drawbacks : it will not be possible to reload the compiled java file if not running the entire program again (once a class is loaded in a classloader, it cannot be loaded again, except using agents which are outside the scope of this question), it requires the program to write to disk which means permissions etc..

    Solution 2 and its evolution which is RAM only are much more elegant, but much more complicated : loading a single class from a classloader different thatn the one used by the rest of your entire application is tricky, you could get weird class exceptions (like ClassCastExceptions, ClassNotFoundException etc..) due to a mismatch between classloaders.

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

Sidebar

Related Questions

Situation: Site with content protected by username/password (not all controlled since they can be
Situation: I have some persons with certain skills and they can/might belong to more
Situation: I want to provide a website service where users can enter some data
Situation Well, as you can see below, I have a main App, creating a
Situation with Thai text on a client site is that we can't control where
Situation: Automatically generated memebers, such as MenuStrip items, have their (automatically generated) names based
Situation: We've got this DIV with thumbnails who need their src being changed on
Situation I have a few single-responsibility Servlets that take a request, do their job,
Situation is pretty straightforward. I have a Java webapp that I'm converting to be
[Situation] I have a custom item control which I add to a stack panel

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.