gpt4 book ai didi

java - 在eclipse插件中启动java程序失败

转载 作者:行者123 更新时间:2023-12-01 05:53:50 26 4
gpt4 key购买 nike

我尝试在 Eclipse 插件中启动 java 程序,但失败了。

我尝试做的是让“Contribution to eclipse”中的示例发挥作用。
在示例中,插件向 eclipse 贡献了一个菜单项,
当您选择一个测试类时,您可以使用菜单来运行该类中的测试用例。

为了运行测试用例,有两个类TestRunnerSocketTestRunner
TestRunnerSocketTestRunner 作为单独的 java 程序启动,并进行通信
通过套接字。

与启动java程序相关的TestRunner代码:

final String MAIN_CLASS = "org.eclipse.contribution.junit.SocketTestRunner";
public TestRunner() {
}

public void run(IType type) throws CoreException {
project = type.getJavaProject();
run(new IType[] { type });
}

public void run(IType[] classes) throws CoreException {
IVMInstall vmInstall = JavaRuntime.getVMInstall(project);
if (vmInstall == null)
vmInstall = JavaRuntime.getDefaultVMInstall();
if (vmInstall == null)
return;

IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
if (vmRunner == null)
return;

String[] classPath = computeClasspath();

// prepare arguments:
// Argument[0] = port
// Argument[1-n] = types
port = SocketUtil.findFreePort();
String[] args = new String[classes.length + 1];
args[0] = Integer.toString(port);
for (int i = 0; i < classes.length; i++) {
args[1 + i] = classes[i].getFullyQualifiedName();
}
VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(MAIN_CLASS,
classPath);
vmConfig.setProgramArguments(args);

ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
vmRunner.run(vmConfig, launch, null);

connect();
}

SocketTestRunner的代码:

public static void main(String[] args) {
try {
logger = new PrintWriter(new FileWriter("log.txt"), true);

logger.println("main");
for (int i = 0; i < args.length; i++) {
logger.println(args[i]);
}

new SocketTestRunner().runTests(args);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (logger != null) {
logger.close();
}
}
}

private void runTests(String[] args) {
port = Integer.parseInt(args[0]);
logger.println("port: " + port);
openClientSocket();
try {
logger.println("Befor new TestSuite");
TestSuite suite = new TestSuite();
for (int i = 1; i < args.length; i++) {
logger.println("add TestSuite");
suite.addTestSuite(Class.forName(args[i]).asSubclass(TestCase.class));
}

logger.println("starting tests " + suite.countTestCases());
//writer.println("starting tests " + suite.countTestCases());

logger.println("Before TestResult");
TestResult result = new TestResult();
logger.println("After TestResult");
result.addListener(this);
suite.run(result);
//writer.println("ending tests ");
logger.println("ending tests ");
} catch (Exception e) {
logger.println("exception: " + e.getMessage());
logger.println(e.toString());
e.printStackTrace(logger);
e.printStackTrace();
} finally {
closeClientSocket();
}
}

private void closeClientSocket() {
logger.println("closeClientSocket");
if (writer != null) {
writer.close();
}

try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private void openClientSocket() {
logger.println("openClientSocket");
try {
socket = new Socket("localhost", port);
writer = new PrintWriter(socket.getOutputStream(), true);
return;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

当我在 eclipse(3.6.0 HELIOS) 中测试时,我在运行时 eclipse 中创建一个简单的 JUnit 测试用例,
选择测试类并单击上下文菜单中的菜单项,
那么运行时 Eclipse 将不会响应任何进一步的交互,
让 ServerSocket 仍然保持监听,并且我不添加用于调试的测试日志。

我可以获得 eclipse 用于启动 SocketTestRunner 的命令行:

"C:\Program Files\Java\jre6\bin\javaw.exe" -classpath C:\Users\User\workspace\org.eclipse.contribution.junit\bin;C:\Users\User\workspace\org.eclipse.contribution.junit\plugins\org.eclipse.contribution.junit_1.0.0.201009102354.jar;C:\Users\User\runtime-EclipseApplication\Hello\bin;C:\Users\User\workspace\org.junit\junit.jar;"C:\Program Files\eclipse\plugins\org.hamcrest.core_1.1.0.v20090501071000.jar" org.eclipse.contribution.junit.SocketTestRunner 41872 org.eclipse.contribution.hello.HelloTest

当我手动使用此命令时,我可以成功运行测试用例(在注释与套接字相关的writer之后),因为我可以获得测试日志。

main
41872
org.eclipse.contribution.hello.HelloTest
port: 41872
openClientSocket
Befor new TestSuite
add TestSuite
starting tests 1
Before TestResult
After TestResult
starting test testSayHi(org.eclipse.contribution.hello.HelloTest)
failing test testSayHi(org.eclipse.contribution.hello.HelloTest)
END TRACE
ending tests
closeClientSocket

如有任何建议,我们将不胜感激,谢谢。

终于让这个插件运行了。
您应该设置工作目录。

vmConfig.setWorkingDirectory("C:\\Users\\User");

当我尝试获取工作目录时,我得到null

不能在调用 connect() 函数的行设置断点。

private void connect() {
try {
ServerSocket server = new ServerSocket(port);
try {
Socket socket = server.accept();
try {
readMessage(socket);
} finally {
socket.close();
}
} finally {
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

但我不知道为什么,任何建议将不胜感激。

最佳答案

可以,但是问题出现在调用 connect 方法之前。因此,在 connect 之前调用插件钩子(Hook),等待 SocketTestRunner 启动。

关于java - 在eclipse插件中启动java程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724367/

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