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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
guilgamos
  • 1,695
  • 4
  • 13
  • 5
  • 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. – aviraldg Aug 15 '10 at 11:24
  • 1
    ... do you want to open the notepad program, or a text file that you created in notepad? – Stephen Aug 15 '10 at 11:25

7 Answers7

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
whiskeysierra
  • 5,030
  • 1
  • 29
  • 40
10

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

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
Hal
  • 101
  • 1
  • 3
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.

Community
  • 1
  • 1
Stephen
  • 6,027
  • 4
  • 37
  • 55
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();
        }
Makoto
  • 104,088
  • 27
  • 192
  • 230
Riyasam
  • 31
  • 1
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

Community
  • 1
  • 1
Mohsen
  • 3,512
  • 3
  • 38
  • 66
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(); 
}
mark
  • 21
  • 1
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();
Seda
  • 103
  • 2
  • 12