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?
Step 1: Open the notepad by pressing the Windows Key + R, type notepad and press enter key, or click on the Ok button. It opens the notepad. Step 2: Write a Java program that you want to compile and run. We have written the following code in the notepad.
Right click on file, Open with -> Other then select external programs and then choose eclipse .
In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open.... In macOS, you can open a TXT file with Apple TextEdit, which comes included with the operating system.
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.
(assuming you want notepad to open "myfile.txt" :)
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With