[英]How to call a Java Class with parameters that is in another Jar, multiple times in Parallel
I have a project in RAD. 我在RAD中有一个项目。 Package is inputFileEdit, and the java class I need is InputFileTest.java. 包是inputFileEdit,我需要的java类是InputFileTest.java。
package inputFileEdit;
public class InputFileTest {
public static void main(String[] args) {
String var1 = args[0];
String var2 = args[1].toLowerCase();
// do stuff with args
}
}
I want to create a new package / java program that can call or instantiate the class InputFileTest above, with arguments, multiple times in parallel. 我想创建一个新的程序包/ java程序,该程序可以并行调用或实例化带有参数的上述InputFileTest类。 I'm basically going to be bringing back a String list, looping through that list to create parallel threads, each row on the list calling InputFileTest. 我基本上将要带回一个String列表,遍历该列表以创建并行线程,列表中的每一行都调用InputFileTest。
Question 1) What's the best way to call InputFileTest? 问题1)调用InputFileTest的最佳方法是什么? I'm using RAD and I created a new Project, a package called CallerPackage, and a Caller.java inside that package? 我正在使用RAD,并且创建了一个新项目,一个名为CallerPackage的程序包以及该程序包中的Caller.java? I also including a "Jar" of the whole InputFileEdit project under /lib via Java Build Path -> Libraries -> Add External Jars. 我还通过Java Build Path-> Libraries-> Add External Jars在/ lib下包括整个InputFileEdit项目的“ Jar”。 I don't know how to call the class with parameters (I tried something like InputFileEdit ifeObj = new InputFileEdit("parm 1", "parm 2");
or InputFileEdit ifeObj = new InputFileEdit("parm 1 parm 2");
) but neither worked so then I tried to just call the jar like Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2");
我不知道如何用参数调用类(我尝试过类似InputFileEdit ifeObj = new InputFileEdit("parm 1", "parm 2");
或InputFileEdit ifeObj = new InputFileEdit("parm 1 parm 2");
)但是两者都没有起作用,于是我尝试像Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2");
一样调用jar Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2");
or since I want the actual Class InputFileTest, Process p = Runtime.getRuntime().exec(new String[]{"java","-cp","/lib/InputFileEdit.jar", "InputFileTest", "parm1","parm1"});
或因为我想要实际的类InputFileTest,所以Process p = Runtime.getRuntime().exec(new String[]{"java","-cp","/lib/InputFileEdit.jar", "InputFileTest", "parm1","parm1"});
: :
package CallerPackage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
//import inputFileEdit.*;
public class Caller {
static int i = 0;
public static void main(String args[]) throws IOException, InterruptedException {
try {
System.out.println("Calling jar");
Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2");
BufferedInputStream errStrm = new BufferedInputStream(p.getErrorStream());
// get the error stream of the process and print it
for (int i = 0; i < errStrm.available(); i++) {
System.out.println("" + errStrm.read());
}
System.out.println("Called jar");
p.destroy();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
but this doesn't seem to work either or print out anything helpful. 但这似乎也不起作用或打印出任何有用的内容。 Any ideas of the best way to go about this? 有什么最好的解决方案的想法吗? I'm only trying to get 1 call to work for now before I loop through my list and call them in parallel. 在遍历列表并并行调用它们之前,我只想暂时让1个电话上班。 Eventually it'll be calling the jar/class looping through a string arraylist. 最终它将通过字符串arraylist调用jar / class循环。
you should be able to call the main method in InputFileTest
the same way you would call any other static method: InputFileTest.main(args);
您应该能够像调用其他任何静态方法一样调用InputFileTest
的main方法: InputFileTest.main(args);
However, generally directly calling main is frowned upon. 但是,一般直接调用main是不满意的。 If you are able, I would advise you to take the code in InputFileTest's main, and put it into another, more descriptively named method, something like processFiles
. 如果可以的话,我建议您使用InputFileTest主程序中的代码,并将其放入另一个更具描述性的方法中,例如processFiles
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.