How to open the notepad file in java?
Asked Answered
G

7

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?

Gawky answered 15/8, 2010 at 11:22 Comment(2)
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.Anthracene
... do you want to open the notepad program, or a text file that you created in notepad?Fallfish
J
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.

Julissa answered 15/8, 2010 at 11:49 Comment(3)
You may want to add details about the required version of Java for this to work.Scaphoid
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.Guaiacum
Thanks @Andreas, fixed it. Making sure the file exists is part of guilgamos' job :) Good catch anyway, +1Julissa
G
10

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

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
Gollin answered 1/3, 2012 at 20:30 Comment(0)
F
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.

Fallfish answered 15/8, 2010 at 11:30 Comment(0)
H
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

Hamhung answered 15/8, 2010 at 11:33 Comment(0)
B
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(); 
}
Bastardize answered 16/11, 2011 at 9:35 Comment(0)
H
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();
        }
Hag answered 13/5, 2012 at 3:33 Comment(0)
I
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();
Immolation answered 25/7, 2014 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.