Java Swing 执行 gFortran 代码

发布于 2024-11-16 06:05:51 字数 86 浏览 1 评论 0原文

我想知道 Java / Swing 代码是否可以在 ubuntu/linux 平台上执行 gFortran 程序?

有人知道如何做到这一点吗?

I was wondering is there anyway for Java / Swing code to execute gFortran program on ubuntu/linux platform?

Anyone has some idea on how this could be done ?

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

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

发布评论

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

评论(1

一个人的旅程 2024-11-23 06:05:51

您可以做的一件事是使用 Runtime 运行单独的程序 启动 进程 组成你的 Fortran 代码。以下是一个示例:

Runtime rt = Runtime.getRuntime();

try {
  String[] env = {"/path/to/program"};
  Process proc = rt.exec("your_program", env);
  System.out.println("return value: " + proc.waitFor());
}
catch (Exception ex) {
  System.err.println(ex);
}

上面的代码将执行 /path/to/program/your_program 并等待其完成,然后读取返回代码。

或者,您可以将一些信息写入 stdout 并从您的 java 程序中读取该信息:

import java.io.*;
...
Runtime rt = Runtime.getRuntime();

try {
  String[] env = {"/path/to/program"}; 
  Process proc = rt.exec("your_program", env);
  System.out.println("return value: " + proc.waitFor());

  InputStream stream = proc.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  while (reader.ready()) {
    // Do something with the data here.
    System.out.println(reader.readLine());
  }
}
catch (Exception ex) {
  System.err.println(ex);
}    

如果您需要向外部程序提供参数,则可以将它们作为字符串数组传递。例如,如果我想运行 ls -lh /etc/ 这就是以下代码的作用:

  String[] cmd = {"ls", "-lh", "/etc/"};
  String[] env = {"/bin/"};
  Process proc = rt.exec(cmd, env);

您也可以尝试使用 Java 本机接口,用于与可与 Fortran 交互的 C/C++ 代码进行通信。

One thing you could do is to run a separate program using Runtime to start a Process consisting of your fortran code. The following is an example of this:

Runtime rt = Runtime.getRuntime();

try {
  String[] env = {"/path/to/program"};
  Process proc = rt.exec("your_program", env);
  System.out.println("return value: " + proc.waitFor());
}
catch (Exception ex) {
  System.err.println(ex);
}

The above code will execute /path/to/program/your_program and wait for it to finish and then read off the return code.

Alternatively, you could write some information to stdout and read that from your java program:

import java.io.*;
...
Runtime rt = Runtime.getRuntime();

try {
  String[] env = {"/path/to/program"}; 
  Process proc = rt.exec("your_program", env);
  System.out.println("return value: " + proc.waitFor());

  InputStream stream = proc.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  while (reader.ready()) {
    // Do something with the data here.
    System.out.println(reader.readLine());
  }
}
catch (Exception ex) {
  System.err.println(ex);
}    

If you need to supply the external program with arguments you pass them as an array of strings. As an example, if I wanted to run ls -lh /etc/ that is what the following code does:

  String[] cmd = {"ls", "-lh", "/etc/"};
  String[] env = {"/bin/"};
  Process proc = rt.exec(cmd, env);

You could also try using Java Native Interface to communicate with C/C++ code which can interface with fortran.

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