7

I want to open Notepad in my Java program. Suppose that I have one button if I click this button the notepad will appear. I already have a file name and a directory.

How can I implement this case?

2
  • 3
    What exactly do you mean by notepad? A crappy text editing program used on Windows, or a TextArea control? Forgive me for assuming "things", but it sounds like you don't know the basics of Swing/AWT. Commented Aug 15, 2010 at 11:24
  • 1
    ... do you want to open the notepad program, or a text file that you created in notepad? Commented Aug 15, 2010 at 11:25

7 Answers 7

21

Try

if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().edit(file);
} else {
    // I don't know, up to you to handle this
}

Make sure the file exists. Thanks to Andreas_D who pointed this out.

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

3 Comments

You may want to add details about the required version of Java for this to work.
Hey, didn't know that. cool. But - second line has to read Desktop.getDesktop().edit(file);. And the file has to be created, otherwise it'll complain with an IllegalArgumentException.
Thanks @Andreas, fixed it. Making sure the file exists is part of guilgamos' job :) Good catch anyway, +1
10

(assuming you want notepad to open "myfile.txt" :)

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

Comments

5

Assuming you wish to launch the windows program notepad.exe, you are looking for the exec function. You probably want to call something like:

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

For example, on my machine notepad is located at C:\Windows\notepad.exe:

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

This will open notepad with the file test.txt open for editing.

Note you can also specify a third parameter to exec which is the working directory to execute from - therefore, you could launch a text file that is stored relative to the working directory of your program.

Comments

2

Using SWT, you can launch any If you want to emulate double-clicking on a text in windows, it's not possible only with a plain JRE. You can use a native library like SWT and use the following code to open a file:

    org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")

If you don't want to use a third-party lib, you should know and you know where notepad.exe is (or it's visible in PATH):

    runtime.exec("notepad.exe c:\path\to\file.txt");

Apache common-exec is a good library for handling external process execution.

UPDATE: A more complete answer to your question can be found here

Comments

2

In IDE (Eclipse) it compains about "C:\path\to\notepad.exe C:\path\to\file.txt" . So i have used the following which works for me keeping me and my IDE happy :o) Hopefully this will help others out there.

String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
       try { 
        Runtime runtime = Runtime.getRuntime();         
        Process process = runtime.exec("explorer " + fPath);

Thread.sleep(500); //lets give the OS some time to open the file before deleting

    boolean success = (new File(fPath)).delete();
    if (!success) {
        System.out.println("failed to delete file :"+fPath);
        // Deletion failed
    }

} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); 
}

Comments

2
String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
        String[] commands = {"cmd", "/c", fileName};
        try {
            Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

Comments

0

You could do this the best if you start notepad in command line with command: start notepad

String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };

Save array of string commands and give it like parametr in exec

Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();

Comments

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.