在另一个进程(VM)中启动Java main

发布于 2024-09-16 11:38:41 字数 428 浏览 4 评论 0原文

我有一个服务器客户端项目,出于测试目的,我想在一个全新的进程中启动服务器。问题是,我的项目中只有一个 main() 方法,没有 jar。所以我的猜测是这样的

Runtime.getRuntime().exec("javac MyServer.java");
Runtime.getRuntime().exec("java class MyServer");

,但我真的不确定它,而且我也不完全喜欢为每个测试用例启动 javac 的需要。

我应该如何进行?

编辑

我想在@Before方法中启动该进程并在@After中销毁它。它必须自动运行,因此手动打开服务器不是一个选择。我正在寻找一种消除服务器类编译的方法。但现在我想也没有别的办法了。

I have a server client project and for testing purposes I want to start the server in a whole new process. The problem is, I have just a main() method in project, no jar. So my guess would be something like

Runtime.getRuntime().exec("javac MyServer.java");
Runtime.getRuntime().exec("java class MyServer");

But I am really not sure about it and also I don't exactly like the need to start javac for each test case.

How should I proceed?

EDIT

I want to start the process in the @Before method and destroy it in @After. It has to run automatically, so manual turning on the server is not an option. What I was looking for is a way to eliminate compilation of the server class. But now I guess there is no other way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪了杯 2024-09-23 11:38:41

如果这是出于测试目的,只需从命令行启动其他进程或使用 Eclipse 来处理它。在 Eclipse 中,同一个项目可以有多个 main() 入口点。当您希望运行应用程序时,您可以创建一个运行/调试配置,说明您希望调用哪个入口点。因此,您可以为客户端定义一个,为服务器定义一个,然后单击按钮即可运行它们。

扩展:

先决条件 - 在执行任何操作之前,首先将项目导入 Eclipse。

  1. 从 Java 角度运行 Eclipse(Java 项目通常是这种情况)
  2. 您将看到两个标记为“调试为...”和“运行为...”的工具栏按钮 从现在开始我将描述“调试”按钮,但同样的原理也适用运行方式...
  3. 调试方式...按钮旁边是一个下拉按钮。单击它,然后从下拉列表中选择调试配置...
  4. 将打开一个配置对话框。在对话框中双击“Java 应用程序”。 Eclipse 将创建一个新的调试配置来调试 Java 应用程序。
  5. 使用右侧的字段选择您正在调试的 Eclipse 项目、主类(即具有您希望调用的静态主类的类)以及您想要设置的任何其他参数。您可以为您的配置指定一个有意义的名称并应用更改。
  6. 创建两种配置,一种用于客户端,一种用于服务器,例如“调试客户端”和“调试服务器”
  7. 退出对话框
  8. 现在“调试为...”下拉列表包含新配置的两个操作。您可以单击它们以在调试模式下启动应用程序。您将观察到每个实例都在单独的 java.exe 进程中运行。

Eclipse 还有一个调试视角,您可以在其中停止/暂停正在运行的进程、设置断点等等。它甚至可以同时调试多个事物,因此您可以在调试中启动客户端和服务器,并在调用的任一侧设置断点。

If this is for testing purposes, just launch the other process from the command line or use Eclipse to take care of it. In Eclipse the same project can have multiple main() entry points. When you wish to run the app you create a run / debug configuration that says which entry point you wish to invoke. So you could define one for the client and one for the server and run them with a button click.

Expanded:

Prerequisite - import your project into Eclipse first before doing any of this.

  1. Run Eclipse from the Java perspective (which is normally the case for a Java project)
  2. You will see two toolbar buttons marked Debug As... and Run As.... I will describe the Debug button from now on but the same principle applies to Run As..
  3. Next to the Debug As... button is a drop down button. Click it and from the drop down choose Debug Configurations...
  4. A configuration dialog will open. In the Dialog double click on "Java Application". Eclipse will create a new debug configuration for debugging a Java application.
  5. Use the fields on the right to choose which Eclipse project you are debugging, the main class (i.e. the one with the static main you wish to invoke) and any other args you want to set. You can give your configuration a meaningful name and apply the changes.
  6. Create two configurations one for your client and one for your server, e.g. "debug client" and "debug server"
  7. Exit the dialog
  8. Now the Debug As... drop down contains two actions with your new configurations. You can click them to launch your apps in debug mode. You will observe that each instance is running in a separate java.exe process.

Eclipse also has a debug perspective where you can stop / pause running processes, set breakpoints and whatnot. It can even debug more than one thing simultaneously so you could launch both client and server in debug and set breakpoints either side of the call.

撧情箌佬 2024-09-23 11:38:41

Java 允许您像这样进行系统调用。

 Runtime r = Runtime.getRuntime();
 Process p = r.exec("java otherMain arg0 arg1");

这将允许您启动另一个 java 进程。如果需要,进程还具有获取该进程的输出的方法。

Java allows you to make system calls like so.

 Runtime r = Runtime.getRuntime();
 Process p = r.exec("java otherMain arg0 arg1");

This would allow you to start another java process. Process also has methods to get the output of that process if you need it.

鹿港巷口少年归 2024-09-23 11:38:41

看看 JUnit,我认为你想要做的是针对你的客户端运行一系列测试,服务器正在运行。您可以通过编写一个 JUnit 测试来完成此任务,该测试使用 @Before 注释在每次测试之前运行设置方法。例如:

import org.junit.Before

public class TestClient {
    private static final int port = 20000 + new Random().nextInt(10000);
    private MyServer server;

    @Before
    public void setup() {
        // setup server
        server = new MyServer(port);
        server.start();
    }

    @Test
    public void testX() {
        // test x
    }

    @Test
    public void testY() {
        // test y
    }
}

Take a look at JUnit, what I think you want to do is run a series of tests against your client, with a server running. You can accomplish this by writing a JUnit test which makes use of the @Before annotation to run a setup method before each test. For example:

import org.junit.Before

public class TestClient {
    private static final int port = 20000 + new Random().nextInt(10000);
    private MyServer server;

    @Before
    public void setup() {
        // setup server
        server = new MyServer(port);
        server.start();
    }

    @Test
    public void testX() {
        // test x
    }

    @Test
    public void testY() {
        // test y
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文