如何在 Linux 上从 java 文件中执行可执行文件?

发布于 2024-12-02 11:20:47 字数 1177 浏览 1 评论 0原文

我正在尝试从 java 程序中执行可执行文件和 perl 脚本。我发现了很多与此类似的主题,但其中大多数都涉及 Windows。我知道java是独立于平台的,无论如何它都应该工作,但事实并非如此。我已经尝试过的解决方案是基于 java 运行时及其 exec 方法的解决方案。它在 Windows 上运行得很好,但由于我要将程序移植到 Linux 上,所以我需要对其进行调整。正如我所说,我需要执行一个我已经编译并用 C++ 编写的可执行文件,它看起来正在工作,但它以退出值 1 完成执行。我不知道这意味着什么,但在 Windows 上它以 0 退出Linux 上也应该如此(?!?!)。另一方面,珍珠脚本根本不启动。我使用命令“perl script.pl”,它以 255 的值退出。不用说,它没有做它应该做的事情。

有人知道执行这些文件的另一种方法吗?或者我的实施可能哪里出了问题?

如果您想看一下,这是代码: 这是 perl 脚本的脚本

public static void main(String[] args){
    System.out.println("Starting");
    try{
        String[] cmd = {"perl", "cloc-1.53.pl"};
        Process pr = Runtime.getRuntime().exec(cmd);
        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exit code: " + exitVal);
    } catch (Throwable t){
        t.printStackTrace();
    }
}

对于编译文件,我将其更改

String[] cmd = {"perl", "cloc-1.53.pl"};

为:

String cmd = "./UCC";

I am trying to execute an executable file and a perl script from within a java program. I have found many topics similar to this but most of them refer to windows. I know java is platform independent and it should work anyways but it doesn't. The solution I have tried already is the one based on the java Runtime and it's exec method. It works just fine on windows but since I'm porting my program on linux I need to adapt it. As I said I need to execute an executable file that I have compiled and was written in c++ which it looks like it's working but it finishes executing with an exit value of 1. I have no idea what it means but on windows it exits with 0 and that's how it should be on linux as well (?!?!). The pearl script on the other hand does not start at all. I use the command "perl script.pl" and it exits with a value of 255. Needless to say, it doesn't do what it's supposed to.

Does anybody know another way to execute these files? Or maybe where I am wrong with my implementation?

here's the code if you want to take a look at it:
This is the one for the perl script

public static void main(String[] args){
    System.out.println("Starting");
    try{
        String[] cmd = {"perl", "cloc-1.53.pl"};
        Process pr = Runtime.getRuntime().exec(cmd);
        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exit code: " + exitVal);
    } catch (Throwable t){
        t.printStackTrace();
    }
}

For the compiled file I change this:

String[] cmd = {"perl", "cloc-1.53.pl"};

with:

String cmd = "./UCC";

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

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

发布评论

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

评论(1

苦行僧 2024-12-09 11:20:47

Windows 和 Linux 上启动进程应该没有区别。

好文章 http://www.javaworld.com/javaworld /jw-12-2000/jw-1229-traps.html
它采用旧方法,但提供了很好的洞察力。

文章转换为新方式:

从 Runtime.exec() 到 ProcessBuilder

There should be no differece in starting processes on windows and linux.

Good article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Its for the old way but gives good insight.

Article converting to the new way:

From Runtime.exec() to ProcessBuilder

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