How to open notepad file in java?

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

How can I implement this case?

+7
java notepad
source share
7 answers

Try

if (Desktop.isDesktopSupported()) { Desktop.getDesktop().edit(file); } else { // dunno, up to you to handle this } 

Make sure the file exists. Thanks to Andreas for pointing this out.

+20
source share

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

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

Assuming you want to run the windows notepad.exe program, 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 in C:\Windows\notepad.exe :

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

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

Please note that you can also specify the third parameter exec , which is the working directory to run, so you can run the text file, which is stored relative to the working directory of your program.

+6
source share

Using SWT , you can run any. If you want to emulate double-clicking on text in windows, this is not possible with just a simple JRE. You can use your own library, such as SWT, and use the following code to open the file:

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

If you do not want to use a third-party library, you should know, and know where notepad.exe (or it appears in PATH):

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

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

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

+2
source share

In the IDE (Eclipse), it compiles "C: \ path \ to \ notepad.exe C: \ path \ to \ file.txt". So I used the following, which works for me, to support me and my IDE: o) Hope this helps others.

 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(); } 
+2
source share
 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(); } 
+2
source share

You can do this best if you start notepad on the command line with the command: start notepad

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

Save an array of string commands and give it as parametr in exec

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

All Articles