1

I have a file named error.log.1.

I would like to tell Java to use the system editor to open this file.

If it were named error.log, then the following would work:

Desktop.getDesktop().edit(new File("error.log") );

However, since it is not a recognized file extension, it doesn't open. Instead I get an error:

Exception in thread "main" java.io.IOException: Failed to open error.log.1. 
Error message: No application is associated with the specified file for this operation.

at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at net.joshuad.hypnos.workbench.EditorTest.main(EditorTest.java:9)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Grumblesaurus
  • 3,021
  • 3
  • 31
  • 61

3 Answers3

0

I am not sure what "System Editor" is, but if it is a specific application you want to open, you'll need to run that application and pass the log filename as an argument.

You will need to determine the path to the application and then you can use the Runtime.getRuntime().exec() method to open the file.

For example, if you wanted to open the log file using Notepad, you could do so like this:

Runtime.getRuntime().exec("C:\\Windows\\System32\\notepad.exe error.log.1");

This, of course, assumes the application accepts a filename as a parameter. You'll need to look into what commandline syntax this System Editor has.

Zephyr
  • 9,885
  • 4
  • 28
  • 63
0

There are two ways to handle this problem:

  1. renaming your file and then open it as you mentioned;
  2. explicitly using an command (the editor) to open the file;

In the second case, it will be something as:

private static void openByCommand(String filePath){
    try {
        Process process = new ProcessBuilder("gedit", filePath)
                .directory(new File("/home/hearen")) // set up your working directory;
                .start();
        int exitCode = process.waitFor();
        System.out.println(exitCode);
    } catch (IOException | InterruptedException ignored) {
        ignored.printStackTrace();
    }
}
Hearen
  • 7,420
  • 4
  • 53
  • 63
  • This thread (https://stackoverflow.com/questions/325299/cross-platform-way-to-open-a-file-using-java-1-5) provides some alternatives. Third-party libraries JDIC and SWT were mentioned. I would love to have a platform independent way to trigger an open file action, and have the operating system/user handle locating a suitable editor, but there seems to be no such thing. In this question's case the file is known as a log file, personally I'd rename the log files to error.[n].log if possible, or rename the file before displaying. – KC Wong Aug 13 '18 at 04:18
0

As suggested by Zephyr, a good solution would be to specify the path to the default editor (Write some code to be platform specific). Or better yet, you could package your own cross-platform text editor, something like jEdit could be a good option.

However, if you really want to use Desktop.getDesktop().edit(path), then a hack is to simply check the file extension, and if it is unknown then add to ".log" or ".txt" to the end.

A bit like this:

File originalName = new File("path/to/my/file/error.log.1");
File appendedName = new File(originalName.getAbsolutePath()+".log");

boolean success = originalName.renameTo(appendedName);

if (success) {
   Desktop.getDesktop().edit(appendedName);
}

//Change it back when you are done:
appendedName.renameTo(originalName);

Obviously this will not work if multiple sources/applications are reading from the original file at the same time (You could make a copy instead of renaming it), but it may fit your use case.

sorifiend
  • 5,927
  • 1
  • 28
  • 45