如何从java程序中执行vimdiff命令

时间:2022-01-07 21:52:36

I have a list of files for which I have to run the vimdiff command and save the output as a html file.I am doing this with Java. Below is the command I am trying to execute

我有一个文件列表,我必须运行vimdiff命令并将输出保存为html文件。我正在使用Java。下面是我试图执行的命令

String cmd = "vimdiff -c 'set foldlevel=9999' src/test/resources/testdata/output/html_output_before_changes/file1.html src/test/resources/testdata/output/html_output_after_changes/file2.html -c TOhtml -c 'w! different.html' -c 'qa!'"

When I run the below code, the code is getting executed. But I am not able to see the file getting generated.

当我运行下面的代码时,代码正在执行。但我无法看到生成的文件。

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(cmd);

The command is running fine when executed from a terminal. But its not working when executed inside a java program. Can someone help me with this issue? I did a lot of search but not able to proceed with this.

从终端执行时,该命令运行正常。但它在java程序中执行时不起作用。有人可以帮我解决这个问题吗?我做了很多搜索,但没能继续这个。

2 个解决方案

#1


1  

You're using :TOhtml and write the result as different.html. If you're not sure where to locate the file, check the current working directory of the Java process, do a file search of your hard disk, or specify an absolute path in the Vim command to be sure.

您正在使用:TOhtml并将结果写为different.html。如果您不确定文件的位置,请检查Java进程的当前工作目录,对硬盘执行文件搜索,或在Vim命令中指定绝对路径以确定。

You won't see anything from Vim's operation itself. Using process.getInputStream(), you could obtain what Vim wrote to the terminal during its operation, but that would just amount to a garble of characters, as Vim is using special ANSI escape sequences to control the terminal, position the cursor, etc.

你不会看到任何来自Vim操作本身的东西。使用process.getInputStream(),您可以获得Vim在操作期间写入终端的内容,但这只会造成大量字符,因为Vim使用特殊的ANSI转义序列来控制终端,定位光标等。

To use Vim non-interactively, it is recommended to pass the following options:

要以非交互方式使用Vim,建议传递以下选项:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-n                No swapfile.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                  user's settings).
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                  with messages or output to avoid blocking.

Without a possibility to interact with Vim (from inside your Java program), a troubleshooting tip is enabling verbose logging: You can capture a full log of a Vim session with -V20vimlog. After quitting Vim, examine the vimlog log file for errors.

如果没有与Vim交互的可能性(来自Java程序内部),则故障排除提示是启用详细日志记录:您可以使用-V20vimlog捕获Vim会话的完整日志。退出Vim后,检查vimlog日志文件是否有错误。

#2


0  

After Two days I found the below Solution:

两天后我找到了以下解决方案:

I added the vimdiff command to a shell script and executed it using the following method and it worked like a gem.

我将vimdiff命令添加到shell脚本并使用以下方法执行它,它像gem一样工作。

Java method

try {
            File[] uiDiffDir = getFiles();

            for (File file : uiDiffDir) {

                String[] cmd = {"sh", shellScriptPath, file1, file2,
                        outputfile};
                Process p = Runtime.getRuntime().exec(cmd);
                p.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

shell.sh

vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'

Note:

  • file1 will be passed as a argument in the place of $1
  • file1将作为参数传递给$ 1

  • file2 will be passed as a argument in the place of $2
  • file2将作为参数传递到$ 2的位置

  • outputfile will be passed as a argument in the place of $3
  • outputfile将作为参数传递给$ 3

#1


1  

You're using :TOhtml and write the result as different.html. If you're not sure where to locate the file, check the current working directory of the Java process, do a file search of your hard disk, or specify an absolute path in the Vim command to be sure.

您正在使用:TOhtml并将结果写为different.html。如果您不确定文件的位置,请检查Java进程的当前工作目录,对硬盘执行文件搜索,或在Vim命令中指定绝对路径以确定。

You won't see anything from Vim's operation itself. Using process.getInputStream(), you could obtain what Vim wrote to the terminal during its operation, but that would just amount to a garble of characters, as Vim is using special ANSI escape sequences to control the terminal, position the cursor, etc.

你不会看到任何来自Vim操作本身的东西。使用process.getInputStream(),您可以获得Vim在操作期间写入终端的内容,但这只会造成大量字符,因为Vim使用特殊的ANSI转义序列来控制终端,定位光标等。

To use Vim non-interactively, it is recommended to pass the following options:

要以非交互方式使用Vim,建议传递以下选项:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-n                No swapfile.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                  user's settings).
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                  with messages or output to avoid blocking.

Without a possibility to interact with Vim (from inside your Java program), a troubleshooting tip is enabling verbose logging: You can capture a full log of a Vim session with -V20vimlog. After quitting Vim, examine the vimlog log file for errors.

如果没有与Vim交互的可能性(来自Java程序内部),则故障排除提示是启用详细日志记录:您可以使用-V20vimlog捕获Vim会话的完整日志。退出Vim后,检查vimlog日志文件是否有错误。

#2


0  

After Two days I found the below Solution:

两天后我找到了以下解决方案:

I added the vimdiff command to a shell script and executed it using the following method and it worked like a gem.

我将vimdiff命令添加到shell脚本并使用以下方法执行它,它像gem一样工作。

Java method

try {
            File[] uiDiffDir = getFiles();

            for (File file : uiDiffDir) {

                String[] cmd = {"sh", shellScriptPath, file1, file2,
                        outputfile};
                Process p = Runtime.getRuntime().exec(cmd);
                p.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

shell.sh

vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'

Note:

  • file1 will be passed as a argument in the place of $1
  • file1将作为参数传递给$ 1

  • file2 will be passed as a argument in the place of $2
  • file2将作为参数传递到$ 2的位置

  • outputfile will be passed as a argument in the place of $3
  • outputfile将作为参数传递给$ 3