如何在IntelliJ中运行程序时启动终端或命令窗口

时间:2018-04-17 03:15:33

标签: macos intellij-idea

我正在编写一个命令行程序,并希望终端在我运行程序时自动打开,这可以在IntelliJ中实现吗?

1 个答案:

答案 0 :(得分:0)

是的,你可以。

使用此代码(您需要Project个实例,可以从ActionEvent.getProject获取):

GeneralCommandLine commandLine = new GeneralCommandLine([List of your command line arguments]);
OSProcessHandler handler = new OSProcessHandler(commandLine);
TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
ConsoleView console = consoleBuilder.getConsole();
Executor executor = DefaultRunExecutor.getRunExecutorInstance();
DefaultActionGroup actionGroup = new DefaultActionGroup();
BorderLayoutPanel component = JBUI.Panels.simplePanel();
component = component.addToCenter(console.getComponent());
ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("MainToolbar", actionGroup, false);
RunContentDescriptor descriptor = new RunContentDescriptor(console,
        handler,
        component.addToLeft(toolbar.getComponent()),
        [Your title],
        [Your icon]);
ExecutionManager manager = ExecutionManager.getInstance(project);
manager.getContentManager().showRunContent(executor, descriptor);
AnAction[] consoleActions = console.createConsoleActions();

for (AnAction action : consoleActions) actionGroup.add(action);

actionGroup.add(new PinActiveTabAction());
actionGroup.add(new CloseAction(executor, descriptor, project));
console.allowHeavyFilters();
console.attachToProcess(handler);
ProcessTerminatedListener.attach(handler);
handler.startNotify();