Reputation: 263
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)
Upvotes: 4
Views: 4377
Reputation: 881
Here we are , below two different example which answer to your question
Process proc = Runtime.getRuntime().exec("java -jar C:\\...\\Run1.jar 1");
proc.waitFor();
//2 inputstream for the result and for the errors in subprogram
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
byte b[]=new byte[in.available()];
in.read(b,0,b.length);
System.out.println(new String(b));
byte c[]=new byte[err.available()];
err.read(c,0,c.length);
System.out.println(new String(c));
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\\...\\Run1.jar", "1");
Process proc2 = pb.start();
proc2.waitFor();
InputStream in2 = proc2.getInputStream();
InputStream err2 = proc2.getErrorStream();
byte b2[]=new byte[in.available()];
in.read(b,0,b.length);
System.out.println(new String(b));
byte c2[]=new byte[err.available()];
err.read(c,0,c.length);
System.out.println(new String(c));
Output from IDE (Eclipse)
Output from command line :
Note: the "ok" is a my System.out.println i put in Run1.jar ( i saved the files jar with capital letter , but you can use yours names )
Upvotes: 2
Reputation: 14853
The error I have added to your question is relative to a missing argument: args[0]
, line 9 throw ArrayIndexOutOfBoundsException
.
The argument "1" isn't passed to run1.jar
The exception stack shows a jar-in-jar loader usage, why?
Build the jars with Oracle command line utilities or ANT and test again.
Upvotes: 0