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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:08:40+00:00 2026-06-16T16:08:40+00:00

I am trying to develop a script in Java which finds all .jar files

  • 0

I am trying to develop a script in Java which finds all .jar files in a specified directory, then them to classpath and under certain conditions, invokes their main() method. Here is my Java info:

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)

Here is the ls of the current working dir:

clojure.jar 
loader.class
loader.java 

I am doing the following in order to add clojure.jar to the classpath and invoke its main method:

import java.io.File;

import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;

import java.lang.reflect.Method;

public final class loader {

  public static void main (String[] args) {
    try {
      printClasspathString();
      System.out.println ("**********");
      URL[] classesRepo = { new File("clojure.jar").toURI ().toURL (),
                            new File(System.getProperty("user.dir")).toURI ().toURL ()};
      ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
      URLClassLoader urlClassLoader = new URLClassLoader( classesRepo, currentThreadClassLoader);
      Thread.currentThread().setContextClassLoader(urlClassLoader);
      printClasspathString();
    } catch (Exception ex) {
      System.out.println(ex.getMessage ());
    }

    //Do I miss something here?

    String mainClassName="clojure.main";

    Class<?> mainClass = null;
    try {
      mainClass = Class.forName(mainClassName);
    }
    catch (Exception ex) {
      throw new IllegalArgumentException("class not found in your jar file " + mainClassName);
    }

    Method mainMethod = null;
    try {
      mainMethod = mainClass.getMethod("main", String[].class);
    }
    catch (Exception ex) {
      throw new IllegalArgumentException("class to launch (" + mainClassName + ") does not have a public static void main(String[]) method");
    }

    try {
      mainMethod.invoke(null, (Object) args);
    } catch (Exception ex) {
      System.out.println(ex.getMessage ());
    }

  }

  public static void printClasspathString() {
    ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader();
    if (applicationClassLoader == null) {
      applicationClassLoader = ClassLoader.getSystemClassLoader();
    }
    URL[] urls = ((URLClassLoader)applicationClassLoader).getURLs();
    for(int i=0; i < urls.length; i++) {
      System.out.println (urls[i].getFile());
    }
  }

}

Unfortunately, the loader doesn’t work as expected:

$ java -cp . loader
/home/proofit404/data/downloads/clojure-loader/
**********
/home/proofit404/data/downloads/clojure-loader/clojure.jar
/home/proofit404/data/downloads/clojure-loader/
Exception in thread "main" java.lang.IllegalArgumentException: class not found in your jar file clojure.main
    at loader.main(loader.java:37)

If I use the -cp option, though, everything works fine:

$ java -cp .:clojure.jar loader
/home/proofit404/data/downloads/clojure-loader/
/home/proofit404/data/downloads/clojure-loader/clojure.jar
**********
/home/proofit404/data/downloads/clojure-loader/clojure.jar
/home/proofit404/data/downloads/clojure-loader/
Clojure 1.4.0
user=> (System/exit 0)

So – what is it that I need to change in my code to make it work as expected?

  • 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-16T16:08:41+00:00Added an answer on June 16, 2026 at 4:08 pm

    I think the problem is that the Class.forName(String) method does not use the threads contextclassloader, but the classloader of the current class:

    public static Class<?> forName(String className)
                        throws ClassNotFoundException
    
    Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
    
        Class.forName(className, true, currentLoader)
    
    where currentLoader denotes the defining class loader of the current class.
    

    This means your URLClassLoader wont be used. Try instead to explicitly pass the classloader by using Class.forName(String,boolean, ClassLoader):

    mainClass = Class.forName(mainClassName, true, urlClassLoader);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to develop a script where all users can upload an image
I'm trying to develop a script which will, in part, generate a list of
I'm trying to develop a perl script that looks through all of the user's
I'm trying to develop a bash build script for a Java project that will
I'm trying to develop a script which compares a runtime generated string against one
I am trying to develop a PHP script that replaces all divs in an
I am trying to develop a script in Python which would function like the
I am trying to develop an application for my localhost on which I can
I was trying to develop a video management software for which I was evaluating
I am trying to develop the XMPP Gateway which can send/receive from standard XMPP

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.