Posso fare in modo che da un programma in java venga aperto un file di testo con il programma predefinito dal SO?
Ad esempio si apra con blocco note in Windows.
Posso fare in modo che da un programma in java venga aperto un file di testo con il programma predefinito dal SO?
Ad esempio si apra con blocco note in Windows.
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++
penso che devi controllare l'estensione e aprire il programma di default, se l'utente cambia l'associazione problema suo
if (est.equals("txt")) {
String[] envp = {file};
Process process = Runtime.getRuntime.exec("notepad", envp);
}
Scusa, non mi č mai capitato di aprire un altro programma dal mio, mi pare di aver capito che il codice chiave č:
ma a cosa serve la riga precedente!codice:Process process = Runtime.getRuntime.exec("notepad", envp);
Grazie
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++
serve a impostare i parametri a linea di comando
notepad file.txt
con exec va fatto un array dei parametri, in questo caso solo il file
Non si legge moltocodice:exec public Process exec(String cmd, String[] envp) throws IOException Executes the specified string command in a separate process with the specified environment. This method breaks the command string into tokens and creates a new array cmdarray containing the tokens in the order that they were produced by the string tokenizer; it then performs the call exec(cmdarray, envp). The token parsing is done by a StringTokenizer created by the call: new StringTokenizer(command) with no further modification of the character categories. The environment variable settings are specified by envp. If envp is null, the subprocess inherits the environment settings of the current process. Parameters: cmd - a specified system command.envp - array of strings, each element of which has environment variable settings in format name=value. Returns: a Process object for managing the subprocess. Throws: SecurityException - if a security manager exists and its checkExec method doesn't allow creation of a subprocess. IOException - if an I/O error occurs NullPointerException - if cmd is null IllegalArgumentException - if cmd is emptySee Also: exec(java.lang.String[]), exec(java.lang.String[], java.lang.String[]), SecurityManager.checkExec(java.lang.String
Una letturina alle api non farebbe male
Avrebbe potuto pure scrivere
non sarebbe cambiato nullacodice:if (est.equals("txt")) { Process process = Runtime.getRuntime.exec("notepad", new String[]{file}); }
Perchč quel metodo accetta come parametro un array di String
Ciauz
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
Scusate un povero autodidatta java ignorante......sigh sigh
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++
:adhone:
Scusato :tongue: :tongue: :tongue: :tongue:
CIauz
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
ma solo per stavolta
:sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp:
Non ci riesco....ho inserito questo codice:
tuttavia mi appare una finestra di notepad.exe in cui dice:"applicazione non inizializzata correttamente...."codice:String[] envp = {" c:/Readme.txt"}; try{ Process processo = Runtime.getRuntime().exec("notepad",envp);} catch(IOException e){System.out.println(e.toString());}
Perchč?
Grazie a tutti
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++
Ho risolto in questo modo, ma non sō perchč ora va...:quote:
ps: mi sono letto le API e ho cpito :gren:codice:try{ Process processo=Runtime.getRuntime().exec("notepad Readme.txt"); } catch(IOException e){System.out.println(e.toString());}
Grazie ciao
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++