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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:33:13+00:00 2026-05-30T19:33:13+00:00

Possible Duplicate: Execute another jar in a java program Basically I want to run

  • 0

Possible Duplicate:
Execute another jar in a java program

Basically I want to run an external .jar from the one I’m working on now.

I.e. I want to run foo.jar from bar.jar

I’ve tried using Runtime and Process to execute “java -jar foo.jar”, but it opens foo.jar and then it closes immediately. Any tips?

  • 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-30T19:33:14+00:00Added an answer on May 30, 2026 at 7:33 pm

    The easiest solution (as Thorn pointed out) would be to have the jar as a build-time dependency and invoke it statically from your code:

    ExternalJarMainClass.main(new String[]{"arguments", "to", "main"});
    

    But if that is not possible, you can use a URLClassLoader to load the jar dynamically. If the jar is indeed runnable, then you can read the main class from META-INF/MANIFEST.MF and invoke main via reflection.

    This is a different approach from creating a separate process, as the external code will run in the same process as your application. Perhaps this is desirable, perhaps not – that depends on the situation.

    Below’s a (hastily written and flawed) sample helper class that does just that.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class JarRunner {
    
        private final Method entryPoint;
    
        public JarRunner(File jarFile) throws
                ClassNotFoundException,
                IOException,
                NoSuchMethodException {
            URL jarUrl = jarFile.toURI().toURL();
            URLClassLoader loader = URLClassLoader.newInstance(
                    new URL[]{jarUrl});
            URL manifestUrl = loader.findResource("META-INF/MANIFEST.MF");
            String manifest = resourceToString(manifestUrl);
            Class<?> clazz = loader.loadClass(findMainClassName(manifest));
            entryPoint = clazz.getMethod("main", String[].class);
        }
    
        public void run(String[] argsToMain) throws
                IllegalAccessException,
                IllegalArgumentException,
                InvocationTargetException {
            entryPoint.invoke(null, (Object) argsToMain);
        }
    
        private static String resourceToString(URL url) throws IOException {
            InputStream contentStream = url.openStream();
            try {
                BufferedReader r = new BufferedReader(
                        new InputStreamReader(contentStream));
                StringBuilder sb = new StringBuilder();
                String line = null;
                do {
                    line = r.readLine();
                    if (line != null) {
                        sb.append(line).append('\n');
                    }
                } while (line != null);
                return sb.toString();
            } finally {
                contentStream.close();
            }
        }
    
        private static String findMainClassName(String manifest) {
            Matcher m = MAIN_CLASS_PATTERN.matcher(manifest);
            if (m.find()) {
                return m.group(1);
            }
            return null;
        }
    
        private static final Pattern MAIN_CLASS_PATTERN = 
                Pattern.compile("Main-Class: (.+)");
    }
    

    Sample usage:

    JarRunner jr = new JarRunner(new File("path/to/MyJar.jar"));
    jr.run(new String[]{"arg1", "arg2"});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Executing another java program from our java program I wanted to execute
Possible Duplicate: Execute a Java program from our Java program I wanted to execute
Possible Duplicate: How to execute sql statements from a C program? I'm creating a
Possible Duplicate: C# Execute function at specific time I want to run certain function
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: How to call one batch file after another I have a batch
Possible Duplicate: Java execute a command with a space in the pathname I am
Possible Duplicate: Best way to periodically execute a PHP script? I need to run
Possible Duplicate: Java REPL shell Hey, Is there any way to execute Java code
Possible Duplicate: Request UAC elevation from within a Python script? I'd like to execute

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.