使用 Runtime.getRuntime().exec() 从 Java Swing 应用程序启动 jEdit

发布于 2024-08-11 04:39:17 字数 590 浏览 2 评论 0原文

我正在编写一个在 Red Hat Enterprise Linux 5 服务器上运行的 Java Swing 应用程序,我想启动 jEdit 来查看日志文件。

这是一些示例代码。

public static void main(String[] args) throws IOException, InterruptedException {

    String cmd = "sh -c \"java -jar /tmp/jEdit/jedit.jar /tmp/test.txt\"";

    System.out.println(cmd);

    Runtime.getRuntime().exec(cmd);

}

输出为:

sh -c "java -jar /tmp/jEdit/jedit.jar /tmp/test.txt"

如果我​​将 cmd 输出复制并粘贴到终端窗口中,它运行正常。

我尝试了一堆 cmd 值,但我永远无法让 jEdit 窗口可见。

经过更改后,此过程在 Windows 上运行良好。

我所做的事情在 Linux 上可行吗?

提前致谢!

I'm writing a Java Swing Application running on Red Hat Enterprise Linux 5 server that I would like to launch jEdit to view log files.

Here is some example code.

public static void main(String[] args) throws IOException, InterruptedException {

    String cmd = "sh -c \"java -jar /tmp/jEdit/jedit.jar /tmp/test.txt\"";

    System.out.println(cmd);

    Runtime.getRuntime().exec(cmd);

}

The output is:

sh -c "java -jar /tmp/jEdit/jedit.jar /tmp/test.txt"

If I copy and paste the cmd output in a terminal window, it runs fine.

I have tried a bunch of cmd values, but I can never get the jEdit window to be visible.

With changes, this process works fine on Windows.

Is what I'm doing possible on Linux?

Thanks in advance!

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

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

发布评论

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

评论(5

如果没有你 2024-08-18 04:39:17

由于 jEdit 是用 Java 实现的,因此检查 main 方法(在 jedit.jar 中包含的清单文件中声明的类中)的功能并执行相同的操作可能会更容易根本不使用 Runtime.getRuntime().exec() 。

如果您确实想坚持使用它,您可以尝试将各个命令作为数组传递给 exec(),这通常为我解决了此类问题。

As jEdit is implemented in Java, perhaps it would be easier to check the source for what the main method (in the class declared in the manifest file included in the jedit.jar) does and do the same thing without using Runtime.getRuntime().exec() at all.

If you do want to stick with it, you could try passing the individual commands as an array to exec(), this often solved such problems for me.

清旖 2024-08-18 04:39:17

Linux 在其 X-Windows 系统中使用显示端口的概念。这使得它可以为每个用户维护不同的桌面环境。它还允许远程计算机上的用户从第一台计算机运行桌面应用程序,但可以看到远程计算机上的 UI。

Windows 一次只有一个可用的桌面环境,但事实并非如此。

您必须做的第一件事就是将环境变量“DISPLAY=localhost:0”添加到您启动它的环境中。但是,您可能还需要运行“xhost +localhost”,否则可能不允许这样做。

还要仔细检查您是否没有成功启动一堆现在是僵尸的 jEdit 进程(使用 top),并在必要时杀死它们(使用kill)。

Linux uses the concept of display ports for its X-Windows system. This allows it to maintain a different desktop environment for each user. It also allows a user on remote machine to run a desktop app from the first machine but see the UI on the remote.

Windows, having only one available desktop environment at a time, does not.

First thing you definitely have to do is add the environment variable "DISPLAY=localhost:0" to the environment from which you are launching this. However, you may also need to run 'xhost +localhost' or this may not be allowed.

Double-check, too, that you didn't successfully launch a bunch of jEdit processes that are now zombies (using top) and kill them if necessary (using kill).

欢你一世 2024-08-18 04:39:17

Runtime.exec() 需要特别注意。接受 String 的 exec 方法使用空格字符作为分隔符将字符串分解为命令。您需要使用接受 String[] 的 exec 方法。阅读此处了解更多信息,特别是在底部附近。

Runtime.exec() needs some special attention. The exec method that accepts a String uses the space character as a delimiter to break up the string into commands. You need to use the exec method that accepts a String[]. Read more here, specifically near the bottom.

紧拥背影 2024-08-18 04:39:17

我已经这样做过一次,也遇到了同样的问题

我所做的是将命令行写入文本文件,然后将文本文件作为 shell 脚本文件执行。
它对我来说效果很好。

I´ve done this once and I got the same problem

What I've done is to write the command line into a text file and then execute the text file as a shell script file.
It worked fine for me.

狼亦尘 2024-08-18 04:39:17

Jedit 有一个启动器脚本,我猜是 /usr/bin/jedit 。只需在命令提示符中输入 jedit 即可运行它,至少在当前版本 4.5 中是这样。尝试使用该脚本而不是显式的 java 命令。

Jedit has a launcher script, /usr/bin/jedit I guess. Simply typing jedit in command prompt runs it, at least in current version, 4.5. Try that script instead of explicit java command.

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