Java appelle la commande CMD et publie les informations d'affichage:
package com.anxin.cmd.test; Importer java.io.bufferedReader; Importer java.io.inputStreamReader; Command de classe publique {public static void main (string [] args) {try {runtime rt = runtime.getRuntime (); Processus pr = rt.exec ("cmd / c dir"); // cmd / c calc // process pr = rt.exec ("d: //xunlei//project.aspx"); BufferedReader Input = new BufferedReader (new InputStreamReader (pr.getInputStream (), "gbk")); Chaîne line = null; while ((line = input.readline ())! = null) {System.out.println (line); } int exitval = pr.waitfor (); System.out.println ("Over avec le code d'erreur" + EXITVAL); } catch (exception e) {System.out.println (e.toString ()); e.printStackTrace (); }}}Il y a trois façons dont Java démarre l'exe de l'application native:
La première méthode: utilisez la méthode CMD
/ ** * Exécutez la commande cmd * * @param commande * @throws ioException * / public static static exécuTeCTeCmd (String Command) lève ioException {log.info ("Execute Command:" + Command); Runtime runtime = runtime.getRuntime (); Process process = runtime.exec ("CMD / C" + Commande); BufferedReader br = new BufferedReader (new inputStreamReader (process.getInputStream (), "utf-8")); Chaîne line = null; StringBuilder build = new StringBuilder (); while ((line = br.readline ())! = null) {log.info (line); build.append (ligne); } return build.toString (); } ExecuteCmd (start "axadwebbrowser" "d: /axadsbrowser/axadwebbrowser.exe"); La deuxième méthode: utilisez ProcessBuilder pour appeler la méthode CMD
/ ** * Démarrez l'application * * @param programme ProgramName * @return * @throws ioException * / public static void startProgram (String Programpath) lève ioException {Log.info ("Démarrer l'application:" + Programpath); if (stringUtils.isnotblank (prorogrampath)) {try {string programName = prorogrampath.substring (prorogrampath.lastIndexof ("/") + 1, progratmpath.lastIndexof (".")); List <string> list = new ArrayList <string> (); list.add ("cmd.exe"); list.add ("/ c"); list.add ("start"); list.add ("/" "+ programname +" / ""); list.add ("/" "+ programmpath +" / ""); ProcessBuilder PSL PBUILDR = new ProcessBuilder (liste); PSHUILDER.START (); } catch (exception e) {e.printStackTrace (); Log.Error ("Application:" + Programpath + "n'existe pas!"); }}} La troisième voie: utilisez le bureau pour démarrer l'application
/ ** * Démarrez l'application * * @param programme ProgramName * @return * @throws ioException * / public static void startProgram (String Programpath) lève ioException {Log.info ("Démarrer l'application:" + Programpath); if (stringUtils.isnotblank (programmpath)) {try {Desktop.getDesktop (). Open (nouveau fichier (programmpath)); } catch (exception e) {e.printStackTrace (); Log.Error ("Application:" + Programpath + "n'existe pas!"); }}}