yixuan
yixuan

Reputation: 3

how to invoke my jar file in one java process

I have execute jar file and I use java -jar xx.jar -file xx.file to execute it.Now, I need use that jar file for more than 56 times concurrent, so there should be more than 56 java.exe in task manager. But it will cost lots of resource for my PC, does there have method to launch only one java process and execute with different argument. Not sure if you can catch my point, if there has unclear, please let me know. jar file is not written by me, and i only have binary.

Any clue?

And one more question, what reason if there has more than 56 java process, will slow down my pc very very slow? Can I set jvm as low memory used or low cpu usage to fix it?

Thanks, Eugene

Upvotes: 0

Views: 543

Answers (3)

Adam
Adam

Reputation: 36723

This is possible from within one java app without using separate processes...

In this example I launch a separate executable jar file 10 times. I use the standard JarFile class to query the main class then execute it on a new thread. Also I use a separate class-loader for each instance. This is important as without it singletons would clash between the separate apps.

public static void main(String[] args) {

    for (int i = 0; i < 10; i++) {
        try {
            File file = new File("foo.jar");
            JarFile jar = new JarFile(file);
            String main = jar.getManifest().getMainAttributes()
                    .getValue(Name.MAIN_CLASS);

            URLClassLoader freshLoader = new URLClassLoader(
                    new URL[] { file.toURI().toURL() }, null);

            Class<?> classToLoad = Class.forName(main, true, freshLoader);
            Class<?>[] argTypes = new Class[] { String[].class };
            final String[] mainArgs = new String[] { "arg1", "arg2" };
            final Method method = classToLoad.getMethod("main", argTypes);

            Runnable job = new Runnable() {
                @Override
                public void run() {
                    try {
                        method.invoke(null, (Object) mainArgs);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            new Thread(job, "Thread :" + jarFile).start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Test app class (contained in executable jar file foo.jar) to simulate your 3rd party jar...

public class Main {

    private static int counter;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test frame");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(2, 1));
        frame.getContentPane().add(new JLabel("instance " + counter));
        frame.getContentPane().add(
                new JLabel("launched with " + Arrays.asList(args)));
        frame.pack();
        frame.setVisible(true);

        counter++;
    }

}

Upvotes: 3

tom
tom

Reputation: 2745

56 java processes will always slow down your pc. You can use -Xmx for max memory setting and -Xms for min memory setting on the command line.

Remark, I really do not think running 56 java processes is a good idea

Upvotes: 0

Glenn Bech
Glenn Bech

Reputation: 6182

You could use Threads in your application. That way, you have one Java process. This will again spawn 50-60 threads. You probably have to redesign your application a bit to do this. Take a look at this page for a start; http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html

Upvotes: 0

Related Questions