Java run jar from another jar with passing arguments

Viewed 3705

I have generated run1.jar:

   package runner;

    import java.io.IOException;
    import java.io.PrintWriter;

    public class Run1 {
        public static void main(String[] args) {

            int i = Integer.parseInt(args[0]);
            System.out.println(i);

            try {
                PrintWriter writer = new PrintWriter("1.txt", "UTF-8");
                writer.println(i);
                writer.println(i);
                writer.close();
            } catch (IOException e) {
                // do something
            }

        }
    }

Also I have generated run2.jar:

package runner;

import java.io.File;
import java.io.IOException;

public class Run2 {

    public static void main(String[] args) throws IOException, InterruptedException {

        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\\test2\\run1.jar", "1");
        pb.directory(new File("C:\\"));
        try {
            Process p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

I have ran with this command java -jar run2.jar 1 and work it. Expecting result is running with this command: java -jar run2.jar who doesn't work.

How can I run run2.jar with passing parameters from inside main method in run2.jar?

Error is:

C:\test2>java -jar run2.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at runner.Run1.main(Run1.java:9)
2 Answers
Related