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

  • Home
  • SEARCH
  • 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 63987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:43:09+00:00 2026-05-10T18:43:09+00:00

How do you change the CLASSPATH of a Java process from within the Java

  • 0

How do you change the CLASSPATH of a Java process from within the Java process?


Before you ask me ‘Why would you want to do that?’ I’ll explain it shortly.

When you have a Clojure REPL running it is common to need more jars in your CLASSPATH to load a Clojure source file, and I’d like to do it without having to restart Clojure itself (which is not really an option when using it on Slime on Emacs).

That’s the reason but I don’t want this question tagged as some-weird-language some-weird-editor and be disregarded by the majority of Java developers that may have the answer.

  • 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-10T18:43:10+00:00Added an answer on May 10, 2026 at 6:43 pm

    Update 2023: as commented below by Holger

    The only way to add jar files to the class path working in Java 9 and newer, is through the Instrumentation API, but it requires a Java Agent.

    If you are in control of the launcher/main jar, you can use the Launcher-Agent-Class attribute in the jar file’s manifest to start an embedded Agent.


    Update Q4 2017: as commented below by vda8888, in Java 9, the System java.lang.ClassLoader is no longer a java.net.URLClassLoader.

    See "Java 9 Migration Guide: The Seven Most Common Challenges"

    The class loading strategy that I just described is implemented in a new type and in Java 9 the application class loader is of that type.
    That means it is not a URLClassLoader anymore, so the occasional (URLClassLoader) getClass().getClassLoader() or (URLClassLoader) ClassLoader.getSystemClassLoader() sequences will no longer execute.

    java.lang.ModuleLayer would be an alternative approach used in order to influence the modulepath (instead of the classpath). See for instance "Java 9 modules – JPMS basics".


    For Java 8 or below:

    Some general comments:

    you cannot (in a portable way that’s guaranteed to work, see below) change the system classpath. Instead, you need to define a new ClassLoader.

    ClassLoaders work in a hierarchical manner… so any class that makes a static reference to class X needs to be loaded in the same ClassLoader as X, or in a child ClassLoader. You can NOT use any custom ClassLoader to make code loaded by the system ClassLoader link properly, if it wouldn’t have done so before. So you need to arrange for your main application code to be run in the custom ClassLoader in addition to the extra code that you locate.
    (That being said, cracked-all mentions in the comments this example of extending the URLClassLoader)

    And you might consider not writing your own ClassLoader, but just use URLClassLoader instead. Create a URLClassLoader with a url that are not in the parent classloaders url’s.

    URL[] url={new URL("file://foo")}; URLClassLoader loader = new URLClassLoader(url); 

    A more complete solution would be:

    ClassLoader currentThreadClassLoader  = Thread.currentThread().getContextClassLoader();  // Add the conf dir to the classpath // Chain the current thread classloader URLClassLoader urlClassLoader  = new URLClassLoader(new URL[]{new File("mtFile").toURL()},                       currentThreadClassLoader);  // Replace the thread classloader - assumes // you have permissions to do so Thread.currentThread().setContextClassLoader(urlClassLoader); 

    If you assume the JVMs system classloader is a URLClassLoader (which may not be true for all JVMs), you can use reflection as well to actually modify the system classpath… (but that’s a hack;)):

    public void addURL(URL url) throws Exception {   URLClassLoader classLoader          = (URLClassLoader) ClassLoader.getSystemClassLoader();   Class clazz= URLClassLoader.class;    // Use reflection   Method method= clazz.getDeclaredMethod("addURL", new Class[] { URL.class });   method.setAccessible(true);   method.invoke(classLoader, new Object[] { url }); }  addURL(new File("conf").toURL());  // This should work now! Thread.currentThread().getContextClassLoader().getResourceAsStream("context.xml"); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The specific numbers in the question are from CCIR 601… May 11, 2026 at 10:26 am
  • added an answer notepad++ -> view -> clone to another view You can… May 11, 2026 at 10:26 am
  • added an answer I don't think you can do that using the DataContractSerializer.… May 11, 2026 at 10:26 am

Related Questions

How do you change the CLASSPATH of a Java process from within the Java
If your data change after the application is deployed, how do you keep the
How do you change controls' Z-order in MFC at design time - i.e. I
For an ecommerce website how do you measure if a change to your site
I want to change member of structure under double pointer. Do you know how?
For those agile practitioners out there... How do you manage changes to a database
When you roll out changes to a live web site, how do you go
How do you test the usability of the user interfaces of your applications -
How do you create your own custom moniker (or URL Protocol) on Windows systems?
How do you restore a database backup using SQL Server 2005 over the network?

Trending Tags

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

Top Members

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.