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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:19:23+00:00 2026-05-13T13:19:23+00:00

I’d like to write a Java app which can create executable jars at runtime.

  • 0

I’d like to write a Java app which can create executable jars at runtime. The “hello world” of what I want to do is write a Java app X that when run, generates an executable jar Y that when run, prints hello world (or perhaps another string not known until after Y is run).

How can I accomplish this?

  • 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-13T13:19:24+00:00Added an answer on May 13, 2026 at 1:19 pm

    The other answers require starting a new process, this is a method that doesn’t. Here are 3 class definitions which produce the hello world scenario described in the question.

    When you run XMain.main, it generates /tmp/y.jar. Then, when you run this at the command line:

    java -jar /tmp/y.jar cool
    

    It prints:

    Hello darling Y!
    cool
    

    example/YMain.java

    package example;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class YMain {
    
        public static void main(String[] args) throws IOException {
            // Fetch and print message from X
            InputStream fromx = YMain.class.getClassLoader().getResourceAsStream("fromx.txt");
            System.out.println(new String(Util.toByteArray(fromx)));
    
            // Print first command line argument
            System.out.println(args[0]);
        }
    }
    

    example/XMain.java

    package example;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.jar.Attributes;
    import java.util.jar.JarEntry;
    import java.util.jar.JarOutputStream;
    import java.util.jar.Manifest;
    
    public class XMain {
    
        public static void main(String[] args) throws IOException {
            Manifest manifest = new Manifest();
            manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
            manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, YMain.class.getName());
            JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream("/tmp/y.jar"), manifest);
    
            // Add the main class
            addClass(YMain.class, jarOutputStream);
    
            // Add the Util class; Y uses it to read our secret message
            addClass(Util.class, jarOutputStream);
    
            // Add a secret message
            jarOutputStream.putNextEntry(new JarEntry("fromx.txt"));
            jarOutputStream.write("Hello darling Y!".getBytes());
            jarOutputStream.closeEntry();
    
            jarOutputStream.close();
        }
    
        private static void addClass(Class c, JarOutputStream jarOutputStream) throws IOException
        {
            String path = c.getName().replace('.', '/') + ".class";
            jarOutputStream.putNextEntry(new JarEntry(path));
            jarOutputStream.write(Util.toByteArray(c.getClassLoader().getResourceAsStream(path)));
            jarOutputStream.closeEntry();
        }
    }
    

    example/Util.java

    package example;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class Util {
    
        public static byte[] toByteArray(InputStream in) throws IOException {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[0x1000];
            while (true) {
                int r = in.read(buf);
                if (r == -1) {
                    break;
                }
                out.write(buf, 0, r);
            }
            return out.toByteArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small

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.