如何执行';诺胡普';通过JAVA命令
我需要在后台并行执行15到20个java程序。我们可以使用如何执行';诺胡普';通过JAVA命令,java,Java,我需要在后台并行执行15到20个java程序。我们可以使用nohup命令并直接执行它,但我想从另一个java程序执行这个nohup命令。我对它做了一些研究,并创建了一个java程序。但是当我使用java命令时,下面的java程序正在工作,但是如果我使用nohup则它失败了 public static void main(String[] args) throws Exception { String sourceFileName = args[0]; String targetF
nohup
命令并直接执行它,但我想从另一个java
程序执行这个nohup
命令。我对它做了一些研究,并创建了一个java
程序。但是当我使用java
命令时,下面的java
程序正在工作,但是如果我使用nohup
则它失败了
public static void main(String[] args) throws Exception {
String sourceFileName = args[0];
String targetFileName = args[1];
String type = args[2];
String logFileName = args[3];
Integer count = new Integer(args[4]);
// Below command is working
String command = "java -cp \"test-jar.jar\" com.test.MyTestClass ";
// Below command is not working
// String command = "nohup java -cp \"test-jar.jar\" com.test.MyTestClass ";
for(int i=1; i<=count; i++){
String cmd = command+sourceFileName+"_"+i+" "+targetFileName+"_"+i+" "+type+" 2>>"+logFileName+"_"+i+".log &";
runProcess(cmd);
}
}
private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}
我将
test jar.jar
保存在执行该程序的同一路径中。问题是因为有两个独立的项目。
我的类在
mytest
项目中,我要执行的类在testjar
项目中。我在“我的测试”项目中添加了testjar
依赖项。仍然面临同样的问题。我在测试jar
文件中添加/复制了我的类,然后执行了它。现在它同时适用于java
和nohup
命令问题是因为有两个独立的项目。
我的类在
mytest
项目中,我要执行的类在testjar
项目中。我在“我的测试”项目中添加了testjar
依赖项。仍然面临同样的问题。我在测试jar
文件中添加/复制了我的类,然后执行了它。现在它同时适用于java
和nohup
命令当它失败时会出现什么错误?更新了输出的问题一切正常。运行每个命令时,您应该尝试找出错误所在。为此,您应该尝试读取pro.getErrorStream()
的内容,并将其放入字符串中,然后将其写入控制台,而不是打印pro.exitValue()
。它包含该命令的实际错误消息$PipeInputStream@7cf10a6f您需要String命令=“/bin/sh nohup java…”代码>nohup
不是可执行文件,它是一个shell命令。您还应该考虑使用Main Class
条目提供清单,并使用java-jar
而不是您正在执行的操作。失败时会出现什么错误?使用outputEverything更新问题一切正常。运行每个命令时,您应该尝试找出错误所在。为此,您应该尝试读取pro.getErrorStream()
的内容,并将其放入字符串中,然后将其写入控制台,而不是打印pro.exitValue()
。它包含该命令的实际错误消息$PipeInputStream@7cf10a6f
您需要String命令=“/bin/sh nohup java…”代码>nohup
不是可执行文件,它是一个shell命令。您还应该考虑使用Main类
条目提供清单,并使用java-jar
代替您正在做的事情。
nohup java -cp "test-jar.jar" com.test.MyTestClass source-file_1 target-file_1 TEST 2>>log-file_1.log & exitValue() 1
nohup java -cp "test-jar.jar" com.test.MyTestClass source-file_2 target-file_2 TEST 2>>log-file_2.log & exitValue() 1
nohup java -cp "test-jar.jar" com.test.MyTestClass source-file_3 target-file_3 TEST 2>>log-file_3.log & exitValue() 1