-1

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

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.

Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17

2 Answers2

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.

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.

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

-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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I tried providing the path for the output html also. vimdiff -c 'set foldlevel=9999' /Users/kmohanavelu/Documents/contentserv/contentservFunctionalTests/src/test/resources/testdata/output/html_output_before_changes/PPC000331_GB_en_GB_31bdc2f5cd58ef2b.html /Users/kmohanavelu/Documents/contentserv/contentservFunctionalTests/src/test/resources/testdata/output/html_output_after_changes/PPC000331_GB_en_GB_31bdc2f5cd58ef2b.html -c TOhtml -c 'w! /Users/username/Documents/gitrepo/reponame/src/test/resources/testdata/output/html_code_comparison/different.html' -c 'qa!' – Kishore Mohanavelu Sep 20 '18 at 11:10
  • This command is working from terminal but not from the java program. That's the issue I am facing – Kishore Mohanavelu Sep 20 '18 at 11:11
  • Oh, that wasn't clear from your question. I've added some more recommendations and troubleshooting tips; that's all I can do. If you suspect problems with the missing terminal, you could try starting `gvim` instead, or wrap the Vim invocation in a terminal emulator (e.g. `xterm`). Starting Vim non-interactively from Java is not a good solution IMHO. – Ingo Karkat Sep 20 '18 at 11:18
  • The suggestions did not work out. I tried all the options but did not work. But after so much search I found a solution. Updated the solution in my question. Please see above. But thanks for reaching out a helping hand. I am much grateful :) – Kishore Mohanavelu Sep 21 '18 at 11:41
  • Glad you've figured it out; making it more modular with a wrapper script is a good idea (though I still don't see the root cause of the problem). Don't forget to consider the Vim options I've mentioned, too! – Ingo Karkat Sep 21 '18 at 11:49
  • Yup I won't forget it!! Thanks – Kishore Mohanavelu Sep 21 '18 at 11:51
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.

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
  • file2 will be passed as a argument in the place of $2
  • outputfile will be passed as a argument in the place of $3
Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17