3
String cmds[] = new String[] { "cmd", "/c","C:\Test.txt" };
Runtime.getRuntime().exec(cmds);

Currently, I use the above code to open a txt file on local. However, it will be opened by notepad and not be kept the original format . I want to open default by notepad++ to fix this. Please tell me the way to process this case. Thanks.

5
  • Is notepad++ a default program for .txt files in your system? Commented Aug 9, 2013 at 10:12
  • 1
    try { "cmd", "/c","notepad++ C:\Test.txt" }; Commented Aug 9, 2013 at 10:13
  • @Satya's comment is relevant, but you may have cases where the notepad++ command isn't recognized by the command line, e.g. the path variable isn't set. Commented Aug 9, 2013 at 10:17
  • in that case, he will have to use full path of notepad++.exe , posting as answer Commented Aug 9, 2013 at 10:21
  • What's your fallback solution of Notepad++ is not installed? Commented Aug 9, 2013 at 10:25

5 Answers 5

3

You can use Desktop.getDesktop().open(file), but it takes the default system application for opening the specified file.

The advantage to this API is its platform independency.

However, you really need to thoroughly think about what you're going to do if:

  • Notepad++ installed, but not default program for .txt files
  • Notepad++ present, but not installed

  • Additional info from WVrock

    I want to point out that it is possible to use edit() instead of open() to open it with the system's default editing application instead of opening application. Note that an average user doesn't know how to change default editing application and it is usually set to notepad in windows.

    Sign up to request clarification or add additional context in comments.

    1 Comment

    I want to point out that it is possible to use edit() instead of open() to open it with the system's default editing application instead of opening application. Note that an average user doesn't know how to change default editing application and it is usually set to notepad in windows.
    2

    Try this:

    ProcessBuilder pb = new ProcessBuilder("Notepad++.exe", "myfile.txt");
    pb.start();
    

    OR

    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("C:\\path\\to\\notepad++.exe C:\\path\\to\\file.txt");
    

    For example, if notepad++ is located at C:\Windows\notepad++.exe:

    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("C:\\Windows\\notepad++.exe C:\\test.txt");
    

    Go to your notepad++ properties and see the path

    Comments

    1
                try {
                    String cmds[] = new String[] { "cmd", "/c","C:\\Program Files (x86)\\Notepad++\\notepad++.exe " , "C:\\Test.txt" };
                    Runtime.getRuntime().exec(cmds);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    

    1 Comment

    I had the same issue and was able to solve it with the code above. Make sure you change it accordigly depending on the location of your notepad++.exe. Also remember that Test.txt can be anywhere ... you just need to give the path name (i do it by right clicking on it and going to properties ... path should be ready for the trusty copy and paste treatment :))
    0

    Right click on that txt file and go properties and change open with to notepad++. Now run your code and you can see now that file open in notepad++.

    First case your computer use notepad as default text editor and by doing above steps it will change to notepad++

    1 Comment

    yes, however, I want to open in other machines. so, I want to open by notepad++ default without changing as above.
    0
    { "cmd", "/c","notepad++ C:\Test.txt" };
    

    should work , however in case you do not have notepad++ added in PATH the command will be

    { "cmd", "/c","fullpath-to-notepad++.exe C:\Test.txt" };
    

    1 Comment

    I hope the other machines @user2659694 will use the code on, will also run Windows. Because, different operating systems require different commands. He will have to have a separate method to return a command depending on the OS.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.