gpt4 book ai didi

java - Runtime.exec() 给出错误 : Could not find or load main class

转载 作者:行者123 更新时间:2023-12-04 06:08:07 25 4
gpt4 key购买 nike

我的 Eclipse 项目中的“Street.class”位于包 trafficcircle 中的\bin 下。下面的错误来自创建进程的 stderror;我认为 Runtime.exec 如果找不到它会首先提示......这是怎么回事?

运行“街道”进程的代码:

    Process process = runtime.exec("java -classpath \\bin trafficcircle.Street 1 2");

“街道”在哪里:
public class Street {

/**
* @param args
* 0 - Simulation run time
* 1 - Flow time interval
*/
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.flush();
}
}

过程打印:

错误:无法找到或加载主类 trafficcircle.Street

进程退出值:1

是的,这适用于 cmd 行:

C:\Users\Brent>java -classpath "D:\Java Programs\IPCTrafficCircle\bin"trafficcircle.Street 1 2

最佳答案

此代码在文件夹 /bin 中运行时给出了预期的结果通过输入命令行 java Test .

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Test {
public static void main(String[] args) throws Exception {
Process process = Runtime.getRuntime().exec(
"java trafficcircle.Street 1 2");

BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}

但是,它在 Eclipse 中运行时不会给出任何结果。为了得到结果,我必须设置类路径。
"java -cp /Users/wannik/Java/Workspace/MyProject/bin trafficcircle.Street 1 2");

关于java - Runtime.exec() 给出错误 : Could not find or load main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com